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
765
765
}
766
766
}
767
767
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
+
768
786
protected override Symbol CreateSymbol ( string name )
769
787
{
770
788
return new Argument < string > ( name ) ;
Original file line number Diff line number Diff line change 1
1
// Copyright (c) .NET Foundation and contributors. All rights reserved.
2
2
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
3
4
+ using FluentAssertions ;
4
5
using System . CommandLine . Parsing ;
5
6
using System . Linq ;
6
- using FluentAssertions ;
7
7
using Xunit ;
8
8
9
9
namespace System . CommandLine . Tests
@@ -370,6 +370,20 @@ public void Option_of_boolean_defaults_to_false_when_not_specified()
370
370
. Should ( )
371
371
. BeFalse ( ) ;
372
372
}
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
+ }
373
387
374
388
protected override Symbol CreateSymbol ( string name ) => new Option < string > ( name ) ;
375
389
}
Original file line number Diff line number Diff line change @@ -76,7 +76,9 @@ public static TArgument FromAmong<TArgument>(
76
76
params string [ ] values )
77
77
where TArgument : Argument
78
78
{
79
+ argument . AllowedValues ? . Clear ( ) ;
79
80
argument . AddAllowedValues ( values ) ;
81
+ argument . Completions . Clear ( ) ;
80
82
argument . Completions . Add ( values ) ;
81
83
82
84
return argument ;
Original file line number Diff line number Diff line change @@ -25,7 +25,9 @@ public static TOption FromAmong<TOption>(
25
25
params string [ ] values )
26
26
where TOption : Option
27
27
{
28
+ option . Argument . AllowedValues ? . Clear ( ) ;
28
29
option . Argument . AddAllowedValues ( values ) ;
30
+ option . Argument . Completions . Clear ( ) ;
29
31
option . Argument . Completions . Add ( values ) ;
30
32
31
33
return option ;
You can’t perform that action at this time.
0 commit comments