@@ -11,10 +11,95 @@ namespace System.CommandLine.Tests
1111{
1212 public partial class ParserTests
1313 {
14- public class DoubleDash
14+ public class DefaultDoubleDashBehavior
1515 {
16+ [ Fact ] // https://github.com/dotnet/command-line-api/issues/1238
17+ public void Subsequent_tokens_are_parsed_as_arguments_even_if_they_match_option_identifiers ( )
18+ {
19+ var option = new Option < string [ ] > ( new [ ] { "-o" , "--one" } ) ;
20+ var argument = new Argument < string [ ] > ( ) ;
21+ var rootCommand = new RootCommand
22+ {
23+ option ,
24+ argument
25+ } ;
26+
27+ var result = new CommandLineBuilder ( rootCommand )
28+ . EnableLegacyDoubleDashBehavior ( false )
29+ . Build ( )
30+ . Parse ( "-o \" some stuff\" -- -o --one -x -y -z -o:foo" ) ;
31+
32+ result . HasOption ( option ) . Should ( ) . BeTrue ( ) ;
33+
34+ result . GetValueForOption ( option ) . Should ( ) . BeEquivalentTo ( "some stuff" ) ;
35+
36+ result . GetValueForArgument ( argument ) . Should ( ) . BeEquivalentSequenceTo ( "-o" , "--one" , "-x" , "-y" , "-z" , "-o:foo" ) ;
37+
38+ result . UnparsedTokens . Should ( ) . BeEmpty ( ) ;
39+ }
40+
41+ [ Fact ]
42+ public void Unparsed_tokens_is_empty ( )
43+ {
44+ var option = new Option < string [ ] > ( new [ ] { "-o" , "--one" } ) ;
45+ var argument = new Argument < string [ ] > ( ) ;
46+ var rootCommand = new RootCommand
47+ {
48+ option ,
49+ argument
50+ } ;
51+
52+ var result = new CommandLineBuilder ( rootCommand )
53+ . EnableLegacyDoubleDashBehavior ( false )
54+ . Build ( )
55+ . Parse ( "-o \" some stuff\" -- --one -x -y -z -o:foo" ) ;
56+
57+ result . UnparsedTokens . Should ( ) . BeEmpty ( ) ;
58+ }
59+
60+ [ Fact ] // https://github.com/dotnet/command-line-api/issues/1631
61+ public void No_errors_are_generated ( )
62+ {
63+ var option = new Option < string [ ] > ( new [ ] { "-o" , "--one" } ) ;
64+ var argument = new Argument < string [ ] > ( ) ;
65+ var rootCommand = new RootCommand
66+ {
67+ option ,
68+ argument
69+ } ;
70+
71+ var result = new CommandLineBuilder ( rootCommand )
72+ . EnableLegacyDoubleDashBehavior ( false )
73+ . Build ( )
74+ . Parse ( "-o \" some stuff\" -- -o --one -x -y -z -o:foo" ) ;
75+
76+ result . Errors . Should ( ) . BeEmpty ( ) ;
77+ }
78+
1679 [ Fact ]
17- public void When_legacy_behavior_is_enabled_then_the_portion_of_the_command_line_following_a_double_dasare_treated_as_unparsed_tokens ( )
80+ public void A_second_double_dash_is_parsed_as_an_argument ( )
81+ {
82+ var argument = new Argument < string [ ] > ( ) ;
83+ var rootCommand = new RootCommand
84+ {
85+ argument
86+ } ;
87+
88+ var result = new CommandLineBuilder ( rootCommand )
89+ . EnableLegacyDoubleDashBehavior ( false )
90+ . Build ( )
91+ . Parse ( "a b c -- -- d" ) ;
92+
93+ var strings = result . GetValueForArgument ( argument ) ;
94+
95+ strings . Should ( ) . BeEquivalentSequenceTo ( "a" , "b" , "c" , "--" , "d" ) ;
96+ }
97+ }
98+
99+ public class LegacyDoubleDashBehavior
100+ {
101+ [ Fact ]
102+ public void The_portion_of_the_command_line_following_a_double_is_treated_as_unparsed_tokens ( )
18103 {
19104 var result = new CommandLineBuilder ( new RootCommand { new Option ( "-o" ) } )
20105 . EnableLegacyDoubleDashBehavior ( )
@@ -27,7 +112,7 @@ public void When_legacy_behavior_is_enabled_then_the_portion_of_the_command_line
27112 }
28113
29114 [ Fact ]
30- public void When_legacy_behavior_is_enabled_then_a_double_dash_specifies_that_tokens_matching_options_will_be_treated_as_unparsed_tokens ( )
115+ public void Subsequent_tokens_matching_options_will_be_treated_as_unparsed_tokens ( )
31116 {
32117 var optionO = new Option ( new [ ] { "-o" } ) ;
33118 var optionX = new Option ( new [ ] { "-x" } ) ;
@@ -59,47 +144,19 @@ public void When_legacy_behavior_is_enabled_then_a_double_dash_specifies_that_to
59144 }
60145
61146 [ Fact ]
62- public void When_legacy_behavior_is_disabled_then_a_double_dash_specifies_that_further_command_line_args_will_be_treated_as_arguments ( )
63- {
64- var option = new Option < string [ ] > ( new [ ] { "-o" , "--one" } ) ;
65- var argument = new Argument < string [ ] > ( ) ;
66- var rootCommand = new RootCommand
67- {
68- option ,
69- argument
70- } ;
71-
72- var result = new CommandLineBuilder ( rootCommand )
73- . EnableLegacyDoubleDashBehavior ( false )
74- . Build ( )
75- . Parse ( "-o \" some stuff\" -- -o --one -x -y -z -o:foo" ) ;
76-
77- result . HasOption ( option ) . Should ( ) . BeTrue ( ) ;
78-
79- result . GetValueForOption ( option ) . Should ( ) . BeEquivalentTo ( "some stuff" ) ;
80-
81- result . GetValueForArgument ( argument ) . Should ( ) . BeEquivalentSequenceTo ( "-o" , "--one" , "-x" , "-y" , "-z" , "-o:foo" ) ;
82-
83- result . UnparsedTokens . Should ( ) . BeEmpty ( ) ;
84- }
85-
86- [ Fact ]
87- public void When_legacy_behavior_is_disabled_then_a_second_double_dash_is_parsed_as_an_argument ( )
147+ public void Subsequent_tokens_matching_argument_will_be_treated_as_unparsed_tokens ( )
88148 {
89- var argument = new Argument < string [ ] > ( ) ;
149+ var argument = new Argument < int [ ] > ( ) ;
90150 var rootCommand = new RootCommand
91151 {
92152 argument
93153 } ;
94-
95154 var result = new CommandLineBuilder ( rootCommand )
96- . EnableLegacyDoubleDashBehavior ( false )
155+ . EnableLegacyDoubleDashBehavior ( )
97156 . Build ( )
98- . Parse ( "a b c -- -- d " ) ;
157+ . Parse ( "1 2 3 -- 4 5 6 7 " ) ;
99158
100- var strings = result . GetValueForArgument ( argument ) ;
101-
102- strings . Should ( ) . BeEquivalentSequenceTo ( "a" , "b" , "c" , "--" , "d" ) ;
159+ result . GetValueForArgument ( argument ) . Should ( ) . BeEquivalentSequenceTo ( 1 , 2 , 3 ) ;
103160 }
104161 }
105162 }
0 commit comments