3

I am trying to use bool argument in my console application. I'm using the CommandLineParser Package, but parser return error.

this is my option

    [Option("randomize", Required = false, DefaultValue = false, HelpText = "Enter \"true\" for the random selection")]
    public bool Randomize { get; set; }

argument: --randomize=true

I am using Parser.Default.ParseArguments

Any idea why this doesn't work?

nvoigt
  • 75,013
  • 26
  • 93
  • 142
Tomáš Čičman
  • 281
  • 1
  • 5
  • 17

1 Answers1

4

You don't need to add true or false as the argument value - CommandLineParser will just set the value to true if the argument is present.

So this will pass in a value of true:

--randomize

So if your application is called ParserApp the following passes true

ParserApp --randomize

While the below line will pass false

ParserApp

The Quick Start guide has an example for the usage of a boolean parameter.

Matt Hogan-Jones
  • 2,981
  • 1
  • 29
  • 35
  • 1
    So it looks like setting `DefaultValue = true` in the `Option` attribute for a `bool` option would make it such that the option could never be set to `false` when executing? Since `--randomize=false` can't be parsed, as mentioned. – Michael Hornfeck Jun 27 '19 at 13:59