dotnet / dotnet/command-line-api
Global options in command handler: ParseResult.ValueForOption<T>(string) does not return anything
- Dominant language
- C#
- Stars
- 3.7k
- Forks
- 428
- PR merge metrics
- No merged PRs in 30d
Description
I'm trying to set up a command that needs access to a global option. This is done through InvocationContext.
When retrieving an option from a ParseResult through the ValueForOption(Option) overload this works as expected, but when using the ValueForOption(string optionName) overload, it doesn't find the option/value.
cf. unit test below
```
[Fact]
public void Testcase()
{
var child1 = new Command("child1");
var fooOption = new Option("--foo");
child1.AddGlobalOption(fooOption);
var root = new RootCommand();
root.AddCommand(child1);
var child2 = new Command("child2");
var barOption = new Option("--bar");
child2.AddOption(barOption);
string foo = null, bar = null;
child2.Handler = CommandHandler.Create((InvocationContext context) =>
{
// this works:
//foo = context.ParseResult.ValueForOption(fooOption);
//bar = context.ParseResult.ValueForOption(barOption);
//this doesn't:
foo = context.ParseResult.ValueForOption("--foo");
bar = context.ParseResult.ValueForOption("--bar");
});
child1.AddCommand(child2);
root.Invoke("child1 --foo=FOO child2 --bar=BAR");
Assert.Equal("FOO", foo);
Assert.Equal("BAR", bar);
}
```
Contributor guide
Assessment
This issue has not been assessed yet.