commandline
commandline copied to clipboard
MissingRequiredOptionError when specifying required Value after enumerable Options
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?
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; }
}
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.
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.
I'm still having issues on the master branch with the code snippet I posted above, as well as @SuperFXMaster's.
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.