commandline icon indicating copy to clipboard operation
commandline copied to clipboard

Passing "0" to boolean option results in "true" value

Open ptylenda opened this issue 8 years ago • 2 comments

Define option:

[Option('e', "engineering", SetName = "console", Required = false, HelpText = "Flag whether the build is engineering build")]
        public bool Engineering { get; set; }

When passing "-e 0" to the executable, it results in boolean "true" value, which is unexpected. In conjunction with #353 it results in no possibility to have command line option which would accept 1/0/true/false and work correctly

ptylenda avatar Sep 20 '16 11:09 ptylenda

Unfortunately, this appears to be by design, @ptylenda . Take the following example:

[Option('e', "engineering")]
public bool Engineering { get; set; }

[Value(0)]
public int Count { get; set; }

How would the library parse the arguments -e 0? The current master branch sets Engineering to true and Count to 0. Adding the your suggestion would either make the arguments ambiguous or break compatibility with the previous argument parsing result.

Please let me know if I misinterpreted your suggestion.

anthonylangsworth avatar Oct 22 '16 12:10 anthonylangsworth

Grammar Details article on wiki explains why it doesn't work as you would expect. Boolean properties are flags which means value is set (to true) when flag is in command line. -e 0 maps to Engineering set to true and unbound value 0. If you really need passing true/false/1/0 arguments you can use enum like shown in CommandLine.Demo project and mentioned in Mapping Properties to Options:

enum BooleanValue {false, true}
//...
[Option('e')]
public BooleanValue Engineering { get; set; }

You can use then both -e true or -e 1 in command line.

IMO this issue can be rejected.


EDIT: Of course my remarks apply to version 1.9 while this issue seems to regard upcoming v2.

Agbar avatar Jan 04 '17 20:01 Agbar