BadScript 2
Loading...
Searching...
No Matches
GetoptParserTests.cs
Go to the documentation of this file.
1using System;
2using Xunit;
3using FluentAssertions;
6using System.IO;
7using System.Linq;
8using System.Collections.Generic;
9
11{
12 public class GetoptParserTests
13 {
15 {
16 }
17
18 public class SimpleArgsData : TheoryData<string[], Simple_Options_WithExtraArgs>
19 {
21 {
22 // Options and values can be mixed by default
23 Add(new string [] { "--stringvalue=foo", "-x", "256" },
25 IntSequence = Enumerable.Empty<int>(),
26 ShortAndLong = null,
27 StringValue = "foo",
28 BoolValue = true,
29 LongValue = 256,
30 ExtraArgs = Enumerable.Empty<string>(),
31 });
32 Add(new string [] { "256", "--stringvalue=foo", "-x" },
34 StringValue = "foo",
35 ShortAndLong = null,
36 IntSequence = Enumerable.Empty<int>(),
37 BoolValue = true,
38 LongValue = 256,
39 ExtraArgs = Enumerable.Empty<string>(),
40 });
41 Add(new string [] {"--stringvalue=foo", "256", "-x" },
43 StringValue = "foo",
44 ShortAndLong = null,
45 IntSequence = Enumerable.Empty<int>(),
46 BoolValue = true,
47 LongValue = 256,
48 ExtraArgs = Enumerable.Empty<string>(),
49 });
50
51 // Sequences end at first non-value arg even if they haven't yet consumed their max
52 Add(new string [] {"--stringvalue=foo", "-i1", "2", "3", "-x", "256" },
54 StringValue = "foo",
55 ShortAndLong = null,
56 IntSequence = new[] { 1, 2, 3 },
57 BoolValue = true,
58 LongValue = 256,
59 ExtraArgs = Enumerable.Empty<string>(),
60 });
61 // Sequences also end after consuming their max even if there would be more parameters
62 Add(new string [] {"--stringvalue=foo", "-i1", "2", "3", "4", "256", "-x" },
64 StringValue = "foo",
65 ShortAndLong = null,
66 IntSequence = new[] { 1, 2, 3, 4 },
67 BoolValue = true,
68 LongValue = 256,
69 ExtraArgs = Enumerable.Empty<string>(),
70 });
71
72 // The special -- option, if not consumed, turns off further option processing
73 Add(new string [] {"--stringvalue", "foo", "256", "-x", "-sbar" },
75 StringValue = "foo",
76 ShortAndLong = "bar",
77 BoolValue = true,
78 LongValue = 256,
79 IntSequence = Enumerable.Empty<int>(),
80 ExtraArgs = Enumerable.Empty<string>(),
81 });
82 Add(new string [] {"--stringvalue", "foo", "--", "256", "-x", "-sbar" },
84 StringValue = "foo",
85 ShortAndLong = null,
86 BoolValue = false,
87 LongValue = 256,
88 IntSequence = Enumerable.Empty<int>(),
89 ExtraArgs = new [] { "-x", "-sbar" },
90 });
91
92 // But if -- is specified as a value following an equal sign, it has no special meaning
93 Add(new string [] {"--stringvalue=--", "256", "-x", "-sbar" },
95 StringValue = "--",
96 ShortAndLong = "bar",
97 BoolValue = true,
98 LongValue = 256,
99 IntSequence = Enumerable.Empty<int>(),
100 ExtraArgs = Enumerable.Empty<string>(),
101 });
102
103 // Options that take values will take the next arg whatever it looks like
104 Add(new string [] {"--stringvalue", "-x", "256" },
106 StringValue = "-x",
107 BoolValue = false,
108 LongValue = 256,
109 IntSequence = Enumerable.Empty<int>(),
110 ExtraArgs = Enumerable.Empty<string>(),
111 });
112 Add(new string [] {"--stringvalue", "-x", "-x", "256" },
114 StringValue = "-x",
115 BoolValue = true,
116 LongValue = 256,
117 IntSequence = Enumerable.Empty<int>(),
118 ExtraArgs = Enumerable.Empty<string>(),
119 });
120
121 // That applies even if the next arg is -- which would normally stop option processing: if it's after an option that takes a value, it's consumed as the value
122 Add(new string [] {"--stringvalue", "--", "256", "-x", "-sbar" },
124 StringValue = "--",
125 ShortAndLong = "bar",
126 BoolValue = true,
127 LongValue = 256,
128 IntSequence = Enumerable.Empty<int>(),
129 ExtraArgs = Enumerable.Empty<string>(),
130 });
131
132 // Options that take values will not swallow the next arg if a value was specified with =
133 Add(new string [] {"--stringvalue=-x", "256" },
135 StringValue = "-x",
136 BoolValue = false,
137 LongValue = 256,
138 IntSequence = Enumerable.Empty<int>(),
139 ExtraArgs = Enumerable.Empty<string>(),
140 });
141 Add(new string [] {"--stringvalue=-x", "-x", "256" },
143 StringValue = "-x",
144 BoolValue = true,
145 LongValue = 256,
146 IntSequence = Enumerable.Empty<int>(),
147 ExtraArgs = Enumerable.Empty<string>(),
148 });
149 }
150 }
151
152 [Theory]
153 [ClassData(typeof(SimpleArgsData))]
155 {
156 // Arrange
157 var sut = new Parser(config => {
158 config.GetoptMode = true;
159 config.PosixlyCorrect = false;
160 });
161
162 // Act
163 var result = sut.ParseArguments<Simple_Options_WithExtraArgs>(args);
164
165 // Assert
166 if (result is Parsed<Simple_Options_WithExtraArgs> parsed) {
167 parsed.Value.Should().BeEquivalentTo(expected);
168 } else if (result is NotParsed<Simple_Options_WithExtraArgs> notParsed) {
169 Console.WriteLine(String.Join(", ", notParsed.Errors.Select(err => err.Tag.ToString())));
170 }
171 result.Should().BeOfType<Parsed<Simple_Options_WithExtraArgs>>();
172 result.As<Parsed<Simple_Options_WithExtraArgs>>().Value.Should().BeEquivalentTo(expected);
173 }
174
175 public class SimpleArgsDataWithPosixlyCorrect : TheoryData<string[], Simple_Options_WithExtraArgs>
176 {
178 {
179 Add(new string [] { "--stringvalue=foo", "-x", "256" },
180 // Parses all options
182 StringValue = "foo",
183 ShortAndLong = null,
184 IntSequence = Enumerable.Empty<int>(),
185 BoolValue = true,
186 LongValue = 256,
187 ExtraArgs = Enumerable.Empty<string>(),
188 });
189 Add(new string [] { "256", "--stringvalue=foo", "-x" },
190 // Stops parsing after "256", so StringValue and BoolValue not set
192 StringValue = null,
193 ShortAndLong = null,
194 IntSequence = Enumerable.Empty<int>(),
195 BoolValue = false,
196 LongValue = 256,
197 ExtraArgs = new string[] { "--stringvalue=foo", "-x" },
198 });
199 Add(new string [] {"--stringvalue=foo", "256", "-x" },
200 // Stops parsing after "256", so StringValue is set but BoolValue is not
202 StringValue = "foo",
203 ShortAndLong = null,
204 IntSequence = Enumerable.Empty<int>(),
205 BoolValue = false,
206 LongValue = 256,
207 ExtraArgs = new string[] { "-x" },
208 });
209 }
210 }
211
212 [Theory]
213 [ClassData(typeof(SimpleArgsDataWithPosixlyCorrect))]
215 {
216 // Arrange
217 var sut = new Parser(config => {
218 config.GetoptMode = true;
219 config.PosixlyCorrect = true;
220 config.EnableDashDash = true;
221 });
222
223 // Act
224 var result = sut.ParseArguments<Simple_Options_WithExtraArgs>(args);
225
226 // Assert
227 result.Should().BeOfType<Parsed<Simple_Options_WithExtraArgs>>();
228 result.As<Parsed<Simple_Options_WithExtraArgs>>().Value.Should().BeEquivalentTo(expected);
229 }
230
231 [Fact]
233 {
234 // Arrange
235 var sut = new Parser(config => {
236 config.GetoptMode = true;
237 config.PosixlyCorrect = false;
238 });
239 var args = new string [] {"--stringvalue", "foo", "256", "--", "-x", "-sbar" };
240 var expected = new Simple_Options_WithExtraArgs {
241 StringValue = "foo",
242 ShortAndLong = null,
243 BoolValue = false,
244 LongValue = 256,
245 IntSequence = Enumerable.Empty<int>(),
246 ExtraArgs = new [] { "-x", "-sbar" },
247 };
248
249 // Act
250 var result = sut.ParseArguments<Simple_Options_WithExtraArgs>(args);
251
252 // Assert
253 result.Should().BeOfType<Parsed<Simple_Options_WithExtraArgs>>();
254 result.As<Parsed<Simple_Options_WithExtraArgs>>().Value.Should().BeEquivalentTo(expected);
255 }
256
257 [Fact]
259 {
260 // Arrange
261 var sut = new Parser(config => {
262 config.GetoptMode = true;
263 config.PosixlyCorrect = false;
264 config.EnableDashDash = false;
265 });
266 var args = new string [] {"--stringvalue", "foo", "256", "--", "-x", "-sbar" };
267 var expected = new Simple_Options_WithExtraArgs {
268 StringValue = "foo",
269 ShortAndLong = "bar",
270 BoolValue = true,
271 LongValue = 256,
272 IntSequence = Enumerable.Empty<int>(),
273 ExtraArgs = new [] { "--" },
274 };
275
276 // Act
277 var result = sut.ParseArguments<Simple_Options_WithExtraArgs>(args);
278
279 // Assert
280 result.Should().BeOfType<Parsed<Simple_Options_WithExtraArgs>>();
281 result.As<Parsed<Simple_Options_WithExtraArgs>>().Value.Should().BeEquivalentTo(expected);
282 }
283 }
284}
It contains a sequence of CommandLine.Error.
It contains an instance of type T with parsed values.
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
void Getopt_parser_without_posixly_correct_allows_mixed_options_and_nonoptions(string[] args, Simple_Options_WithExtraArgs expected)
void Getopt_parser_with_posixly_correct_stops_parsing_at_first_nonoption(string[] args, Simple_Options_WithExtraArgs expected)