11using System ;
2+ using System . Collections ;
3+ using System . Collections . Generic ;
24using System . Linq ;
35using CommandLine . Tests . Fakes ;
46using CommandLine . Text ;
57using FluentAssertions ;
8+ using Microsoft . FSharp . Core ;
69using Xunit ;
710using Xunit . Abstractions ;
811
@@ -22,26 +25,115 @@ public void Parse_option_with_aliased_verbs(string args, Type expectedArgType)
2225 {
2326 var arguments = args . Split ( ' ' ) ;
2427 object options = null ;
28+ IEnumerable < Error > errors = null ;
2529 var result = Parser . Default . ParseArguments < AliasedVerbOption1 , AliasedVerbOption2 > ( arguments )
26- . WithParsed ( ( o ) => options = o )
30+ . WithParsed ( o => options = o )
31+ . WithNotParsed ( o => errors = o )
2732 ;
33+ if ( errors != null && errors . Any ( ) )
34+ {
35+ foreach ( Error e in errors )
36+ {
37+ System . Console . WriteLine ( e . ToString ( ) ) ;
38+ }
39+ }
2840
2941 Assert . NotNull ( options ) ;
3042 Assert . Equal ( expectedArgType , options . GetType ( ) ) ;
3143 }
3244
33- [ Verb ( "move" , aliases : new string [ ] { "mv" } ) ]
45+ [ Theory ]
46+ [ InlineData ( "--help" , true , new string [ ]
47+ {
48+ "copy, cp, cpy (Default Verb) Copy some stuff" ,
49+ "move, mv" ,
50+ "delete Delete stuff" ,
51+ "help Display more information on a specific command." ,
52+ "version Display version information." ,
53+ } ) ]
54+ [ InlineData ( "help" , true , new string [ ]
55+ {
56+ "copy, cp, cpy (Default Verb) Copy some stuff" ,
57+ "move, mv" ,
58+ "delete Delete stuff" ,
59+ "help Display more information on a specific command." ,
60+ "version Display version information." ,
61+ } ) ]
62+ [ InlineData ( "move --help" , false , new string [ ]
63+ {
64+ "-a, --alpha Required." ,
65+ "--help Display this help screen." ,
66+ "--version Display version information." ,
67+ } ) ]
68+ [ InlineData ( "mv --help" , false , new string [ ]
69+ {
70+ "-a, --alpha Required." ,
71+ "--help Display this help screen." ,
72+ "--version Display version information." ,
73+ } ) ]
74+ [ InlineData ( "delete --help" , false , new string [ ]
75+ {
76+ "-b, --beta Required." ,
77+ "--help Display this help screen." ,
78+ "--version Display version information." ,
79+ } ) ]
80+ public void Parse_help_option_for_aliased_verbs ( string args , bool verbsIndex , string [ ] expected )
81+ {
82+ var arguments = args . Split ( ' ' ) ;
83+ object options = null ;
84+ IEnumerable < Error > errors = null ;
85+ // the order of the arguments here drives the order of the commands shown
86+ // in the help message
87+ var result = Parser . Default . ParseArguments <
88+ AliasedVerbOption2 ,
89+ AliasedVerbOption1 ,
90+ VerbNoAlias
91+ > ( arguments )
92+ . WithParsed ( o => options = o )
93+ . WithNotParsed ( o => errors = o )
94+ ;
95+
96+ var message = HelpText . AutoBuild ( result ,
97+ error => error ,
98+ ex => ex ,
99+ verbsIndex : verbsIndex
100+ ) ;
101+
102+ string helpMessage = message . ToString ( ) ;
103+ var helps = helpMessage . Split ( new [ ] { '\r ' , '\n ' } , StringSplitOptions . RemoveEmptyEntries ) . Skip ( 2 ) . ToList < string > ( ) ;
104+
105+ expected . Length . Should ( ) . Be ( helps . Count ) ;
106+ int i = 0 ;
107+ foreach ( var expect in expected )
108+ {
109+ helps [ i ] . Trim ( ) . Should ( ) . Be ( expect ) ;
110+ i ++ ;
111+ }
112+ }
113+
114+ [ Verb ( "move" , aliases : new string [ ] { "mv" } ) ]
34115 public class AliasedVerbOption1
35116 {
36117 [ Option ( 'a' , "alpha" , Required = true ) ]
37118 public string Option { get ; set ; }
38119 }
39120
40- [ Verb ( "copy" , aliases : new string [ ] { "cp" } ) ]
121+ [ Verb ( "copy" ,
122+ isDefault : true ,
123+ aliases : new string [ ] { "cp" , "cpy" } ,
124+ HelpText = "Copy some stuff"
125+ ) ]
41126 public class AliasedVerbOption2
42127 {
43128 [ Option ( 'a' , "alpha" , Required = true ) ]
44129 public string Option { get ; set ; }
45130 }
131+
132+ [ Verb ( "delete" , HelpText = "Delete stuff" ) ]
133+ public class VerbNoAlias
134+ {
135+ [ Option ( 'b' , "beta" , Required = true ) ]
136+ public string Option { get ; set ; }
137+ }
46138 }
47139}
0 commit comments