@@ -176,7 +176,17 @@ fn option_exact_less() {
176176 . try_get_matches_from ( vec ! [ "" , "-o" , "val1" , "-o" , "val2" ] ) ;
177177
178178 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+ "# ] ] ) ;
180190}
181191
182192#[ test]
@@ -194,7 +204,17 @@ fn option_exact_more() {
194204 ] ) ;
195205
196206 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+ "# ] ] ) ;
198218}
199219
200220#[ test]
@@ -235,7 +255,17 @@ fn option_min_less() {
235255 . try_get_matches_from ( vec ! [ "" , "-o" , "val1" , "val2" ] ) ;
236256
237257 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 more 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+ "# ] ] ) ;
239269}
240270
241271#[ test]
@@ -356,7 +386,15 @@ fn option_max_zero() {
356386 . try_get_matches_from ( vec ! [ "" , "-o" ] ) ;
357387
358388 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+ "# ] ] ) ;
360398}
361399
362400#[ test]
@@ -397,8 +435,18 @@ fn option_max_more() {
397435 . try_get_matches_from ( vec ! [ "" , "-o" , "val1" , "val2" , "val3" , "val4" ] ) ;
398436
399437 assert ! ( m. is_err( ) ) ;
438+ let err = m. unwrap_err ( ) ;
400439 // 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+ "# ] ] ) ;
402450}
403451
404452#[ test]
@@ -429,14 +477,17 @@ fn optional_value() {
429477 assert_eq ! ( m. get_one:: <String >( "port" ) . unwrap( ) , "42" ) ;
430478
431479 let help = cmd. render_help ( ) . to_string ( ) ;
432- assert_data_eq ! ( help, str ![ [ r#"
480+ assert_data_eq ! (
481+ help,
482+ str ![ [ r#"
433483Usage: test [OPTIONS]
434484
435485Options:
436486 -p [<NUM>]
437487 -h, --help Print help
438488
439- "# ] ] ) ;
489+ "# ] ]
490+ ) ;
440491}
441492
442493#[ test]
@@ -489,7 +540,17 @@ fn positional_exact_less() {
489540 . try_get_matches_from ( vec ! [ "myprog" , "val1" , "val2" ] ) ;
490541
491542 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+ "# ] ] ) ;
493554}
494555
495556#[ test]
@@ -499,7 +560,17 @@ fn positional_exact_more() {
499560 . try_get_matches_from ( vec ! [ "myprog" , "val1" , "val2" , "val3" , "val4" ] ) ;
500561
501562 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+ "# ] ] ) ;
503574}
504575
505576#[ test]
@@ -528,7 +599,17 @@ fn positional_min_less() {
528599 . try_get_matches_from ( vec ! [ "myprog" , "val1" , "val2" ] ) ;
529600
530601 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 more 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+ "# ] ] ) ;
532613}
533614
534615#[ test]
@@ -595,7 +676,17 @@ fn positional_max_more() {
595676 . try_get_matches_from ( vec ! [ "myprog" , "val1" , "val2" , "val3" , "val4" ] ) ;
596677
597678 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+ "# ] ] ) ;
599690}
600691
601692#[ test]
@@ -1586,22 +1677,42 @@ fn issue_1480_max_values_consumes_extra_arg_1() {
15861677
15871678#[ test]
15881679fn issue_1480_max_values_consumes_extra_arg_2 ( ) {
1589- let res = Command :: new ( "prog" )
1680+ let m = Command :: new ( "prog" )
15901681 . arg ( Arg :: new ( "field" ) . num_args ( ..=1 ) . long ( "field" ) )
15911682 . try_get_matches_from ( vec ! [ "prog" , "--field" , "1" , "2" ] ) ;
15921683
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+ "# ] ] ) ;
15951696}
15961697
15971698#[ test]
15981699fn issue_1480_max_values_consumes_extra_arg_3 ( ) {
1599- let res = Command :: new ( "prog" )
1700+ let m = Command :: new ( "prog" )
16001701 . arg ( Arg :: new ( "field" ) . num_args ( ..=1 ) . long ( "field" ) )
16011702 . try_get_matches_from ( vec ! [ "prog" , "--field" , "1" , "2" , "3" ] ) ;
16021703
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+ "# ] ] ) ;
16051716}
16061717
16071718#[ test]
@@ -1728,7 +1839,17 @@ fn issue_2229() {
17281839 ] ) ;
17291840
17301841 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+ "# ] ] ) ;
17321853}
17331854
17341855#[ test]
0 commit comments