j-maly / CommandLineParser

Command line parser. Declarative arguments support. Rich set of argument types (switches, enums, files, etc...). Mutually exclusive arguments validations.
MIT License
137 stars 30 forks source link

Default values are being ignored - regression #70

Closed kjbartel closed 3 years ago

kjbartel commented 4 years ago

After upgrading to latest version (3.0.20), parsing now ignores the default value if the argument isn't defined in the arguments list. It instead will use the default value for the type. e.g. 0 for an int or enum.

public enum OptionsFlag
{
    O1, O2, O3
}

public class Options
{
    [ValueArgument(typeof( OptionsFlag), 'f', "flag", DefaultValue = OptionsFlag.O3)]
    public OptionsFlag Flag = OptionsFlag.O3;

    public static Options ParseOptions(string[] args)
    {
        var options = new Options();
        Debug.Assert(options.Flag == OptionsFlag.O3);
        var parser = new CommandLineParser.CommandLineParser();
        parser.ExtractArgumentAttributes(options);

        parser.ParseCommandLine(args);
        // This will fail when args is empty! it has been set to O1
        Debug.Assert(options.Flag == OptionsFlag.O3);

        return options;
    }
}
kjbartel commented 4 years ago

Ok this isn't just enum. All default values are being ignored. For example an int with a default value of -1 will be changed to 0.

StefH commented 3 years ago

https://github.com/j-maly/CommandLineParser/pull/68