@@ -182,46 +182,52 @@ public void Bool_does_not_parse_as_the_default_value_when_the_option_has_been_ap
182
182
}
183
183
184
184
[ Fact ]
185
- public void By_default_an_option_with_zero_or_one_argument_parses_as_the_argument_string_value_by_default ( )
185
+ public void By_default_an_option_with_zero_or_one_argument_parses_as_the_argument_string_value ( )
186
186
{
187
+ var option = new Option ( "-x" , arity : ArgumentArity . ZeroOrOne ) ;
188
+
187
189
var command = new Command ( "the-command" )
188
190
{
189
- new Option ( "-x" , arity : ArgumentArity . ZeroOrOne )
191
+ option
190
192
} ;
191
193
192
194
var result = command . Parse ( "the-command -x the-argument" ) ;
193
195
194
- result . ValueForOption ( "-x" )
196
+ result . ValueForOption ( option )
195
197
. Should ( )
196
198
. Be ( "the-argument" ) ;
197
199
}
198
200
199
201
[ Fact ]
200
- public void By_default_an_option_with_exactly_one_argument_parses_as_the_argument_string_value_by_default ( )
202
+ public void By_default_an_option_with_exactly_one_argument_parses_as_the_argument_string_value ( )
201
203
{
204
+ var option = new Option ( "-x" , arity : ArgumentArity . ExactlyOne ) ;
205
+
202
206
var command = new Command ( "the-command" )
203
207
{
204
- new Option ( "-x" , arity : ArgumentArity . ExactlyOne )
208
+ option
205
209
} ;
206
210
207
211
var result = command . Parse ( "the-command -x the-argument" ) ;
208
212
209
- result . ValueForOption ( "-x" )
213
+ result . ValueForOption ( option )
210
214
. Should ( )
211
215
. Be ( "the-argument" ) ;
212
216
}
213
217
214
218
[ Fact ]
215
219
public void When_exactly_one_argument_is_expected_and_none_are_provided_then_getting_value_throws ( )
216
220
{
221
+ var option = new Option ( "-x" , arity : ArgumentArity . ExactlyOne ) ;
222
+
217
223
var command = new Command ( "the-command" )
218
224
{
219
- new Option ( "-x" , arity : ArgumentArity . ExactlyOne )
225
+ option
220
226
} ;
221
227
222
228
var result = command . Parse ( "the-command -x" ) ;
223
229
224
- Action getValue = ( ) => result . ValueForOption ( "-x" ) ;
230
+ Action getValue = ( ) => result . ValueForOption ( option ) ;
225
231
226
232
getValue . Should ( )
227
233
. Throw < InvalidOperationException > ( )
@@ -234,14 +240,16 @@ public void When_exactly_one_argument_is_expected_and_none_are_provided_then_get
234
240
[ Fact ]
235
241
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 ( )
236
242
{
243
+ var option = new Option ( "-x" , arity : ArgumentArity . ZeroOrMore ) ;
244
+
237
245
var command = new Command ( "the-command" )
238
246
{
239
- new Option ( "-x" , arity : ArgumentArity . ZeroOrMore )
247
+ option
240
248
} ;
241
249
242
250
var result = command . Parse ( "the-command -x" ) ;
243
251
244
- result . ValueForOption ( "-x" )
252
+ result . ValueForOption ( option )
245
253
. Should ( )
246
254
. BeAssignableTo < IReadOnlyCollection < string > > ( )
247
255
. Which
@@ -262,7 +270,7 @@ public void
262
270
263
271
var result = command . Parse ( "the-command" ) ;
264
272
265
- result . ValueForOption ( "-x" )
273
+ result . ValueForOption ( option )
266
274
. Should ( )
267
275
. BeAssignableTo < IReadOnlyCollection < string > > ( )
268
276
. Which
@@ -273,14 +281,16 @@ public void
273
281
[ Fact ]
274
282
public void When_one_or_more_arguments_of_unspecified_type_are_expected_and_none_are_provided_then_getting_value_throws ( )
275
283
{
284
+ var option = new Option ( "-x" , arity : ArgumentArity . OneOrMore ) ;
285
+
276
286
var command = new Command ( "the-command" )
277
287
{
278
- new Option ( "-x" , arity : ArgumentArity . OneOrMore )
288
+ option
279
289
} ;
280
290
281
291
var result = command . Parse ( "the-command -x" ) ;
282
292
283
- Action getValue = ( ) => result . ValueForOption ( "-x" ) ;
293
+ Action getValue = ( ) => result . ValueForOption ( option ) ;
284
294
285
295
getValue . Should ( )
286
296
. Throw < InvalidOperationException > ( )
@@ -293,27 +303,31 @@ public void When_one_or_more_arguments_of_unspecified_type_are_expected_and_none
293
303
[ Fact ]
294
304
public void By_default_an_option_that_allows_multiple_arguments_and_is_passed_multiple_arguments_parses_as_a_sequence_of_strings ( )
295
305
{
306
+ var option = new Option ( "-x" , arity : ArgumentArity . ZeroOrMore ) ;
307
+
296
308
var command = new Command ( "the-command" )
297
309
{
298
- new Option ( "-x" , arity : ArgumentArity . ZeroOrMore )
310
+ option
299
311
} ;
300
312
301
313
command . Parse ( "the-command -x arg1 -x arg2" )
302
- . ValueForOption ( "-x" )
314
+ . ValueForOption ( option )
303
315
. Should ( )
304
316
. BeEquivalentTo ( new [ ] { "arg1" , "arg2" } ) ;
305
317
}
306
318
307
319
[ Fact ]
308
320
public void By_default_an_option_that_allows_multiple_arguments_and_is_passed_one_argument_parses_as_a_sequence_of_strings ( )
309
321
{
322
+ var option = new Option ( "-x" , arity : ArgumentArity . ZeroOrMore ) ;
323
+
310
324
var command = new Command ( "the-command" )
311
325
{
312
- new Option ( "-x" , arity : ArgumentArity . ZeroOrMore )
326
+ option
313
327
} ;
314
328
315
329
command . Parse ( "the-command -x arg1" )
316
- . ValueForOption ( "-x" )
330
+ . ValueForOption ( option )
317
331
. Should ( )
318
332
. BeEquivalentTo ( new [ ] { "arg1" } ) ;
319
333
}
@@ -324,24 +338,26 @@ public void By_default_an_option_that_allows_multiple_arguments_and_is_passed_on
324
338
[ InlineData ( "c c c" ) ]
325
339
public void When_command_argument_has_arity_greater_than_one_it_captures_arguments_before_and_after_option ( string commandLine )
326
340
{
341
+ var argument = new Argument < string [ ] > ( "the-arg" )
342
+ {
343
+ Arity = ArgumentArity . ZeroOrMore
344
+ } ;
345
+
327
346
var command = new Command ( "the-command" )
328
347
{
329
348
new Option < string > ( "-a" ) ,
330
- new Argument < string > ( "the-arg" )
331
- {
332
- Arity = ArgumentArity . ZeroOrMore
333
- }
349
+ argument
334
350
} ;
335
351
336
352
var result = command . Parse ( commandLine ) ;
337
353
338
- result . ValueForArgument ( "the-arg" )
354
+ result . ValueForArgument ( argument )
339
355
. Should ( )
340
356
. BeEquivalentTo ( new [ ] { "c" , "c" , "c" } ) ;
341
357
}
342
358
343
359
[ Fact ]
344
- public void The_default_value_of_an_option_with_no_arguments_is_false ( )
360
+ public void The_default_value_of_an_option_with_no_arguments_is_true ( )
345
361
{
346
362
var option = new Option ( "-x" ) ;
347
363
@@ -353,8 +369,7 @@ public void The_default_value_of_an_option_with_no_arguments_is_false()
353
369
354
370
var result = command . Parse ( "-x" ) ;
355
371
356
- result . FindResultFor ( option )
357
- . GetValueOrDefault ( )
372
+ result . ValueForOption ( option )
358
373
. Should ( )
359
374
. Be ( true ) ;
360
375
}
@@ -371,9 +386,9 @@ public void By_default_an_option_without_arguments_parses_as_false_when_it_is_no
371
386
372
387
var result = command . Parse ( "something" ) ;
373
388
374
- result . ValueForOption < bool > ( option )
389
+ result . ValueForOption ( option )
375
390
. Should ( )
376
- . BeFalse ( ) ;
391
+ . Be ( false ) ;
377
392
}
378
393
379
394
[ Fact ]
@@ -442,83 +457,28 @@ public void A_default_value_with_a_custom_constructor_can_be_specified_for_a_com
442
457
443
458
result . Errors . Should ( ) . BeEmpty ( ) ;
444
459
445
- var value = result . ValueForArgument ( "the-arg" ) ;
460
+ var value = result . ValueForArgument ( argument ) ;
446
461
447
462
value . Should ( ) . Be ( directoryInfo ) ;
448
463
}
449
464
450
465
[ Fact ]
451
- public void An_option_argument_with_a_default_argument_can_be_converted_to_the_requested_type ( )
466
+ public void Specifying_an_option_argument_overrides_the_default_value ( )
452
467
{
453
- var option = new Option < string > ( "-x" , ( ) => " 123" ) ;
468
+ var option = new Option < int > ( "-x" , ( ) => 123 ) ;
454
469
455
470
var command = new Command ( "something" )
456
471
{
457
472
option
458
473
} ;
459
474
460
- var result = command . Parse ( "something" ) ;
461
-
462
- var value = result . ValueForOption < int > ( option ) ;
463
-
464
- value . Should ( ) . Be ( 123 ) ;
465
- }
466
-
467
- [ Fact ]
468
- public void Specifying_an_option_argument_overrides_the_default_value ( )
469
- {
470
- var command = new Command ( "something" )
471
- {
472
- new Option < int > ( "-x" , ( ) => 123 )
473
- } ;
474
-
475
475
var result = command . Parse ( "something -x 456" ) ;
476
476
477
- var value = result . ValueForOption < int > ( "-x" ) ;
477
+ var value = result . ValueForOption ( option ) ;
478
478
479
479
value . Should ( ) . Be ( 456 ) ;
480
480
}
481
-
482
- [ Fact ]
483
- public void Values_can_be_correctly_converted_to_int_without_the_parser_specifying_a_custom_converter ( )
484
- {
485
- var option = new Option ( "-x" , arity : ArgumentArity . ZeroOrOne ) ;
486
-
487
- var value = option . Parse ( "-x 123" ) . ValueForOption < int > ( option ) ;
488
-
489
- value . Should ( ) . Be ( 123 ) ;
490
- }
491
-
492
- [ Fact ]
493
- public void Values_can_be_correctly_converted_to_nullable_int_with_no_value_without_the_parser_specifying_a_custom_converter ( )
494
- {
495
- var option = new Option ( "-x" , arity : ArgumentArity . ZeroOrOne ) ;
496
-
497
- var value = option . Parse ( "" ) . ValueForOption < int ? > ( "-x" ) ;
498
-
499
- value . Should ( ) . BeNull ( ) ;
500
- }
501
-
502
- [ Fact ]
503
- public void Values_can_be_correctly_converted_to_nullable_int_with_a_value_without_the_parser_specifying_a_custom_converter ( )
504
- {
505
- var option = new Option ( "-x" , arity : ArgumentArity . ZeroOrOne ) ;
506
-
507
- var value = option . Parse ( "-x 123" ) . ValueForOption < int ? > ( option ) ;
508
-
509
- value . Should ( ) . Be ( 123 ) ;
510
- }
511
-
512
- [ Fact ]
513
- public void Values_can_be_correctly_converted_to_decimal_without_the_parser_specifying_a_custom_converter ( )
514
- {
515
- var option = new Option ( "-x" , arity : ArgumentArity . ZeroOrOne ) ;
516
-
517
- var value = option . Parse ( "-x 123.456" ) . ValueForOption < decimal > ( option ) ;
518
-
519
- value . Should ( ) . Be ( 123.456m ) ;
520
- }
521
-
481
+
522
482
[ Fact ]
523
483
public void Values_can_be_correctly_converted_to_double_without_the_parser_specifying_a_custom_converter ( )
524
484
{
0 commit comments