-1

The test program below works as desired using the DEFAULT string having code points like \u00FC, as well as if that type of code point is coded as a sting within the prog. Passing the same string from cmd line like: prog.exe -input="ABC\u00FC" does NOT. I assumed it was os interaction so tried other quoting, even wrapping like: "(ABC\u00FC)" and trimming the parens inside the func NG.

Is the "for _, runeRead := range []rune" incorrect for escaped values?

package main

import (
        "fmt"
        "flag"
        "os"
)

var input string
var m = make(map[rune]struct{})

func init() {
        flag.StringVar(&input, "input", "A7\u00FC", "string of runes")
        m['A'] = struct{}{}
        m['\u00FC'] = struct{}{}
        m['7'] = struct{}{}
}

func main() {
        flag.Parse()
        ck(input)      // cmd line - with default OK
        ck("A\u00FC")  // hard code - OK
}

func ck(in string) {
        for _, runeRead := range []rune(in)  {
                fmt.Printf("DEBUG: Testing rune: %v %v\n", string(runeRead), byte(runeRead))

                if _, ok := m[runeRead]; ! ok {
                        fmt.Printf("\nERROR: Invalid entry <%v>, in string <%s>.\n", string(runeRead), in)
                        os.Exit(9)
                }
        }
}

Soluntion needs to work windows and linux.

2 Answers2

0

https://ss64.com/nt/syntax-esc.html

^ Escape character.

Adding the escape character before a command symbol allows it to be treated as ordinary text. When piping or redirecting any of these characters you should prefix with the escape character: & \ < > ^ |

e.g.  ^\  ^&  ^|  ^>  ^<  ^^ 

So you should do

prog.exe -input="ABC^\u00FC"
dave
  • 62,300
  • 5
  • 72
  • 93
  • No good. I understand your reference in terms of windows commands, but this is inside a string passed to the golang exe. The "for loop with the range" is seeing the ^ as a stand aloe char, just as it saw the \ of \u. But if NOT from the command line i.e. using the default arg to flags its fine. $ ./exp.exe DEBUG: Testing rune: A 65 DEBUG: Testing rune: 7 55 DEBUG: Testing rune: ü 252 owner@Mr-Finn MINGW64 /c/users/owner/wdl $ ./exp.exe -input="A7^\u00FC" DEBUG: Testing rune: A 65 DEBUG: Testing rune: 7 55 DEBUG: Testing rune: ^ 94 ERROR: Invalid entry <^> – Bill Lanahan Apr 13 '20 at 20:03
  • Maybe this is an issue with or lack of understanding with the flag pkg. I had to do a horrible workaround to get past this issue. – Bill Lanahan Apr 14 '20 at 22:05
0

in case it helps others

It apparently is that different OSs and/or shells (in my case bash) are having issue with the the "\u" of the unicode character. In bash at the cmd line the user could enter $' the characters ' to protect the \u. It was suggested that WITHIN the program if a string had the same issue that the strconv.Quote could have been a solution.

Since I wanted an OS/shell independent solution for non-computer savvy users, I did a slightly more involved workaround.

I tell users to enter the unicode that needs the \u format to use %FC instead of \u00FC. I parse the string from the command line i.e. ABC%FC%F6123 with rexexp and inside my GO code I replace the %xx with the unicode rune as I had originally expected to get it. With a few lines of code the user input is now OS agnostic.