英文:
Backslashes are removed unless quoted in command line flags
问题
我正在使用flag包来解释在命令行输入的标志。
我使用以下代码创建了一个变量:
ptrString := flag.String("string", "", "A test string")
flag.Parse()
然后当我想要打印它时,
fmt.Println("You entered " + *ptrString)
如果我输入像-string=hello!这样的命令行参数,它会打印"hello!"。
如果我输入像-string=hello\Bob这样的命令行参数,它会打印"helloBob"。
有没有一种推荐的方法将标志参数转换或解释为不删除反斜杠的字符串?(这在Linux和OS X上进行了测试,如果shell干扰的话...)
英文:
I'm using the flag package to interpret flags entered at the command line.
I created a variable using
ptrString := flag.String("string", "", "A test string")
flat.Parse()
Then when I want to print it,
fmt.Println("You entered " + *ptrString)
If I enter something like -string=hello! as a command line argument, it prints "hello!"
If I enter something like -string=hello\Bob as a command line argument, it prints "helloBob"
Is there a recommended way to convert or interpret the flag argument to a string that doesn't remove the backslash? (This is being tested on Linux and OS X, if the shell is interfering...)
答案1
得分: 2
在shell中具有特殊含义的字符需要进行引用或转义。你可以在shell的man页面中找到完整的列表(在man 1 bash
中的“Quoting”部分)。
在这种情况下,你可以引用或转义反斜杠:
-string=hello\\Bob
// 或者
-string='hello\Bob'
英文:
Characters that have special meaning in the shell need to be quoted or escaped. You can find complete list in the shell's man pages (under "Quoting" in man 1 bash
).
In this case, you can either quote or escape the baskslash
-string=hello\\Bob
// or
-string='hello\Bob'
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论