Commandline: Values are not set properly wit multiple bool arguments

Created on 1 Apr 2020  路  4Comments  路  Source: commandlineparser/commandline

I am passing 2 bool options, one on true and the other on false
But after being convert to the class all are true;

    static void Main(string[] args)
    {
        args = new[] {"-p 88", "-t true", "-s false"};
        var parseResult = Parser.Default.ParseArguments<Options>(args)
            .WithParsed(o =>
                {
                    Console.WriteLine($"PartnerId: -p {o.PartnerId}");
                    Console.WriteLine($"Test: -t {o.Test.ToString().ToLower()}");
                    Console.WriteLine($"SuppressEmail: -s {o.SuppressEmail.ToString().ToLower()}");
                }
            );
    }
}

public class Options
{
    [Option('p', "partnerId", Required = true, HelpText = "p = PartnerId")]
    public int PartnerId { get; set; }

    [Option('t', "test", HelpText = "Allows the app to be run on test mode, using only the allowed accounts")]
    public bool Test { get; set; }

    [Option('s', "suppressEmail", HelpText = "The app will not send the email to Post Up")]
    public bool SuppressEmail { get; set; }
}

Most helpful comment

@SommerEngineering

Then, it is impossible to set the boolean parameter to false by any means.
You can do.

Using nullable bool, you can toggle the value of option, example

[Option(Default = (bool)true)]
public bool? Visible {get;set;}

commandline can be:

--visible true   # Visible =true
--visible false # Visible =false
no option for visible  # Visible =true, the default value is set to true
no defaultvalue # Visible=null

All 4 comments

Boolean options don't take a true or false argument, if -s is present then its true if its missing its false.

See: https://github.com/gsscoder/commandline/wiki/Grammar-Details

Thanks @SplatterPenguin. Had the same issue. In combination with Default=true things getting worse: Then, it is impossible to set the boolean parameter to false by any means.

@SommerEngineering

Then, it is impossible to set the boolean parameter to false by any means.
You can do.

Using nullable bool, you can toggle the value of option, example

[Option(Default = (bool)true)]
public bool? Visible {get;set;}

commandline can be:

--visible true   # Visible =true
--visible false # Visible =false
no option for visible  # Visible =true, the default value is set to true
no defaultvalue # Visible=null

Thanks @moh-hassan for this hint

Was this page helpful?
0 / 5 - 0 ratings