dotnet / dotnet/command-line-api
Completions for Enum values provide all enum names instead of provided
- Dominant language
- C#
- Stars
- 3.7k
- Forks
- 428
- PR merge metrics
- No merged PRs in 30d
Description
I have an enum type:
```csharp
public enum JsonWebSignatureAlgorithmType
{
Unsupported = 0,
Hs256 = 1,
Hs384 = 2,
Hs512 = 3,
Rs256 = 10,
Rs384 = 11,
Rs512 = 12,
Ps256 = 20,
Ps384 = 21,
Ps512 = 22,
Es256 = 30,
Es384 = 31,
Es512 = 32,
}
```
And use that to create an option:
```csharp
var option = new Option(
"-algorithm",
"The algorithm."
) {
Arity = ArgumentArity.ExactlyOne,
IsRequired = true
};
option.GetArgument().Completions.Clear();
var names = Enum.GetNames(typeof(JsonWebSignatureAlgorithmType))
.Where(
n =>
!n.Equals(nameof(JsonWebSignatureAlgorithmType.Unsupported), StringComparison.OrdinalIgnoreCase)
&& !n.StartsWith("HS", StringComparison.OrdinalIgnoreCase)
&& !n.StartsWith("PS", StringComparison.OrdinalIgnoreCase))
.ToArray();
option.AddCompletions(names);
// same behavior with
//option.FromAmong(names);
```
Now I call the executable with the `--help` argument.
**Expected behavior:**
The output looks like this - The *filtered* names:
```
-algorithm The algorithm.
(REQUIRED)
```
**Actual behavior:**
The output looks like this - The *un-filtered* names:
```
-algorithm The algorithm.
(REQUIRED)
```
**Workaround:**
As the Argument is a internal member I can only access It by doing dirty and unwanted reflection to clear the completions collection.
```csharp
...
var optionType = typeof(Option);
var argumentProperty =
_s_optionType.GetProperty(
"Argument",
BindingFlags.Instance | BindingFlags.NonPublic);
var argument = argumentProperty .GetValue(option) as Argument;
argument.Completions.Clear();
```
** Wishlist**
At least the `FromAmong`-call should clear the collections for validation and completion, or provide a boolean argument (force), which will do so (more explicit).
Contributor guide
Assessment
This issue has not been assessed yet.