dotnet / dotnet/command-line-api

API proposal: AcceptOnlyFromAmong for enum

Open
#1,960 1 comment 0 reactions 0 assignees View on GitHub
Area-API enhancement
Dominant language
C#
Stars
3.7k
Forks
428
PR merge metrics
No merged PRs in 30d

Description

```patch
namespace System.CommandLine {
public partial class ArgumentValidation {
+ public static Argument AcceptOnlyFromAmong(
+ this Argument argument,
+ params TEnum[] values)
+ where TEnum: struct, System.Enum;
}

public partial class OptionValidation {
+ public static Option AcceptOnlyFromAmong(
+ this Option option,
+ params TEnum[] values)
+ where TEnum: struct, System.Enum;
}
}
```

Same behaviour as in Argument.AcceptOnlyFromAmong(params string[]) and Option.AcceptOnlyFromAmong(params string[]), except:

- These are only applicable for enum types
- The return type is different (but still matches the `this` parameter)
- The `values` parameter is TEnum[] rather than string[], and the method calls ToString or similar on each value (not sure what to do about values composed of multiple bit flags)
- Validation is case-insensitive like the Enum.Parse call in ArgumentConverter (see also )

Inspired by

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.