gsscoder / commandline

Terse syntax C# command line parser for .NET with F# support
1.63k stars 293 forks source link

All unknown arguments are ignored despite IgnoreUnknownArguments being false #385

Open cannontrodder opened 7 years ago

cannontrodder commented 7 years ago

In this repro pulled from my unit tests, I am checking that parsing fails when the full option 'source' is used but with a single '-' character. This actually parses:

image

I understand why "ource" is parsed as the value for "Source" in my options class - because I am using the short switch name by using one '-' character. In my opinion that makes 'sourcepath' and 'thisshouldnotbehere' unknown arguments, and I'd expect parsing to fail.

Is this expected behaviour?

void Main()
{
    var commandLine = "validate thisshouldnotbehere -source sourcepath thisshouldnotbehere";

    var args = commandLine.Split(' ');

    var parser = new Parser(with =>
    {
        with.CaseSensitive = false;
        with.HelpWriter = Console.Out;
        with.IgnoreUnknownArguments = false;
    });

    var result = parser.ParseArguments<CommandLineOptionsValidateOptions>(args);

    if (result.Tag == ParserResultType.NotParsed)
        throw new Exception("Unable to parse command line.");

    var parsedValue = ((Parsed<CommandLineOptionsValidateOptions>)result).Value;
}

[Verb("validate")]
public class CommandLineOptionsValidateOptions 
{
    [Option('s', "Source", Required = true)]
    public string Source { get; set; }

    [Option('m', "MultiDatabase")]
    public bool MultiDatabase { get; set; }

    [Option('d', "Database", Required = false)]
    public string DatabaseName { get; set; }
}
dbjorge commented 7 years ago

:+1:, if a user specifies positional values against an options class with no [Value] members and IgnoreUnknownArguments = false, I'd expect it to be an error.