Skip to content

Commit eda3d19

Browse files
Keboojonsequitur
authored andcommitted
Adding proposed fix for FromAmong
The behavior of the method now matches its description.
1 parent 4d9cc58 commit eda3d19

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

src/System.CommandLine.Tests/ArgumentTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,24 @@ public void OnlyTake_can_pass_on_all_tokens_from_a_single_arity_argument_to_anot
765765
}
766766
}
767767

768+
[Fact]
769+
public void Argument_of_enum_can_limit_enum_members_as_valid_values()
770+
{
771+
var argument = new Argument<ConsoleColor>()
772+
.FromAmong(ConsoleColor.Red.ToString(), ConsoleColor.Green.ToString());
773+
Command command = new("set-color")
774+
{
775+
argument
776+
};
777+
778+
var result = command.Parse("set-color Fuschia");
779+
780+
result.Errors
781+
.Select(e => e.Message)
782+
.Should()
783+
.BeEquivalentTo(new[] { $"Argument 'Fuschia' not recognized. Must be one of:\n\t'Red'\n\t'Green'" });
784+
}
785+
768786
protected override Symbol CreateSymbol(string name)
769787
{
770788
return new Argument<string>(name);

src/System.CommandLine.Tests/OptionTests.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// Copyright (c) .NET Foundation and contributors. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4+
using FluentAssertions;
45
using System.CommandLine.Parsing;
56
using System.Linq;
6-
using FluentAssertions;
77
using Xunit;
88

99
namespace System.CommandLine.Tests
@@ -370,6 +370,20 @@ public void Option_of_boolean_defaults_to_false_when_not_specified()
370370
.Should()
371371
.BeFalse();
372372
}
373+
374+
[Fact]
375+
public void Option_of_enum_can_limit_enum_members_as_valid_values()
376+
{
377+
var option = new Option<ConsoleColor>("--color")
378+
.FromAmong(ConsoleColor.Red.ToString(), ConsoleColor.Green.ToString());
379+
380+
var result = option.Parse("--color Fuschia");
381+
382+
result.Errors
383+
.Select(e => e.Message)
384+
.Should()
385+
.BeEquivalentTo(new[] { $"Argument 'Fuschia' not recognized. Must be one of:\n\t'Red'\n\t'Green'" });
386+
}
373387

374388
protected override Symbol CreateSymbol(string name) => new Option<string>(name);
375389
}

src/System.CommandLine/ArgumentExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ public static TArgument FromAmong<TArgument>(
7676
params string[] values)
7777
where TArgument : Argument
7878
{
79+
argument.AllowedValues?.Clear();
7980
argument.AddAllowedValues(values);
81+
argument.Completions.Clear();
8082
argument.Completions.Add(values);
8183

8284
return argument;

src/System.CommandLine/OptionExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ public static TOption FromAmong<TOption>(
2525
params string[] values)
2626
where TOption : Option
2727
{
28+
option.Argument.AllowedValues?.Clear();
2829
option.Argument.AddAllowedValues(values);
30+
option.Argument.Completions.Clear();
2931
option.Argument.Completions.Add(values);
3032

3133
return option;

0 commit comments

Comments
 (0)