diff --git a/src/System.CommandLine.Tests/ArgumentTests.cs b/src/System.CommandLine.Tests/ArgumentTests.cs index 51351ef5e1..19ffb3db3f 100644 --- a/src/System.CommandLine.Tests/ArgumentTests.cs +++ b/src/System.CommandLine.Tests/ArgumentTests.cs @@ -765,6 +765,24 @@ public void OnlyTake_can_pass_on_all_tokens_from_a_single_arity_argument_to_anot } } + [Fact] + public void Argument_of_enum_can_limit_enum_members_as_valid_values() + { + var argument = new Argument() + .FromAmong(ConsoleColor.Red.ToString(), ConsoleColor.Green.ToString()); + Command command = new("set-color") + { + argument + }; + + var result = command.Parse("set-color Fuschia"); + + result.Errors + .Select(e => e.Message) + .Should() + .BeEquivalentTo(new[] { $"Argument 'Fuschia' not recognized. Must be one of:\n\t'Red'\n\t'Green'" }); + } + protected override Symbol CreateSymbol(string name) { return new Argument(name); diff --git a/src/System.CommandLine.Tests/OptionTests.cs b/src/System.CommandLine.Tests/OptionTests.cs index f4a7f8a23f..dc3ac47b23 100644 --- a/src/System.CommandLine.Tests/OptionTests.cs +++ b/src/System.CommandLine.Tests/OptionTests.cs @@ -1,9 +1,9 @@ // Copyright (c) .NET Foundation and contributors. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. +using FluentAssertions; using System.CommandLine.Parsing; using System.Linq; -using FluentAssertions; using Xunit; namespace System.CommandLine.Tests @@ -370,6 +370,20 @@ public void Option_of_boolean_defaults_to_false_when_not_specified() .Should() .BeFalse(); } + + [Fact] + public void Option_of_enum_can_limit_enum_members_as_valid_values() + { + var option = new Option("--color") + .FromAmong(ConsoleColor.Red.ToString(), ConsoleColor.Green.ToString()); + + var result = option.Parse("--color Fuschia"); + + result.Errors + .Select(e => e.Message) + .Should() + .BeEquivalentTo(new[] { $"Argument 'Fuschia' not recognized. Must be one of:\n\t'Red'\n\t'Green'" }); + } protected override Symbol CreateSymbol(string name) => new Option(name); } diff --git a/src/System.CommandLine/ArgumentExtensions.cs b/src/System.CommandLine/ArgumentExtensions.cs index 3d153dd346..e43d286cd1 100644 --- a/src/System.CommandLine/ArgumentExtensions.cs +++ b/src/System.CommandLine/ArgumentExtensions.cs @@ -76,7 +76,9 @@ public static TArgument FromAmong( params string[] values) where TArgument : Argument { + argument.AllowedValues?.Clear(); argument.AddAllowedValues(values); + argument.Completions.Clear(); argument.Completions.Add(values); return argument; diff --git a/src/System.CommandLine/OptionExtensions.cs b/src/System.CommandLine/OptionExtensions.cs index 79558cf043..d27166f2f4 100644 --- a/src/System.CommandLine/OptionExtensions.cs +++ b/src/System.CommandLine/OptionExtensions.cs @@ -25,7 +25,9 @@ public static TOption FromAmong( params string[] values) where TOption : Option { + option.Argument.AllowedValues?.Clear(); option.Argument.AddAllowedValues(values); + option.Argument.Completions.Clear(); option.Argument.Completions.Add(values); return option;