dotnet / dotnet/command-line-api
CommandResult missing ValueForOption<T>
- Dominant language
- C#
- Stars
- 3.7k
- Forks
- 428
- PR merge metrics
- No merged PRs in 30d
Description
CommandResult used to contain a ValueForOption method that could fetch values during validation, but that was removed by this [commit](https://github.com/dotnet/command-line-api/commit/2d02d36569030d7b96a2b59f9cfa4ed53310dd80#).
Example code:
```cs
var command = new Command("someCommand", "Does something interesting")
{
new Option(
new [] {"--boolOption"},
getDefaultValue: () => false,
"Some bool option"),
new Option(
new [] {"--intOption"},
getDefaultValue: () => 1,
"Some int option")
};
command.AddValidator(result =>
{
try
{
var boolOptionValue= result.ValueForOption("boolOption");
var intOptionValue = result.ValueForOption("intOption");
// This code ^ no longer compiles :-(
if (!boolOptionValue && intOptionValue == 42)
{ return "You can't do that."; }
}
#pragma warning disable RCS1075, CS0168
catch (Exception e)
{
// Ignore exceptions here, any type issues will be caught elsewhere
}
#pragma warning disable RCS1075, CS0168
return null;
});
```
Is there a better way to achieve this or was the removal of the ValueForOption method an accident?
Contributor guide
Assessment
This issue has not been assessed yet.