commandlineparser / commandlineparser/commandline

Use separate options for enum values

Aperta
#817 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
C#
Stelle
4.8k
Fork
478
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

Having support for `enum` flags is great! But sometimes you don't want the user to use the actual enum values. In fact, they could easily be re-factored causing breaking changes for existing command line integrations.

It would be nice if you could specify names which would then be used instead of the enum values. E.g. using the [DisplayAttribute](https://docs.microsoft.com/en-us/dotnet/api/system.componentmodel.dataannotations.displayattribute?view=net-6.0).

```c#
[Flags]
public enum MyEnum
{
None = 0,
[Display(Name = "a")]
VeryLongEnumValueNameHere = 1,
[Display(Name = "b")]
EvenLongerEnumValueNameHereWithNoRiskForTypos = 2,
[Display(Name = "c")]
WouldBeNiceWithShorterName = 4
}
```

The corresponding command line could look like this: `foo.exe --my-enum-value a|b|c`

Or even better...

**Allow OptionAttribute on enum values?**
We could allow the `OptionAttribute` on enum values and get separate arguments for each enum flag:
`foo.exe -a -b -c`

```c#
[Flags]
public enum MyEnum
{
None = 0,
[Option('a', "long-value", HelpText = "Long value")]
VeryLongEnumValueNameHere = 1,
[Option('b', "even-longer-value", HelpText = "Even longer value")]
EvenLongerEnumValueNameHereWithNoRiskForTypos = 2,
[Option('c', "nice", HelpText = "Would be nice")]
WouldBeNiceWithShorterName = 4
}
```

Feasible?

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.