Golang boolean flag parsing restriction

时间:2017-10-12 09:46:13

标签: go

Can someone elaborate a little more about an explanation from official golang documentation about cmd syntax for boolean flags.

One or two minus signs may be used; they are equivalent. The last form is not permitted for boolean flags because the meaning of the command

cmd -x *

will change if there is a file called 0, false, etc. You must use the -flag=false form to turn off a boolean flag.

I don't get the point. Could you explain it or give an example?

1 个答案:

答案 0 :(得分:6)

If you run cmd -x * in a shell, the shell will try to expand the * into the list of all files in the current directory. If the current directory contains exactly one file that is named "0" or "false", the shell will execute cmd -x 0 or cmd -x false respectively.

It is unclear whether cmd -x false is meant to be understood as cmd -x=true false (with "false" being a positional argument), or cmd -x=false. So the flag package forces you to make it explicit by requiring the equal sign.

相关问题