除非在命令行标志中被引用,否则会删除反斜杠。

huangapple go评论95阅读模式
英文:

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'

huangapple
  • 本文由 发表于 2015年7月29日 03:00:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/31684942.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定