@@ -183,44 +183,47 @@ public void Bool_does_not_parse_as_the_default_value_when_the_option_has_been_ap
183
183
[ Fact ]
184
184
public void By_default_an_option_with_zero_or_one_argument_parses_as_the_argument_string_value_by_default ( )
185
185
{
186
+ var option = new Option ( "-x" , arity : ArgumentArity . ZeroOrOne ) ;
186
187
var command = new Command ( "the-command" )
187
188
{
188
- new Option ( "-x" , arity : ArgumentArity . ZeroOrOne )
189
+ option
189
190
} ;
190
191
191
192
var result = command . Parse ( "the-command -x the-argument" ) ;
192
193
193
- result . ValueForOption ( "-x" )
194
+ result . ValueForOption ( option )
194
195
. Should ( )
195
196
. Be ( "the-argument" ) ;
196
197
}
197
198
198
199
[ Fact ]
199
200
public void By_default_an_option_with_exactly_one_argument_parses_as_the_argument_string_value_by_default ( )
200
201
{
202
+ var option = new Option ( "-x" , arity : ArgumentArity . ExactlyOne ) ;
201
203
var command = new Command ( "the-command" )
202
204
{
203
- new Option ( "-x" , arity : ArgumentArity . ExactlyOne )
205
+ option
204
206
} ;
205
207
206
208
var result = command . Parse ( "the-command -x the-argument" ) ;
207
209
208
- result . ValueForOption ( "-x" )
210
+ result . ValueForOption ( option )
209
211
. Should ( )
210
212
. Be ( "the-argument" ) ;
211
213
}
212
214
213
215
[ Fact ]
214
216
public void When_exactly_one_argument_is_expected_and_none_are_provided_then_getting_value_throws ( )
215
217
{
218
+ var option = new Option ( "-x" , arity : ArgumentArity . ExactlyOne ) ;
216
219
var command = new Command ( "the-command" )
217
220
{
218
- new Option ( "-x" , arity : ArgumentArity . ExactlyOne )
221
+ option
219
222
} ;
220
223
221
224
var result = command . Parse ( "the-command -x" ) ;
222
225
223
- Action getValue = ( ) => result . ValueForOption ( "-x" ) ;
226
+ Action getValue = ( ) => result . ValueForOption ( option ) ;
224
227
225
228
getValue . Should ( )
226
229
. Throw < InvalidOperationException > ( )
@@ -233,14 +236,15 @@ public void When_exactly_one_argument_is_expected_and_none_are_provided_then_get
233
236
[ Fact ]
234
237
public void When_zero_or_more_arguments_of_unspecified_type_are_expected_and_none_are_provided_then_getting_value_returns_an_empty_sequence_of_strings ( )
235
238
{
239
+ var option = new Option ( "-x" , arity : ArgumentArity . ZeroOrMore ) ;
236
240
var command = new Command ( "the-command" )
237
241
{
238
- new Option ( "-x" , arity : ArgumentArity . ZeroOrMore )
242
+ option
239
243
} ;
240
244
241
245
var result = command . Parse ( "the-command -x" ) ;
242
246
243
- result . ValueForOption ( "-x" )
247
+ result . ValueForOption ( option )
244
248
. Should ( )
245
249
. BeAssignableTo < IReadOnlyCollection < string > > ( )
246
250
. Which
@@ -253,15 +257,14 @@ public void
253
257
When_zero_or_more_arguments_of_unspecified_type_are_expected_and_none_are_provided_and_there_is_a_default_then_getting_value_returns_default_in_an_empty_sequence_of_strings ( )
254
258
{
255
259
var option = new Option ( "-x" , getDefaultValue : ( ) => "the-default" , arity : ArgumentArity . ZeroOrMore ) ;
256
-
257
260
var command = new Command ( "the-command" )
258
261
{
259
262
option
260
263
} ;
261
264
262
265
var result = command . Parse ( "the-command" ) ;
263
266
264
- result . ValueForOption ( "-x" )
267
+ result . ValueForOption ( option )
265
268
. Should ( )
266
269
. BeAssignableTo < IReadOnlyCollection < string > > ( )
267
270
. Which
@@ -272,14 +275,15 @@ public void
272
275
[ Fact ]
273
276
public void When_one_or_more_arguments_of_unspecified_type_are_expected_and_none_are_provided_then_getting_value_throws ( )
274
277
{
278
+ var option = new Option ( "-x" , arity : ArgumentArity . OneOrMore ) ;
275
279
var command = new Command ( "the-command" )
276
280
{
277
- new Option ( "-x" , arity : ArgumentArity . OneOrMore )
281
+ option
278
282
} ;
279
283
280
284
var result = command . Parse ( "the-command -x" ) ;
281
285
282
- Action getValue = ( ) => result . ValueForOption ( "-x" ) ;
286
+ Action getValue = ( ) => result . ValueForOption ( option ) ;
283
287
284
288
getValue . Should ( )
285
289
. Throw < InvalidOperationException > ( )
@@ -292,27 +296,29 @@ public void When_one_or_more_arguments_of_unspecified_type_are_expected_and_none
292
296
[ Fact ]
293
297
public void By_default_an_option_that_allows_multiple_arguments_and_is_passed_multiple_arguments_parses_as_a_sequence_of_strings ( )
294
298
{
299
+ var option = new Option ( "-x" , arity : ArgumentArity . ZeroOrMore ) ;
295
300
var command = new Command ( "the-command" )
296
301
{
297
- new Option ( "-x" , arity : ArgumentArity . ZeroOrMore )
302
+ option
298
303
} ;
299
304
300
305
command . Parse ( "the-command -x arg1 -x arg2" )
301
- . ValueForOption ( "-x" )
306
+ . ValueForOption ( option )
302
307
. Should ( )
303
308
. BeEquivalentTo ( new [ ] { "arg1" , "arg2" } ) ;
304
309
}
305
310
306
311
[ Fact ]
307
312
public void By_default_an_option_that_allows_multiple_arguments_and_is_passed_one_argument_parses_as_a_sequence_of_strings ( )
308
313
{
314
+ var option = new Option ( "-x" , arity : ArgumentArity . ZeroOrMore ) ;
309
315
var command = new Command ( "the-command" )
310
316
{
311
- new Option ( "-x" , arity : ArgumentArity . ZeroOrMore )
317
+ option
312
318
} ;
313
319
314
320
command . Parse ( "the-command -x arg1" )
315
- . ValueForOption ( "-x" )
321
+ . ValueForOption ( option )
316
322
. Should ( )
317
323
. BeEquivalentTo ( new [ ] { "arg1" } ) ;
318
324
}
@@ -323,18 +329,19 @@ public void By_default_an_option_that_allows_multiple_arguments_and_is_passed_on
323
329
[ InlineData ( "c c c" ) ]
324
330
public void When_command_argument_has_arity_greater_than_one_it_captures_arguments_before_and_after_option ( string commandLine )
325
331
{
332
+ var argument = new Argument < string [ ] > ( "the-arg" )
333
+ {
334
+ Arity = ArgumentArity . ZeroOrMore
335
+ } ;
326
336
var command = new Command ( "the-command" )
327
337
{
328
338
new Option < string > ( "-a" ) ,
329
- new Argument < string > ( "the-arg" )
330
- {
331
- Arity = ArgumentArity . ZeroOrMore
332
- }
339
+ argument
333
340
} ;
334
341
335
342
var result = command . Parse ( commandLine ) ;
336
343
337
- result . ValueForArgument ( "the-arg" )
344
+ result . ValueForArgument ( argument )
338
345
. Should ( )
339
346
. BeEquivalentTo ( new [ ] { "c" , "c" , "c" } ) ;
340
347
}
@@ -361,14 +368,15 @@ public void The_default_value_of_an_option_with_no_arguments_is_null()
361
368
[ Fact ]
362
369
public void By_default_an_option_without_arguments_parses_as_false_when_it_is_not_applied ( )
363
370
{
371
+ var option = new Option ( "-x" ) ;
364
372
var command = new Command ( "something" )
365
373
{
366
- new Option ( "-x" )
374
+ option
367
375
} ;
368
376
369
377
var result = command . Parse ( "something" ) ;
370
378
371
- result . ValueForOption < bool > ( "-x" )
379
+ result . ValueForOption < bool > ( option )
372
380
. Should ( )
373
381
. BeFalse ( ) ;
374
382
}
@@ -439,7 +447,7 @@ public void A_default_value_with_a_custom_constructor_can_be_specified_for_a_com
439
447
440
448
result . Errors . Should ( ) . BeEmpty ( ) ;
441
449
442
- var value = result . ValueForArgument ( "the-arg" ) ;
450
+ var value = result . ValueForArgument ( argument ) ;
443
451
444
452
value . Should ( ) . Be ( directoryInfo ) ;
445
453
}
@@ -464,14 +472,15 @@ public void An_option_argument_with_a_default_argument_can_be_converted_to_the_r
464
472
[ Fact ]
465
473
public void Specifying_an_option_argument_overrides_the_default_value ( )
466
474
{
475
+ var option = new Option < int > ( "-x" , ( ) => 123 ) ;
467
476
var command = new Command ( "something" )
468
477
{
469
- new Option < int > ( "-x" , ( ) => 123 )
478
+ option
470
479
} ;
471
480
472
481
var result = command . Parse ( "something -x 456" ) ;
473
482
474
- var value = result . ValueForOption < int > ( "-x" ) ;
483
+ var value = result . ValueForOption ( option ) ;
475
484
476
485
value . Should ( ) . Be ( 456 ) ;
477
486
}
@@ -491,7 +500,7 @@ public void Values_can_be_correctly_converted_to_nullable_int_with_no_value_with
491
500
{
492
501
var option = new Option ( "-x" , arity : ArgumentArity . ZeroOrOne ) ;
493
502
494
- var value = option . Parse ( "" ) . ValueForOption < int ? > ( "-x" ) ;
503
+ var value = option . Parse ( "" ) . ValueForOption < int ? > ( option ) ;
495
504
496
505
value . Should ( ) . BeNull ( ) ;
497
506
}
@@ -521,7 +530,7 @@ public void Values_can_be_correctly_converted_to_double_without_the_parser_speci
521
530
{
522
531
var option = new Option ( "-x" , arity : ArgumentArity . ZeroOrOne ) ;
523
532
524
- var value = option . Parse ( "-x 123.456" ) . ValueForOption < double > ( "-x" ) ;
533
+ var value = option . Parse ( "-x 123.456" ) . ValueForOption < double > ( option ) ;
525
534
526
535
value . Should ( ) . Be ( 123.456d ) ;
527
536
}
@@ -531,7 +540,7 @@ public void Values_can_be_correctly_converted_to_float_without_the_parser_specif
531
540
{
532
541
var option = new Option ( "-x" , arity : ArgumentArity . ZeroOrOne ) ;
533
542
534
- var value = option . Parse ( "-x 123.456" ) . ValueForOption < float > ( "-x" ) ;
543
+ var value = option . Parse ( "-x 123.456" ) . ValueForOption < float > ( option ) ;
535
544
536
545
value . Should ( ) . Be ( 123.456f ) ;
537
546
}
@@ -541,24 +550,24 @@ public void Options_with_no_arguments_specified_can_be_correctly_converted_to_bo
541
550
{
542
551
var option = new Option ( "-x" , arity : ArgumentArity . ZeroOrOne ) ;
543
552
544
- option . Parse ( "-x" ) . ValueForOption < bool > ( "-x" ) . Should ( ) . BeTrue ( ) ;
553
+ option . Parse ( "-x" ) . ValueForOption < bool > ( option ) . Should ( ) . BeTrue ( ) ;
545
554
}
546
555
547
556
[ Fact ]
548
557
public void Options_with_arguments_specified_can_be_correctly_converted_to_bool_without_the_parser_specifying_a_custom_converter ( )
549
558
{
550
559
var option = new Option ( "-x" , arity : ArgumentArity . ZeroOrOne ) ;
551
560
552
- option . Parse ( "-x false" ) . ValueForOption < bool > ( "-x" ) . Should ( ) . BeFalse ( ) ;
553
- option . Parse ( "-x true" ) . ValueForOption < bool > ( "-x" ) . Should ( ) . BeTrue ( ) ;
561
+ option . Parse ( "-x false" ) . ValueForOption < bool > ( option ) . Should ( ) . BeFalse ( ) ;
562
+ option . Parse ( "-x true" ) . ValueForOption < bool > ( option ) . Should ( ) . BeTrue ( ) ;
554
563
}
555
564
556
565
[ Fact ]
557
566
public void Values_can_be_correctly_converted_to_array_of_int_without_the_parser_specifying_a_custom_converter ( )
558
567
{
559
568
var option = new Option ( "-x" , arity : ArgumentArity . ZeroOrMore ) ;
560
569
561
- var value = option . Parse ( "-x 1 -x 2 -x 3" ) . ValueForOption < int [ ] > ( "-x" ) ;
570
+ var value = option . Parse ( "-x 1 -x 2 -x 3" ) . ValueForOption < int [ ] > ( option ) ;
562
571
563
572
value . Should ( ) . BeEquivalentTo ( 1 , 2 , 3 ) ;
564
573
}
@@ -608,7 +617,7 @@ public void Values_can_be_correctly_converted_to_List_of_int_without_the_parser_
608
617
{
609
618
var option = new Option ( "-x" , arity : ArgumentArity . ZeroOrMore ) ;
610
619
611
- var value = option . Parse ( "-x 1 -x 2 -x 3" ) . ValueForOption < List < int > > ( "-x" ) ;
620
+ var value = option . Parse ( "-x 1 -x 2 -x 3" ) . ValueForOption < List < int > > ( option ) ;
612
621
613
622
value . Should ( ) . BeEquivalentTo ( 1 , 2 , 3 ) ;
614
623
}
@@ -618,7 +627,7 @@ public void Values_can_be_correctly_converted_to_IEnumerable_of_int_without_the_
618
627
{
619
628
var option = new Option ( "-x" , arity : ArgumentArity . ZeroOrMore ) ;
620
629
621
- var value = option . Parse ( "-x 1 -x 2 -x 3" ) . ValueForOption < IEnumerable < int > > ( "-x" ) ;
630
+ var value = option . Parse ( "-x 1 -x 2 -x 3" ) . ValueForOption < IEnumerable < int > > ( option ) ;
622
631
623
632
value . Should ( ) . BeEquivalentTo ( 1 , 2 , 3 ) ;
624
633
}
@@ -630,7 +639,7 @@ public void Enum_values_can_be_correctly_converted_based_on_enum_value_name_with
630
639
631
640
var parseResult = option . Parse ( "-x Monday" ) ;
632
641
633
- var value = parseResult . ValueForOption < DayOfWeek > ( "-x" ) ;
642
+ var value = parseResult . ValueForOption < DayOfWeek > ( option ) ;
634
643
635
644
value . Should ( ) . Be ( DayOfWeek . Monday ) ;
636
645
}
@@ -655,7 +664,7 @@ public void When_getting_values_and_specifying_a_conversion_type_that_is_not_sup
655
664
656
665
var result = option . Parse ( "-x not-an-int" ) ;
657
666
658
- Action getValue = ( ) => result . ValueForOption < int > ( "-x" ) ;
667
+ Action getValue = ( ) => result . ValueForOption < int > ( option ) ;
659
668
660
669
getValue . Should ( )
661
670
. Throw < InvalidOperationException > ( )
@@ -672,7 +681,7 @@ public void When_getting_an_array_of_values_and_specifying_a_conversion_type_tha
672
681
673
682
var result = option . Parse ( "-x not-an-int -x 2" ) ;
674
683
675
- Action getValue = ( ) => result . ValueForOption < int [ ] > ( "-x" ) ;
684
+ Action getValue = ( ) => result . ValueForOption < int [ ] > ( option ) ;
676
685
677
686
getValue . Should ( )
678
687
. Throw < InvalidOperationException > ( )
0 commit comments