File tree Expand file tree Collapse file tree 4 files changed +37
-1
lines changed Expand file tree Collapse file tree 4 files changed +37
-1
lines changed Original file line number Diff line number Diff 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 ) ;
Original file line number Diff line number Diff line change 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 ;
45using System . CommandLine . Parsing ;
56using System . Linq ;
6- using FluentAssertions ;
77using Xunit ;
88
99namespace 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 }
Original file line number Diff line number Diff 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 ;
Original file line number Diff line number Diff 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 ;
You can’t perform that action at this time.
0 commit comments