44

I try to pass a boolean parameter to a console application and process the value with the Command Line Parser Library.

[Option('c', "closeWindow", Required = true, HelpText = "Close the window.")]
public bool CloseWindow { get; set; }

I tried to pass the parameter as

-c false
-c False
-c "false"
-...

There are no differences, on each try I get "true" as value.

Can anyone tell me how I have to pass the parameter to get the boolean false value?

To avoid possible asks, there is a string option which is passed correctly:

[Option('s', "system", Required = true, HelpText = "Any help text")]
public string System { get; set; }
Sebastian S.
  • 1,173
  • 3
  • 13
  • 22
  • I would have thought Boolean parameters are present or not present. They don't require and argument - no idea how that works with `Required = true` – James Mar 08 '16 at 17:26

3 Answers3

45

You don't need to add True or False. Using -c will evaluate to True. Not using it will evaluate to False. Somewhere in the documentation there is an example with -v for verbose output. But I can't find it right now. I guess Required=true is not necessary for Boolean options.

arne.z
  • 3,242
  • 3
  • 24
  • 46
  • You are right, passing -c is evaluated as true if -c is not passed i get false. But what is if I want to use the "Required" option? – Sebastian S. Mar 08 '16 at 17:38
  • I think the library is not intended to use it this way, but you could make your option a string and then match the string to evaluate your Boolean. You can probably do that in the getter function of your option. – arne.z Mar 08 '16 at 17:44
  • 16
    How about if default is 'True', how would this parameter make sense? Not specifying it means 'True' because of the default, specifying it means 'True', because is specified. – Kosmo Jun 27 '18 at 16:41
  • 8
    IMHO, that looks like a design fault – momo Mar 12 '19 at 16:03
  • 1
    @momo, that's because it is. – A.R. Jan 26 '22 at 16:13
44

bool? behaves the way you want

with:

[Option('c', "closeWindow", Required = true, HelpText = "Close the window.")]
public bool? CloseWindow { get; set; }

the result will be:

-c false // -> false
-c true  // -> true
-c       // -> error
         // -> error if Required = true, null otherwise
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
klev
  • 622
  • 7
  • 10
0

Here is a work-around to consider:

Change the names of the options so false is always the default. If you want "close window" to be the default, then the name of the option becomes -w "keepWindowOpen".

James Lawruk
  • 30,112
  • 19
  • 130
  • 137