@@ -176,7 +176,17 @@ fn option_exact_less() {
176
176
. try_get_matches_from ( vec ! [ "" , "-o" , "val1" , "-o" , "val2" ] ) ;
177
177
178
178
assert ! ( m. is_err( ) ) ;
179
- assert_eq ! ( m. unwrap_err( ) . kind( ) , ErrorKind :: WrongNumberOfValues ) ;
179
+ let err = m. unwrap_err ( ) ;
180
+ assert_eq ! ( err. kind( ) , ErrorKind :: WrongNumberOfValues ) ;
181
+ #[ cfg( feature = "error-context" ) ]
182
+ assert_data_eq ! ( err. to_string( ) , str ![ [ r#"
183
+ error: 3 values required for '-o <option> <option> <option>' but 1 was provided
184
+
185
+ Usage: multiple_values [OPTIONS]
186
+
187
+ For more information, try '--help'.
188
+
189
+ "# ] ] ) ;
180
190
}
181
191
182
192
#[ test]
@@ -194,7 +204,17 @@ fn option_exact_more() {
194
204
] ) ;
195
205
196
206
assert ! ( m. is_err( ) ) ;
197
- assert_eq ! ( m. unwrap_err( ) . kind( ) , ErrorKind :: WrongNumberOfValues ) ;
207
+ let err = m. unwrap_err ( ) ;
208
+ assert_eq ! ( err. kind( ) , ErrorKind :: WrongNumberOfValues ) ;
209
+ #[ cfg( feature = "error-context" ) ]
210
+ assert_data_eq ! ( err. to_string( ) , str ![ [ r#"
211
+ error: 3 values required for '-o <option> <option> <option>' but 1 was provided
212
+
213
+ Usage: multiple_values [OPTIONS]
214
+
215
+ For more information, try '--help'.
216
+
217
+ "# ] ] ) ;
198
218
}
199
219
200
220
#[ test]
@@ -235,7 +255,17 @@ fn option_min_less() {
235
255
. try_get_matches_from ( vec ! [ "" , "-o" , "val1" , "val2" ] ) ;
236
256
237
257
assert ! ( m. is_err( ) ) ;
238
- assert_eq ! ( m. unwrap_err( ) . kind( ) , ErrorKind :: TooFewValues ) ;
258
+ let err = m. unwrap_err ( ) ;
259
+ assert_eq ! ( err. kind( ) , ErrorKind :: TooFewValues ) ;
260
+ #[ cfg( feature = "error-context" ) ]
261
+ assert_data_eq ! ( err. to_string( ) , str ![ [ r#"
262
+ error: 3 values required by '-o <option> <option> <option>...'; only 2 were provided
263
+
264
+ Usage: multiple_values [OPTIONS]
265
+
266
+ For more information, try '--help'.
267
+
268
+ "# ] ] ) ;
239
269
}
240
270
241
271
#[ test]
@@ -356,7 +386,15 @@ fn option_max_zero() {
356
386
. try_get_matches_from ( vec ! [ "" , "-o" ] ) ;
357
387
358
388
assert ! ( m. is_err( ) ) ;
359
- assert_eq ! ( m. unwrap_err( ) . kind( ) , ErrorKind :: InvalidValue ) ;
389
+ let err = m. unwrap_err ( ) ;
390
+ assert_eq ! ( err. kind( ) , ErrorKind :: InvalidValue ) ;
391
+ #[ cfg( feature = "error-context" ) ]
392
+ assert_data_eq ! ( err. to_string( ) , str ![ [ r#"
393
+ error: a value is required for '-o <option>...' but none was supplied
394
+
395
+ For more information, try '--help'.
396
+
397
+ "# ] ] ) ;
360
398
}
361
399
362
400
#[ test]
@@ -397,8 +435,18 @@ fn option_max_more() {
397
435
. try_get_matches_from ( vec ! [ "" , "-o" , "val1" , "val2" , "val3" , "val4" ] ) ;
398
436
399
437
assert ! ( m. is_err( ) ) ;
438
+ let err = m. unwrap_err ( ) ;
400
439
// Can end up being TooManyValues or UnknownArgument
401
- assert_eq ! ( m. unwrap_err( ) . kind( ) , ErrorKind :: UnknownArgument ) ;
440
+ assert_eq ! ( err. kind( ) , ErrorKind :: UnknownArgument ) ;
441
+ #[ cfg( feature = "error-context" ) ]
442
+ assert_data_eq ! ( err. to_string( ) , str ![ [ r#"
443
+ error: unexpected argument 'val4' found
444
+
445
+ Usage: multiple_values [OPTIONS]
446
+
447
+ For more information, try '--help'.
448
+
449
+ "# ] ] ) ;
402
450
}
403
451
404
452
#[ test]
@@ -429,14 +477,17 @@ fn optional_value() {
429
477
assert_eq ! ( m. get_one:: <String >( "port" ) . unwrap( ) , "42" ) ;
430
478
431
479
let help = cmd. render_help ( ) . to_string ( ) ;
432
- assert_data_eq ! ( help, str ![ [ r#"
480
+ assert_data_eq ! (
481
+ help,
482
+ str ![ [ r#"
433
483
Usage: test [OPTIONS]
434
484
435
485
Options:
436
486
-p [<NUM>]
437
487
-h, --help Print help
438
488
439
- "# ] ] ) ;
489
+ "# ] ]
490
+ ) ;
440
491
}
441
492
442
493
#[ test]
@@ -489,7 +540,17 @@ fn positional_exact_less() {
489
540
. try_get_matches_from ( vec ! [ "myprog" , "val1" , "val2" ] ) ;
490
541
491
542
assert ! ( m. is_err( ) ) ;
492
- assert_eq ! ( m. unwrap_err( ) . kind( ) , ErrorKind :: WrongNumberOfValues ) ;
543
+ let err = m. unwrap_err ( ) ;
544
+ assert_eq ! ( err. kind( ) , ErrorKind :: WrongNumberOfValues ) ;
545
+ #[ cfg( feature = "error-context" ) ]
546
+ assert_data_eq ! ( err. to_string( ) , str ![ [ r#"
547
+ error: 3 values required for '[pos] [pos] [pos]' but 2 were provided
548
+
549
+ Usage: myprog [pos] [pos] [pos]
550
+
551
+ For more information, try '--help'.
552
+
553
+ "# ] ] ) ;
493
554
}
494
555
495
556
#[ test]
@@ -499,7 +560,17 @@ fn positional_exact_more() {
499
560
. try_get_matches_from ( vec ! [ "myprog" , "val1" , "val2" , "val3" , "val4" ] ) ;
500
561
501
562
assert ! ( m. is_err( ) ) ;
502
- assert_eq ! ( m. unwrap_err( ) . kind( ) , ErrorKind :: WrongNumberOfValues ) ;
563
+ let err = m. unwrap_err ( ) ;
564
+ assert_eq ! ( err. kind( ) , ErrorKind :: WrongNumberOfValues ) ;
565
+ #[ cfg( feature = "error-context" ) ]
566
+ assert_data_eq ! ( err. to_string( ) , str ![ [ r#"
567
+ error: 3 values required for '[pos] [pos] [pos]' but 4 were provided
568
+
569
+ Usage: myprog [pos] [pos] [pos]
570
+
571
+ For more information, try '--help'.
572
+
573
+ "# ] ] ) ;
503
574
}
504
575
505
576
#[ test]
@@ -528,7 +599,17 @@ fn positional_min_less() {
528
599
. try_get_matches_from ( vec ! [ "myprog" , "val1" , "val2" ] ) ;
529
600
530
601
assert ! ( m. is_err( ) ) ;
531
- assert_eq ! ( m. unwrap_err( ) . kind( ) , ErrorKind :: TooFewValues ) ;
602
+ let err = m. unwrap_err ( ) ;
603
+ assert_eq ! ( err. kind( ) , ErrorKind :: TooFewValues ) ;
604
+ #[ cfg( feature = "error-context" ) ]
605
+ assert_data_eq ! ( err. to_string( ) , str ![ [ r#"
606
+ error: 3 values required by '[pos] [pos] [pos]...'; only 2 were provided
607
+
608
+ Usage: myprog [pos] [pos] [pos]...
609
+
610
+ For more information, try '--help'.
611
+
612
+ "# ] ] ) ;
532
613
}
533
614
534
615
#[ test]
@@ -595,7 +676,17 @@ fn positional_max_more() {
595
676
. try_get_matches_from ( vec ! [ "myprog" , "val1" , "val2" , "val3" , "val4" ] ) ;
596
677
597
678
assert ! ( m. is_err( ) ) ;
598
- assert_eq ! ( m. unwrap_err( ) . kind( ) , ErrorKind :: TooManyValues ) ;
679
+ let err = m. unwrap_err ( ) ;
680
+ assert_eq ! ( err. kind( ) , ErrorKind :: TooManyValues ) ;
681
+ #[ cfg( feature = "error-context" ) ]
682
+ assert_data_eq ! ( err. to_string( ) , str ![ [ r#"
683
+ error: unexpected value 'val4' for '[pos]...' found; no more were expected
684
+
685
+ Usage: myprog [pos]...
686
+
687
+ For more information, try '--help'.
688
+
689
+ "# ] ] ) ;
599
690
}
600
691
601
692
#[ test]
@@ -1586,22 +1677,42 @@ fn issue_1480_max_values_consumes_extra_arg_1() {
1586
1677
1587
1678
#[ test]
1588
1679
fn issue_1480_max_values_consumes_extra_arg_2 ( ) {
1589
- let res = Command :: new ( "prog" )
1680
+ let m = Command :: new ( "prog" )
1590
1681
. arg ( Arg :: new ( "field" ) . num_args ( ..=1 ) . long ( "field" ) )
1591
1682
. try_get_matches_from ( vec ! [ "prog" , "--field" , "1" , "2" ] ) ;
1592
1683
1593
- assert ! ( res. is_err( ) ) ;
1594
- assert_eq ! ( res. unwrap_err( ) . kind( ) , ErrorKind :: UnknownArgument ) ;
1684
+ assert ! ( m. is_err( ) ) ;
1685
+ let err = m. unwrap_err ( ) ;
1686
+ assert_eq ! ( err. kind( ) , ErrorKind :: UnknownArgument ) ;
1687
+ #[ cfg( feature = "error-context" ) ]
1688
+ assert_data_eq ! ( err. to_string( ) , str ![ [ r#"
1689
+ error: unexpected argument '2' found
1690
+
1691
+ Usage: prog [OPTIONS]
1692
+
1693
+ For more information, try '--help'.
1694
+
1695
+ "# ] ] ) ;
1595
1696
}
1596
1697
1597
1698
#[ test]
1598
1699
fn issue_1480_max_values_consumes_extra_arg_3 ( ) {
1599
- let res = Command :: new ( "prog" )
1700
+ let m = Command :: new ( "prog" )
1600
1701
. arg ( Arg :: new ( "field" ) . num_args ( ..=1 ) . long ( "field" ) )
1601
1702
. try_get_matches_from ( vec ! [ "prog" , "--field" , "1" , "2" , "3" ] ) ;
1602
1703
1603
- assert ! ( res. is_err( ) ) ;
1604
- assert_eq ! ( res. unwrap_err( ) . kind( ) , ErrorKind :: UnknownArgument ) ;
1704
+ assert ! ( m. is_err( ) ) ;
1705
+ let err = m. unwrap_err ( ) ;
1706
+ assert_eq ! ( err. kind( ) , ErrorKind :: UnknownArgument ) ;
1707
+ #[ cfg( feature = "error-context" ) ]
1708
+ assert_data_eq ! ( err. to_string( ) , str ![ [ r#"
1709
+ error: unexpected argument '2' found
1710
+
1711
+ Usage: prog [OPTIONS]
1712
+
1713
+ For more information, try '--help'.
1714
+
1715
+ "# ] ] ) ;
1605
1716
}
1606
1717
1607
1718
#[ test]
@@ -1728,7 +1839,17 @@ fn issue_2229() {
1728
1839
] ) ;
1729
1840
1730
1841
assert ! ( m. is_err( ) ) ;
1731
- assert_eq ! ( m. unwrap_err( ) . kind( ) , ErrorKind :: WrongNumberOfValues ) ;
1842
+ let err = m. unwrap_err ( ) ;
1843
+ assert_eq ! ( err. kind( ) , ErrorKind :: WrongNumberOfValues ) ;
1844
+ #[ cfg( feature = "error-context" ) ]
1845
+ assert_data_eq ! ( err. to_string( ) , str ![ [ r#"
1846
+ error: 3 values required for '[pos] [pos] [pos]' but 6 were provided
1847
+
1848
+ Usage: myprog [pos] [pos] [pos]
1849
+
1850
+ For more information, try '--help'.
1851
+
1852
+ "# ] ] ) ;
1732
1853
}
1733
1854
1734
1855
#[ test]
0 commit comments