GO flag pkg reading option string containing escaped runes like "\u00FC" won't read

636 Views Asked by At

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

There are 2 best solutions below

2
On

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"
0
On

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.