BadScript 2
Loading...
Searching...
No Matches
Issue6Tests.cs
Go to the documentation of this file.
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.Linq;
7using FluentAssertions;
8using Microsoft.FSharp.Core;
9using Xunit;
10using Xunit.Abstractions;
11
12//Issue #6
13//Support Aliases on verbs (i.e. "move" and "mv" are the same verb).
14
16{
17 public class Issue6Tests
18 {
24 [Theory]
25 [InlineData("move -a bob", typeof(AliasedVerbOption1))]
26 [InlineData("mv -a bob", typeof(AliasedVerbOption1))]
27 [InlineData("copy -a bob", typeof(AliasedVerbOption2))]
28 [InlineData("cp -a bob", typeof(AliasedVerbOption2))]
29 [InlineData("-a bob", typeof(AliasedVerbOption2))]
30 public void Parse_option_with_aliased_verbs(string args, Type expectedArgType)
31 {
32 var arguments = args.Split(' ');
33 object options = null;
34 IEnumerable<Error> errors = null;
36 .WithParsed(o => options = o)
37 .WithNotParsed(o => errors = o)
38 ;
39 if (errors != null && errors.Any())
40 {
41 foreach (Error e in errors)
42 {
43 System.Console.WriteLine(e.ToString());
44 }
45 }
46
47 Assert.NotNull(options);
48 Assert.Equal(expectedArgType, options.GetType());
49 }
50
56 [Theory]
57 [InlineData("move -a bob", typeof(AliasedVerbOption1))]
58 [InlineData("mv -a bob", typeof(AliasedVerbOption1))]
59 [InlineData("delete -b fred", typeof(VerbNoAlias))]
60 public void Parse_option_with_aliased_verb(string args, Type expectedArgType)
61 {
62 var arguments = args.Split(' ');
63 object options = null;
64 IEnumerable<Error> errors = null;
66 .WithParsed(o => options = o)
67 .WithNotParsed(o => errors = o)
68 ;
69 if (errors != null && errors.Any())
70 {
71 foreach (Error e in errors)
72 {
73 System.Console.WriteLine(e.ToString());
74 }
75 }
76
77 Assert.NotNull(options);
78 Assert.Equal(expectedArgType, options.GetType());
79 }
80
87 [Theory]
88 [InlineData("--help", true, new string[]
89 {
90 "copy, cp, cpy (Default Verb) Copy some stuff",
91 "move, mv",
92 "delete Delete stuff",
93 "help Display more information on a specific command.",
94 "version Display version information.",
95 })]
96 [InlineData("help", true, new string[]
97 {
98 "copy, cp, cpy (Default Verb) Copy some stuff",
99 "move, mv",
100 "delete Delete stuff",
101 "help Display more information on a specific command.",
102 "version Display version information.",
103 })]
104 [InlineData("move --help", false, new string[]
105 {
106 "-a, --alpha Required.",
107 "--help Display this help screen.",
108 "--version Display version information.",
109 })]
110 [InlineData("mv --help", false, new string[]
111 {
112 "-a, --alpha Required.",
113 "--help Display this help screen.",
114 "--version Display version information.",
115 })]
116 [InlineData("delete --help", false, new string[]
117 {
118 "-b, --beta Required.",
119 "--help Display this help screen.",
120 "--version Display version information.",
121 })]
122 public void Parse_help_option_for_aliased_verbs(string args, bool verbsIndex, string[] expected)
123 {
124 var arguments = args.Split(' ');
125 object options = null;
126 IEnumerable<Error> errors = null;
127 // the order of the arguments here drives the order of the commands shown
128 // in the help message
129 var result = Parser.Default.ParseArguments<
133 >(arguments)
134 .WithParsed(o => options = o)
135 .WithNotParsed(o => errors = o)
136 ;
137
138 var message = HelpText.AutoBuild(result,
139 error => error,
140 ex => ex,
141 verbsIndex: verbsIndex
142 );
143
144 string helpMessage = message.ToString();
145 var helps = helpMessage.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries).Skip(2).ToList<string>();
146
147 expected.Length.Should().Be(helps.Count);
148 int i = 0;
149 foreach (var expect in expected)
150 {
151 helps[i].Trim().Should().Be(expect);
152 i++;
153 }
154 }
155
162 [Theory]
163 [InlineData("--help", true, new string[]
164 {
165 "move, mv",
166 "delete Delete stuff",
167 "help Display more information on a specific command.",
168 "version Display version information.",
169 })]
170 [InlineData("help", true, new string[]
171 {
172 "move, mv",
173 "delete Delete stuff",
174 "help Display more information on a specific command.",
175 "version Display version information.",
176 })]
177 [InlineData("move --help", false, new string[]
178 {
179 "-a, --alpha Required.",
180 "--help Display this help screen.",
181 "--version Display version information.",
182 })]
183 [InlineData("mv --help", false, new string[]
184 {
185 "-a, --alpha Required.",
186 "--help Display this help screen.",
187 "--version Display version information.",
188 })]
189 [InlineData("delete --help", false, new string[]
190 {
191 "-b, --beta Required.",
192 "--help Display this help screen.",
193 "--version Display version information.",
194 })]
195 public void Parse_help_option_for_aliased_verbs_no_default(string args, bool verbsIndex, string[] expected)
196 {
197 var arguments = args.Split(' ');
198 object options = null;
199 IEnumerable<Error> errors = null;
200 // the order of the arguments here drives the order of the commands shown
201 // in the help message
202 var result = Parser.Default.ParseArguments<
205 >(arguments)
206 .WithParsed(o => options = o)
207 .WithNotParsed(o => errors = o)
208 ;
209
210 var message = HelpText.AutoBuild(result,
211 error => error,
212 ex => ex,
213 verbsIndex: verbsIndex
214 );
215
216 string helpMessage = message.ToString();
217 var helps = helpMessage.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries).Skip(2).ToList<string>();
218
219 expected.Length.Should().Be(helps.Count);
220 int i = 0;
221 foreach (var expect in expected)
222 {
223 helps[i].Trim().Should().Be(expect);
224 i++;
225 }
226 }
227
228 [Verb("move",
229 aliases: new string[] { "mv" }
230 )]
232 {
233 [Option('a', "alpha", Required = true)]
234 public string Option { get; set; }
235 }
236
237 [Verb("copy",
238 isDefault: true,
239 aliases: new string[] { "cp", "cpy" },
240 HelpText = "Copy some stuff"
241 )]
243 {
244 [Option('a', "alpha", Required = true)]
245 public string Option { get; set; }
246 }
247
248 [Verb("delete", HelpText = "Delete stuff")]
249 public class VerbNoAlias
250 {
251 [Option('b', "beta", Required = true)]
252 public string Option { get; set; }
253 }
254 }
255}
Base type of all errors.
Definition Error.cs:110
Provides methods to parse command line arguments.
Definition Parser.cs:21
ParserResult< object > ParseArguments(IEnumerable< string > args, params Type[] types)
Parses a string array of command line arguments for verb commands scenario, constructing the proper i...
Definition Parser.cs:216
static Parser Default
Gets the singleton instance created with basic defaults.
Definition Parser.cs:75
void Parse_option_with_aliased_verbs(string args, Type expectedArgType)
Test Verb aliases when one verb is set as a default.
void Parse_help_option_for_aliased_verbs_no_default(string args, bool verbsIndex, string[] expected)
Verify auto-help generation with no default verb.
void Parse_option_with_aliased_verb(string args, Type expectedArgType)
Test verb aliases with no default verb and 1 verb with no aliases.
void Parse_help_option_for_aliased_verbs(string args, bool verbsIndex, string[] expected)
Verify auto-help generation.
override string ToString()
Returns the help screen as a System.String.
Definition HelpText.cs:830