@@ -30,6 +30,21 @@ public override void OnConfigure(ICommandConfigurationBuilder<object> builder)
30
30
}
31
31
}
32
32
33
+ [ Theory ]
34
+ [ InlineData ( "" , "--" ) ]
35
+ [ InlineData ( "-" , "" ) ]
36
+ [ InlineData ( "---" , "---" ) ]
37
+ public void InvalidOptionsThrowException ( string shortOption , string longOption )
38
+ {
39
+ var options = new CommandLineParserOptions
40
+ {
41
+ PrefixShortOption = shortOption ,
42
+ PrefixLongOption = longOption
43
+ } ;
44
+
45
+ Assert . Throws < ArgumentException > ( ( ) => new CommandLineParser ( options , Services ) ) ;
46
+ }
47
+
33
48
[ Theory ]
34
49
[ InlineData ( true ) ]
35
50
[ InlineData ( false ) ]
@@ -53,7 +68,7 @@ public void CommandLineParserUsesCorrectOptions()
53
68
{
54
69
var opt = new CommandLineParserOptions ( ) ;
55
70
56
- var parser = new CommandLineParser ( opt ) ;
71
+ var parser = new CommandLineParser ( opt , Services ) ;
57
72
58
73
Assert . Equal ( opt , parser . ParserOptions ) ;
59
74
}
@@ -131,7 +146,7 @@ public async Task CommandLineParserUsesContainerCorrectlyAsync(bool generic)
131
146
[ Fact ]
132
147
public void AutoExecuteCommandsWithExceptionDoesntCrashTheParser ( )
133
148
{
134
- var parser = new CommandLineParser ( ) ;
149
+ var parser = new CommandLineParser ( Services ) ;
135
150
136
151
var ex = new Exception ( "uh-oh" ) ;
137
152
@@ -151,7 +166,7 @@ public void AutoExecuteCommandsWithExceptionDoesntCrashTheParser()
151
166
[ Fact ]
152
167
public async Task AutoExecuteCommandsWithExceptionDoesntCrashTheParserAsync ( )
153
168
{
154
- var parser = new CommandLineParser ( ) ;
169
+ var parser = new CommandLineParser ( Services ) ;
155
170
156
171
var ex = new Exception ( "uh-oh" ) ;
157
172
@@ -172,32 +187,10 @@ public async Task AutoExecuteCommandsWithExceptionDoesntCrashTheParserAsync()
172
187
Assert . Equal ( ex , result . Errors . First ( ) . GetBaseException ( ) ) ;
173
188
}
174
189
175
- //[Fact]
176
- //public void CommandLineParserUsesArgumentFactoryCorrectly()
177
- //{
178
- // var resolverMock = new Mock<IArgumentResolver<string>>();
179
- // resolverMock.Setup(_ => _.CanResolve(It.IsAny<ArgumentModel>())).Returns(true).Verifiable();
180
- // resolverMock.Setup(_ => _.Resolve(It.IsAny<ArgumentModel>())).Returns("return").Verifiable();
181
-
182
- // var argResolverFactory = new Mock<IServiceProvider>();
183
- // argResolverFactory.Setup(c => c.GetService(typeof(string))).Returns(resolverMock.Object).Verifiable();
184
-
185
- // var parser = new CommandLineParser<AddOption>(.Object);
186
-
187
- // parser.Configure(p => p.Message).Name("m");
188
-
189
- // var result = parser.Parse(new[] { "app.exe", "-m" });
190
-
191
- // result.AssertNoErrors();
192
-
193
- // resolverMock.VerifyAll();
194
- // argResolverFactory.Verify();
195
- //}
196
-
197
190
[ Fact ]
198
191
public void ParseTests ( )
199
192
{
200
- var parser = new CommandLineParser < Options > ( ) ;
193
+ var parser = new CommandLineParser < Options > ( Services ) ;
201
194
202
195
parser . Configure ( opt => opt . Option1 )
203
196
. Name ( "o" )
@@ -221,7 +214,7 @@ public void ParseTests()
221
214
[ InlineData ( new [ ] { "app.exe" , "-e" } , true , default ( EnumOption ) ) ]
222
215
public void ParseEnumInArguments ( string [ ] args , bool hasErrors , EnumOption enumOption )
223
216
{
224
- var parser = new CommandLineParser < EnumOptions > ( ) ;
217
+ var parser = new CommandLineParser < EnumOptions > ( Services ) ;
225
218
226
219
parser . Configure ( opt => opt . EnumOption )
227
220
. Name ( "e" )
@@ -286,7 +279,7 @@ void Test<T>()
286
279
287
280
private void TestParsingWithDefaults < T > ( string [ ] args , T defaultValue , T result1 , T result2 , T result3 )
288
281
{
289
- var parser = new CommandLineParser < OptionsWithThreeParams < T > > ( ) ;
282
+ var parser = new CommandLineParser < OptionsWithThreeParams < T > > ( Services ) ;
290
283
291
284
parser . Configure ( opt => opt . Option1 )
292
285
. Name ( "1" )
@@ -312,48 +305,12 @@ private void TestParsingWithDefaults<T>(string[] args, T defaultValue, T result1
312
305
Assert . Equal ( result3 , parsed . Result . Option3 ) ;
313
306
}
314
307
315
- [ Fact ]
316
- public void ParseWithCommandTests ( )
317
- {
318
- var wait = new ManualResetEvent ( false ) ;
319
-
320
- var parser = new CommandLineParser < Options > ( ) ;
321
-
322
- parser . Configure ( opt => opt . Option1 )
323
- . Name ( "o" )
324
- . Default ( "Default message" )
325
- . Required ( ) ;
326
-
327
- var addCmd = parser . AddCommand < AddOption > ( )
328
- . Name ( "add" )
329
- . OnExecuting ( ( opt , cmdOpt ) =>
330
- {
331
- Assert . Equal ( "test" , opt . Option1 ) ;
332
- Assert . Equal ( "my message" , cmdOpt . Message ) ;
333
- wait . Set ( ) ;
334
- } ) ;
335
-
336
- addCmd . Configure ( opt => opt . Message )
337
- . Name ( "m" , "message" )
338
- . Required ( ) ;
339
-
340
- var parsed = parser . Parse ( new string [ ] { "app.exe" , "-o" , "test" , "add" , "-m=my message" } ) ;
341
-
342
- parsed . AssertNoErrors ( ) ;
343
-
344
- Assert . Equal ( "test" , parsed . Result . Option1 ) ;
345
-
346
- parsed . ExecuteCommands ( ) ;
347
-
348
- Assert . True ( wait . WaitOne ( 2000 ) ) ;
349
- }
350
-
351
308
[ Fact ]
352
309
public async Task ParseWithCommandTestsAsync ( )
353
310
{
354
311
var wait = new ManualResetEvent ( false ) ;
355
312
356
- var parser = new CommandLineParser < Options > ( ) ;
313
+ var parser = new CommandLineParser < Options > ( Services ) ;
357
314
358
315
parser . Configure ( opt => opt . Option1 )
359
316
. Name ( "o" )
@@ -386,7 +343,7 @@ public async Task ParseWithCommandTestsAsync()
386
343
387
344
Assert . Equal ( "test" , parsed . Result . Option1 ) ;
388
345
389
- parsed . ExecuteCommands ( ) ;
346
+ parsed . ExecuteCommandsAsync ( default ) . GetAwaiter ( ) . GetResult ( ) ;
390
347
391
348
Assert . True ( wait . WaitOne ( 2000 ) ) ;
392
349
}
@@ -398,7 +355,7 @@ public async Task ParseWithCommandTestsAsync()
398
355
[ InlineData ( new [ ] { "-m" , "message1" , "add" , "-m" , "message2" } , "message1" , "message2" ) ]
399
356
public void ParseCommandTests ( string [ ] args , string result1 , string result2 )
400
357
{
401
- var parser = new CommandLineParser < AddOption > ( ) ;
358
+ var parser = new CommandLineParser < AddOption > ( Services ) ;
402
359
var wait = new ManualResetEvent ( false ) ;
403
360
404
361
parser . AddCommand < AddOption > ( )
@@ -434,7 +391,7 @@ public void ParseCommandTests(string[] args, string result1, string result2)
434
391
[ InlineData ( new [ ] { "-m" , "message1" , "add" , "-m" , "message2" } , "message1" , "message2" ) ]
435
392
public async Task ParseCommandTestsAsync ( string [ ] args , string result1 , string result2 )
436
393
{
437
- var parser = new CommandLineParser < AddOption > ( ) ;
394
+ var parser = new CommandLineParser < AddOption > ( Services ) ;
438
395
var wait = new ManualResetEvent ( false ) ;
439
396
440
397
parser . AddCommand < AddOption > ( )
@@ -473,7 +430,7 @@ public async Task ParseCommandTestsAsync(string[] args, string result1, string r
473
430
[ InlineData ( new string [ ] { "-x" , "false" } , false ) ]
474
431
public void BoolResolverSpecialCaseParsesCorrectly ( string [ ] args , bool expected )
475
432
{
476
- var parser = new CommandLineParser < Options > ( ) ;
433
+ var parser = new CommandLineParser < Options > ( Services ) ;
477
434
478
435
parser . Configure ( opt => opt . Option2 )
479
436
. Name ( "x" , "xsomething" )
@@ -493,7 +450,7 @@ public void BoolResolverSpecialCaseParsesCorrectly(string[] args, bool expected)
493
450
[ InlineData ( new string [ ] { "command" , "-v" } , true ) ]
494
451
public void BoolResolverSpecialCaseParsesCorrectlyWithDefaultValueAndNotBeingSpecified ( string [ ] args , bool expected )
495
452
{
496
- var parser = new CommandLineParser < Model_Issue_35 > ( ) ;
453
+ var parser = new CommandLineParser < Model_Issue_35 > ( Services ) ;
497
454
498
455
parser . AddCommand ( ) . Name ( "command" ) ;
499
456
@@ -515,7 +472,7 @@ private class Model_Issue_35
515
472
[ Fact ]
516
473
public void ConfigureTests ( )
517
474
{
518
- var parser = new CommandLineParser < Options > ( ) ;
475
+ var parser = new CommandLineParser < Options > ( Services ) ;
519
476
520
477
parser . Configure ( opt => opt . Option1 )
521
478
. Name ( "o" , "opt" )
@@ -549,7 +506,7 @@ public void ConfigureTests()
549
506
[ InlineData ( new string [ ] { "--message" , "test" } , "testtransformed" , false ) ]
550
507
public void TransformationWorksAsExpected ( string [ ] args , string expected , bool errors )
551
508
{
552
- var parser = new CommandLineParser < AddOption > ( ) ;
509
+ var parser = new CommandLineParser < AddOption > ( Services ) ;
553
510
554
511
parser . Configure ( a => a . Message )
555
512
. Name ( "m" , "message" )
@@ -570,7 +527,7 @@ public void TransformationWorksAsExpected(string[] args, string expected, bool e
570
527
[ InlineData ( new string [ ] { "--int" , "10" } , 20 , false ) ]
571
528
public void TransformationWorksAsExpectedForInts ( string [ ] args , int expected , bool errors )
572
529
{
573
- var parser = new CommandLineParser < IntOptions > ( ) ;
530
+ var parser = new CommandLineParser < IntOptions > ( Services ) ;
574
531
575
532
parser . Configure ( a => a . SomeInt )
576
533
. Name ( "i" , "int" )
@@ -593,7 +550,7 @@ public void TransformationWorksAsExpectedForCommandOptions(string[] args, int ex
593
550
{
594
551
int outcome = - 1 ;
595
552
596
- var parser = new CommandLineParser ( ) ;
553
+ var parser = new CommandLineParser ( Services ) ;
597
554
598
555
var cmd = parser . AddCommand < IntOptions > ( )
599
556
. Name ( "cmd" )
@@ -620,7 +577,7 @@ public void TransformationWorksAsExpectedForCommandOptions(string[] args, int ex
620
577
[ InlineData ( new string [ ] { "cmd" , "--string" , "test" , "-s2" , "test" } , "test" , false ) ]
621
578
public void CustomTypeWithStringTryParseGetsParsedCorrectly ( string [ ] args , string expected , bool errors )
622
579
{
623
- var parser = new CommandLineParser < StringTryParseTypeOptions > ( ) ;
580
+ var parser = new CommandLineParser < StringTryParseTypeOptions > ( Services ) ;
624
581
625
582
var result = parser . Parse ( args ) ;
626
583
@@ -640,7 +597,7 @@ public void CustomTypeWithStringTryParseGetsParsedCorrectly(string[] args, strin
640
597
[ InlineData ( new string [ ] { "cmd" , "--string" , "test" , "-s2" , "test" , "-s3" , "test" } , "test" , false ) ]
641
598
public void CustomTypeWithStringConstructorGetsParsedCorrectly ( string [ ] args , string expected , bool errors )
642
599
{
643
- var parser = new CommandLineParser < StringTypeOptions > ( ) ;
600
+ var parser = new CommandLineParser < StringTypeOptions > ( Services ) ;
644
601
645
602
var result = parser . Parse ( args ) ;
646
603
0 commit comments