1
1
using System ;
2
+ using System . Collections ;
3
+ using System . Collections . Generic ;
2
4
using System . Linq ;
3
5
using CommandLine . Tests . Fakes ;
4
6
using CommandLine . Text ;
5
7
using FluentAssertions ;
8
+ using Microsoft . FSharp . Core ;
6
9
using Xunit ;
7
10
using Xunit . Abstractions ;
8
11
@@ -22,26 +25,115 @@ public void Parse_option_with_aliased_verbs(string args, Type expectedArgType)
22
25
{
23
26
var arguments = args . Split ( ' ' ) ;
24
27
object options = null ;
28
+ IEnumerable < Error > errors = null ;
25
29
var result = Parser . Default . ParseArguments < AliasedVerbOption1 , AliasedVerbOption2 > ( arguments )
26
- . WithParsed ( ( o ) => options = o )
30
+ . WithParsed ( o => options = o )
31
+ . WithNotParsed ( o => errors = o )
27
32
;
33
+ if ( errors != null && errors . Any ( ) )
34
+ {
35
+ foreach ( Error e in errors )
36
+ {
37
+ System . Console . WriteLine ( e . ToString ( ) ) ;
38
+ }
39
+ }
28
40
29
41
Assert . NotNull ( options ) ;
30
42
Assert . Equal ( expectedArgType , options . GetType ( ) ) ;
31
43
}
32
44
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" } ) ]
34
115
public class AliasedVerbOption1
35
116
{
36
117
[ Option ( 'a' , "alpha" , Required = true ) ]
37
118
public string Option { get ; set ; }
38
119
}
39
120
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
+ ) ]
41
126
public class AliasedVerbOption2
42
127
{
43
128
[ Option ( 'a' , "alpha" , Required = true ) ]
44
129
public string Option { get ; set ; }
45
130
}
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
+ }
46
138
}
47
139
}
0 commit comments