dotnet / dotnet/command-line-api

CommandResult missing ValueForOption<T>

Open
#1,119 3 comments 0 reactions 0 assignees View on GitHub
question
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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.