dotnet / dotnet/command-line-api
Option/Argument/Command static initialization pattern for specifying validators
- Dominant language
- C#
- Stars
- 3.7k
- Forks
- 428
- PR merge metrics
- No merged PRs in 30d
Description
Is there a way or could we add a way to do static initialization of a Command/Argument/Option _with_ validators? Currently, the `Validators` list is private and only mutable via the `AddValidator` method. I've started declaring shared Options with the following pattern which feels a little cumbersome. Perhaps I'm just missing a more elegant way to do it, though. Otherwise, making `Validators` a public property would make static initialization easier.
This:
```csharp
static private Option _eventRateOption = null;
static public Option EventRateOption
{
get
{
if (_eventRateOption != null)
return _eventRateOption;
_eventRateOption = new Option(
alias: "--event-rate",
getDefaultValue: () => -1,
description: "...");
_eventRateOption.AddValidator(ValidatorOne);
_eventRateOption.AddValidator(ValidatorTwo);
_eventRateOption.AddValidator(ValidatorThree);
return _eventRateOption;
}
private set {}
}
```
vs.
```csharp
static public Option EventRateOption =
new Option(
alias: "--event-rate",
getDefaultValue: () => -1,
description: "The rate of events in events/sec. -1 means 'as fast as possible'.")
{
Validators = new List<...>()
{
ValidatorOne,
ValidatorTwo,
ValidatorThree
}
};
```
Contributor guide
Assessment
This issue has not been assessed yet.