commandline icon indicating copy to clipboard operation
commandline copied to clipboard

MissingRequiredOptionError when specifying required Value after enumerable Options

Open SuperFXMaster opened this issue 8 years ago • 5 comments

The following options class:

public class CommandLineOptions
{
    [Value(0, Required = true)]
    public string InputFileName { get; set; }

    [Option('o', "output")]
    public string OutputFileName { get; set; }

    [Option('i', "include", Separator = ',')]
    public IEnumerable<string> Included { get; set; }

    [Option('e', "exclude", Separator = ',')]
    public IEnumerable<string> Excluded { get; set; }
}

When provided to the default parser, with the commandline app.exe --exclude=a,b InputFile.txt results in a MissingRequiredOptionError: A required value not bound to option name is missing. I expected the library to accept InputFileName = InputFile.txt and --exclude=a,b as Excluded = { "a", "b" }. Is there something else required to make this work as I expect, or is it a bug?

SuperFXMaster avatar Oct 11 '16 19:10 SuperFXMaster

Yes, I think that's a bug. If you set Required=false you'll see that a, b, and InputFile.txt are all part of Included. Additionally, the "EnableDashDash" setting doesn't work either:

void Main()
{
    var p = new Parser(s =>
    {
        s.EnableDashDash = true;
    });
    var result = p.ParseArguments<CommandLineOptions>("--exclude=a,b -- InputFile.txt".Split());
    result.WithNotParsed(e => e.Dump());
    result.WithParsed(d => d.Dump());
}

public class CommandLineOptions
{
    [Value(0, Required = true)]
    public string InputFileName { get; set; }

    [Option('o', "output")]
    public string OutputFileName { get; set; }

    [Option('i', "include", Separator = ',')]
    public IEnumerable<string> Included { get; set; }

    [Option('e', "exclude", Separator = ',')]
    public IEnumerable<string> Excluded { get; set; }
}

nemec avatar Oct 11 '16 19:10 nemec

As a workaround, the commandline --exclude=a,b --output=OutFile InFile works fine, just as -e a b c -o OutFile InFile does. It seems like the problem arises when the --exclude option is directly before the Required parameter.

SuperFXMaster avatar Oct 12 '16 16:10 SuperFXMaster

This appears to be fixed in the master branch, @SuperFXMaster and @nemec . I cannot reproduce the issue. Please let me know if I am incorrect.

anthonylangsworth avatar Oct 22 '16 11:10 anthonylangsworth

I'm still having issues on the master branch with the code snippet I posted above, as well as @SuperFXMaster's.

nemec avatar Nov 05 '16 07:11 nemec

Had quick look into this and could let the example above to run by changing the order of parameters in Concat in the following line: https://github.com/gsscoder/commandline/blob/master/src/CommandLine/Core/Tokenizer.cs#L54 The changed line becomes: return tokenizerResult.Map(tokens => values.Concat(tokens));

I'm not sure it's valid for other cases though. Maybe someone who knows more about it can have a look. It would be nice to get a stable version.

hugener avatar Mar 13 '17 18:03 hugener