commandlineparser / commandline

The best C# command line parser that brings standardized *nix getopt style, for .NET. Includes F# support
MIT License
4.46k stars 473 forks source link

Looking to parse "default" value after initializing object with factory #908

Closed TalZaccai closed 7 months ago

TalZaccai commented 7 months ago

Hi all,

I'm currently initializing my options object using a custom factory (i.e. ParseArguments(factory, args)). I also am not using any explicit default values in the Option attributes. When the args contain a default value (e.g. false for an boolean option), ParseArguments doesn't override the matching property value, however it leaves it as the value initialized by the factory. Is there any way to "force" ParseArguments to override the property value in this case?

Example code snippet:

// Override initialized value with default value
var myArgs = new string[] { "--recover", "false" };
ParserResult<Options> result = parser.ParseArguments(() => new Options
{
    Recover = true
}, myArgs);
var cmdLineOptions = result.MapResult(o => o, xs => new Options());
Assert.AreEqual(false, cmdLineOptions.Recover); // This fails

Appreciate your help!

Tal.

TalZaccai commented 7 months ago

Just realized that the issue here was that booleans are treated as flags and that the following value is actually ignored.