2016-01-22 17:58:56 +00:00
#![ cfg(not(windows)) ]
2022-05-23 12:23:16 +00:00
use clap ::{ arg , error ::ErrorKind , value_parser , Arg , Command } ;
2016-01-22 17:58:56 +00:00
use std ::ffi ::OsString ;
use std ::os ::unix ::ffi ::OsStringExt ;
#[ test ]
fn invalid_utf8_strict_positional ( ) {
2022-02-12 03:48:29 +00:00
let m = Command ::new ( " bad_utf8 " )
2021-08-10 12:27:49 +00:00
. arg ( Arg ::new ( " arg " ) )
2018-10-19 20:42:13 +00:00
. try_get_matches_from ( vec! [ OsString ::from ( " " ) , OsString ::from_vec ( vec! [ 0xe9 ] ) ] ) ;
2016-01-22 17:58:56 +00:00
assert! ( m . is_err ( ) ) ;
2022-01-25 22:19:28 +00:00
assert_eq! ( m . unwrap_err ( ) . kind ( ) , ErrorKind ::InvalidUtf8 ) ;
2016-01-22 17:58:56 +00:00
}
#[ test ]
fn invalid_utf8_strict_option_short_space ( ) {
2022-02-12 03:48:29 +00:00
let m = Command ::new ( " bad_utf8 " )
2021-08-10 12:27:49 +00:00
. arg ( Arg ::new ( " arg " ) . short ( 'a' ) . long ( " arg " ) . takes_value ( true ) )
2018-10-19 20:42:13 +00:00
. try_get_matches_from ( vec! [
2018-01-25 04:05:05 +00:00
OsString ::from ( " " ) ,
OsString ::from ( " -a " ) ,
OsString ::from_vec ( vec! [ 0xe9 ] ) ,
] ) ;
2016-01-22 17:58:56 +00:00
assert! ( m . is_err ( ) ) ;
2022-01-25 22:19:28 +00:00
assert_eq! ( m . unwrap_err ( ) . kind ( ) , ErrorKind ::InvalidUtf8 ) ;
2016-01-22 17:58:56 +00:00
}
#[ test ]
fn invalid_utf8_strict_option_short_equals ( ) {
2022-02-12 03:48:29 +00:00
let m = Command ::new ( " bad_utf8 " )
2021-08-10 12:27:49 +00:00
. arg ( Arg ::new ( " arg " ) . short ( 'a' ) . long ( " arg " ) . takes_value ( true ) )
2018-10-19 20:42:13 +00:00
. try_get_matches_from ( vec! [
2018-01-25 04:05:05 +00:00
OsString ::from ( " " ) ,
OsString ::from_vec ( vec! [ 0x2d , 0x61 , 0x3d , 0xe9 ] ) ,
] ) ;
2016-01-22 17:58:56 +00:00
assert! ( m . is_err ( ) ) ;
2022-01-25 22:19:28 +00:00
assert_eq! ( m . unwrap_err ( ) . kind ( ) , ErrorKind ::InvalidUtf8 ) ;
2016-01-22 17:58:56 +00:00
}
#[ test ]
fn invalid_utf8_strict_option_short_no_space ( ) {
2022-02-12 03:48:29 +00:00
let m = Command ::new ( " bad_utf8 " )
2021-08-10 12:27:49 +00:00
. arg ( Arg ::new ( " arg " ) . short ( 'a' ) . long ( " arg " ) . takes_value ( true ) )
2018-10-19 20:42:13 +00:00
. try_get_matches_from ( vec! [
2018-01-25 04:05:05 +00:00
OsString ::from ( " " ) ,
OsString ::from_vec ( vec! [ 0x2d , 0x61 , 0xe9 ] ) ,
] ) ;
2016-01-22 17:58:56 +00:00
assert! ( m . is_err ( ) ) ;
2022-01-25 22:19:28 +00:00
assert_eq! ( m . unwrap_err ( ) . kind ( ) , ErrorKind ::InvalidUtf8 ) ;
2016-01-22 17:58:56 +00:00
}
#[ test ]
fn invalid_utf8_strict_option_long_space ( ) {
2022-02-12 03:48:29 +00:00
let m = Command ::new ( " bad_utf8 " )
2021-08-10 12:27:49 +00:00
. arg ( Arg ::new ( " arg " ) . short ( 'a' ) . long ( " arg " ) . takes_value ( true ) )
2018-10-19 20:42:13 +00:00
. try_get_matches_from ( vec! [
2018-01-25 04:05:05 +00:00
OsString ::from ( " " ) ,
OsString ::from ( " --arg " ) ,
OsString ::from_vec ( vec! [ 0xe9 ] ) ,
] ) ;
2016-01-22 17:58:56 +00:00
assert! ( m . is_err ( ) ) ;
2022-01-25 22:19:28 +00:00
assert_eq! ( m . unwrap_err ( ) . kind ( ) , ErrorKind ::InvalidUtf8 ) ;
2016-01-22 17:58:56 +00:00
}
#[ test ]
fn invalid_utf8_strict_option_long_equals ( ) {
2022-02-12 03:48:29 +00:00
let m = Command ::new ( " bad_utf8 " )
2021-08-10 12:27:49 +00:00
. arg ( Arg ::new ( " arg " ) . short ( 'a' ) . long ( " arg " ) . takes_value ( true ) )
2018-10-19 20:42:13 +00:00
. try_get_matches_from ( vec! [
2018-01-25 04:05:05 +00:00
OsString ::from ( " " ) ,
OsString ::from_vec ( vec! [ 0x2d , 0x2d , 0x61 , 0x72 , 0x67 , 0x3d , 0xe9 ] ) ,
] ) ;
2016-01-22 17:58:56 +00:00
assert! ( m . is_err ( ) ) ;
2022-01-25 22:19:28 +00:00
assert_eq! ( m . unwrap_err ( ) . kind ( ) , ErrorKind ::InvalidUtf8 ) ;
2016-01-22 17:58:56 +00:00
}
#[ test ]
fn invalid_utf8_lossy_positional ( ) {
2022-05-23 12:23:16 +00:00
#![ allow(deprecated) ]
2022-02-12 03:48:29 +00:00
let r = Command ::new ( " bad_utf8 " )
2021-08-10 12:27:49 +00:00
. arg ( Arg ::new ( " arg " ) . allow_invalid_utf8 ( true ) )
2018-10-19 20:42:13 +00:00
. try_get_matches_from ( vec! [ OsString ::from ( " " ) , OsString ::from_vec ( vec! [ 0xe9 ] ) ] ) ;
2021-12-27 19:57:38 +00:00
assert! ( r . is_ok ( ) , " {} " , r . unwrap_err ( ) ) ;
2016-01-22 17:58:56 +00:00
let m = r . unwrap ( ) ;
assert! ( m . is_present ( " arg " ) ) ;
2016-01-29 02:51:50 +00:00
assert_eq! ( & * m . value_of_lossy ( " arg " ) . unwrap ( ) , " \u{FFFD} " ) ;
2016-01-22 17:58:56 +00:00
}
#[ test ]
fn invalid_utf8_lossy_option_short_space ( ) {
2022-05-23 12:23:16 +00:00
#![ allow(deprecated) ]
2022-02-12 03:48:29 +00:00
let r = Command ::new ( " bad_utf8 " )
2021-08-10 12:27:49 +00:00
. arg (
Arg ::new ( " arg " )
. short ( 'a' )
. long ( " arg " )
. takes_value ( true )
. allow_invalid_utf8 ( true ) ,
)
2018-10-19 20:42:13 +00:00
. try_get_matches_from ( vec! [
2018-01-25 04:05:05 +00:00
OsString ::from ( " " ) ,
OsString ::from ( " -a " ) ,
OsString ::from_vec ( vec! [ 0xe9 ] ) ,
] ) ;
2021-12-27 19:57:38 +00:00
assert! ( r . is_ok ( ) , " {} " , r . unwrap_err ( ) ) ;
2016-01-22 17:58:56 +00:00
let m = r . unwrap ( ) ;
assert! ( m . is_present ( " arg " ) ) ;
2016-01-29 02:51:50 +00:00
assert_eq! ( & * m . value_of_lossy ( " arg " ) . unwrap ( ) , " \u{FFFD} " ) ;
2016-01-22 17:58:56 +00:00
}
#[ test ]
fn invalid_utf8_lossy_option_short_equals ( ) {
2022-05-23 12:23:16 +00:00
#![ allow(deprecated) ]
2022-02-12 03:48:29 +00:00
let r = Command ::new ( " bad_utf8 " )
2021-08-10 12:27:49 +00:00
. arg (
Arg ::new ( " arg " )
. short ( 'a' )
. long ( " arg " )
. takes_value ( true )
. allow_invalid_utf8 ( true ) ,
)
2018-10-19 20:42:13 +00:00
. try_get_matches_from ( vec! [
2018-01-25 04:05:05 +00:00
OsString ::from ( " " ) ,
OsString ::from_vec ( vec! [ 0x2d , 0x61 , 0x3d , 0xe9 ] ) ,
] ) ;
2021-12-27 19:57:38 +00:00
assert! ( r . is_ok ( ) , " {} " , r . unwrap_err ( ) ) ;
2016-01-22 17:58:56 +00:00
let m = r . unwrap ( ) ;
assert! ( m . is_present ( " arg " ) ) ;
2016-01-29 02:51:50 +00:00
assert_eq! ( & * m . value_of_lossy ( " arg " ) . unwrap ( ) , " \u{FFFD} " ) ;
2016-01-22 17:58:56 +00:00
}
#[ test ]
fn invalid_utf8_lossy_option_short_no_space ( ) {
2022-05-23 12:23:16 +00:00
#![ allow(deprecated) ]
2022-02-12 03:48:29 +00:00
let r = Command ::new ( " bad_utf8 " )
2021-08-10 12:27:49 +00:00
. arg (
Arg ::new ( " arg " )
. short ( 'a' )
. long ( " arg " )
. takes_value ( true )
. allow_invalid_utf8 ( true ) ,
)
2018-10-19 20:42:13 +00:00
. try_get_matches_from ( vec! [
2018-01-25 04:05:05 +00:00
OsString ::from ( " " ) ,
OsString ::from_vec ( vec! [ 0x2d , 0x61 , 0xe9 ] ) ,
] ) ;
2021-12-27 19:57:38 +00:00
assert! ( r . is_ok ( ) , " {} " , r . unwrap_err ( ) ) ;
2016-01-22 17:58:56 +00:00
let m = r . unwrap ( ) ;
assert! ( m . is_present ( " arg " ) ) ;
2016-01-29 02:51:50 +00:00
assert_eq! ( & * m . value_of_lossy ( " arg " ) . unwrap ( ) , " \u{FFFD} " ) ;
2016-01-22 17:58:56 +00:00
}
#[ test ]
fn invalid_utf8_lossy_option_long_space ( ) {
2022-05-23 12:23:16 +00:00
#![ allow(deprecated) ]
2022-02-12 03:48:29 +00:00
let r = Command ::new ( " bad_utf8 " )
2021-08-10 12:27:49 +00:00
. arg (
Arg ::new ( " arg " )
. short ( 'a' )
. long ( " arg " )
. takes_value ( true )
. allow_invalid_utf8 ( true ) ,
)
2018-10-19 20:42:13 +00:00
. try_get_matches_from ( vec! [
2018-01-25 04:05:05 +00:00
OsString ::from ( " " ) ,
OsString ::from ( " --arg " ) ,
OsString ::from_vec ( vec! [ 0xe9 ] ) ,
] ) ;
2021-12-27 19:57:38 +00:00
assert! ( r . is_ok ( ) , " {} " , r . unwrap_err ( ) ) ;
2016-01-22 17:58:56 +00:00
let m = r . unwrap ( ) ;
assert! ( m . is_present ( " arg " ) ) ;
2016-01-29 02:51:50 +00:00
assert_eq! ( & * m . value_of_lossy ( " arg " ) . unwrap ( ) , " \u{FFFD} " ) ;
2016-01-22 17:58:56 +00:00
}
#[ test ]
fn invalid_utf8_lossy_option_long_equals ( ) {
2022-05-23 12:23:16 +00:00
#![ allow(deprecated) ]
2022-02-12 03:48:29 +00:00
let r = Command ::new ( " bad_utf8 " )
2021-08-10 12:27:49 +00:00
. arg (
Arg ::new ( " arg " )
. short ( 'a' )
. long ( " arg " )
. takes_value ( true )
. allow_invalid_utf8 ( true ) ,
)
2018-10-19 20:42:13 +00:00
. try_get_matches_from ( vec! [
2018-01-25 04:05:05 +00:00
OsString ::from ( " " ) ,
OsString ::from_vec ( vec! [ 0x2d , 0x2d , 0x61 , 0x72 , 0x67 , 0x3d , 0xe9 ] ) ,
] ) ;
2021-12-27 19:57:38 +00:00
assert! ( r . is_ok ( ) , " {} " , r . unwrap_err ( ) ) ;
2016-01-22 17:58:56 +00:00
let m = r . unwrap ( ) ;
assert! ( m . is_present ( " arg " ) ) ;
2016-01-29 02:51:50 +00:00
assert_eq! ( & * m . value_of_lossy ( " arg " ) . unwrap ( ) , " \u{FFFD} " ) ;
2016-01-22 17:58:56 +00:00
}
#[ test ]
fn invalid_utf8_positional ( ) {
2022-02-12 03:48:29 +00:00
let r = Command ::new ( " bad_utf8 " )
2022-05-23 12:23:16 +00:00
. arg ( Arg ::new ( " arg " ) . value_parser ( value_parser! ( OsString ) ) )
2018-10-19 20:42:13 +00:00
. try_get_matches_from ( vec! [ OsString ::from ( " " ) , OsString ::from_vec ( vec! [ 0xe9 ] ) ] ) ;
2021-12-27 19:57:38 +00:00
assert! ( r . is_ok ( ) , " {} " , r . unwrap_err ( ) ) ;
2016-01-22 17:58:56 +00:00
let m = r . unwrap ( ) ;
assert! ( m . is_present ( " arg " ) ) ;
2018-01-25 04:05:05 +00:00
assert_eq! (
2022-05-23 12:23:16 +00:00
& * m . get_one ::< OsString > ( " arg " ) . unwrap ( ) ,
2018-01-25 04:05:05 +00:00
& * OsString ::from_vec ( vec! [ 0xe9 ] )
) ;
2016-01-22 17:58:56 +00:00
}
#[ test ]
fn invalid_utf8_option_short_space ( ) {
2022-02-12 03:48:29 +00:00
let r = Command ::new ( " bad_utf8 " )
2021-08-10 12:27:49 +00:00
. arg (
Arg ::new ( " arg " )
. short ( 'a' )
. long ( " arg " )
. takes_value ( true )
2022-05-23 12:23:16 +00:00
. value_parser ( value_parser! ( OsString ) ) ,
2021-08-10 12:27:49 +00:00
)
2018-10-19 20:42:13 +00:00
. try_get_matches_from ( vec! [
2018-01-25 04:05:05 +00:00
OsString ::from ( " " ) ,
OsString ::from ( " -a " ) ,
OsString ::from_vec ( vec! [ 0xe9 ] ) ,
] ) ;
2021-12-27 19:57:38 +00:00
assert! ( r . is_ok ( ) , " {} " , r . unwrap_err ( ) ) ;
2016-01-22 17:58:56 +00:00
let m = r . unwrap ( ) ;
assert! ( m . is_present ( " arg " ) ) ;
2018-01-25 04:05:05 +00:00
assert_eq! (
2022-05-23 12:23:16 +00:00
& * m . get_one ::< OsString > ( " arg " ) . unwrap ( ) ,
2018-01-25 04:05:05 +00:00
& * OsString ::from_vec ( vec! [ 0xe9 ] )
) ;
2016-01-22 17:58:56 +00:00
}
#[ test ]
fn invalid_utf8_option_short_equals ( ) {
2022-02-12 03:48:29 +00:00
let r = Command ::new ( " bad_utf8 " )
2021-08-10 12:27:49 +00:00
. arg (
Arg ::new ( " arg " )
. short ( 'a' )
. long ( " arg " )
. takes_value ( true )
2022-05-23 12:23:16 +00:00
. value_parser ( value_parser! ( OsString ) ) ,
2021-08-10 12:27:49 +00:00
)
2018-10-19 20:42:13 +00:00
. try_get_matches_from ( vec! [
2018-01-25 04:05:05 +00:00
OsString ::from ( " " ) ,
OsString ::from_vec ( vec! [ 0x2d , 0x61 , 0x3d , 0xe9 ] ) ,
] ) ;
2021-12-27 19:57:38 +00:00
assert! ( r . is_ok ( ) , " {} " , r . unwrap_err ( ) ) ;
2016-01-22 17:58:56 +00:00
let m = r . unwrap ( ) ;
assert! ( m . is_present ( " arg " ) ) ;
2018-01-25 04:05:05 +00:00
assert_eq! (
2022-05-23 12:23:16 +00:00
& * m . get_one ::< OsString > ( " arg " ) . unwrap ( ) ,
2018-01-25 04:05:05 +00:00
& * OsString ::from_vec ( vec! [ 0xe9 ] )
) ;
2016-01-22 17:58:56 +00:00
}
#[ test ]
fn invalid_utf8_option_short_no_space ( ) {
2022-02-12 03:48:29 +00:00
let r = Command ::new ( " bad_utf8 " )
2021-08-10 12:27:49 +00:00
. arg (
Arg ::new ( " arg " )
. short ( 'a' )
. long ( " arg " )
. takes_value ( true )
2022-05-23 12:23:16 +00:00
. value_parser ( value_parser! ( OsString ) ) ,
2021-08-10 12:27:49 +00:00
)
2018-10-19 20:42:13 +00:00
. try_get_matches_from ( vec! [
2018-01-25 04:05:05 +00:00
OsString ::from ( " " ) ,
OsString ::from_vec ( vec! [ 0x2d , 0x61 , 0xe9 ] ) ,
] ) ;
2021-12-27 19:57:38 +00:00
assert! ( r . is_ok ( ) , " {} " , r . unwrap_err ( ) ) ;
2016-01-22 17:58:56 +00:00
let m = r . unwrap ( ) ;
assert! ( m . is_present ( " arg " ) ) ;
2018-01-25 04:05:05 +00:00
assert_eq! (
2022-05-23 12:23:16 +00:00
& * m . get_one ::< OsString > ( " arg " ) . unwrap ( ) ,
2018-01-25 04:05:05 +00:00
& * OsString ::from_vec ( vec! [ 0xe9 ] )
) ;
2016-01-22 17:58:56 +00:00
}
#[ test ]
fn invalid_utf8_option_long_space ( ) {
2022-02-12 03:48:29 +00:00
let r = Command ::new ( " bad_utf8 " )
2021-08-10 12:27:49 +00:00
. arg (
Arg ::new ( " arg " )
. short ( 'a' )
. long ( " arg " )
. takes_value ( true )
2022-05-23 12:23:16 +00:00
. value_parser ( value_parser! ( OsString ) ) ,
2021-08-10 12:27:49 +00:00
)
2018-10-19 20:42:13 +00:00
. try_get_matches_from ( vec! [
2018-01-25 04:05:05 +00:00
OsString ::from ( " " ) ,
OsString ::from ( " --arg " ) ,
OsString ::from_vec ( vec! [ 0xe9 ] ) ,
] ) ;
2021-12-27 19:57:38 +00:00
assert! ( r . is_ok ( ) , " {} " , r . unwrap_err ( ) ) ;
2016-01-22 17:58:56 +00:00
let m = r . unwrap ( ) ;
assert! ( m . is_present ( " arg " ) ) ;
2018-01-25 04:05:05 +00:00
assert_eq! (
2022-05-23 12:23:16 +00:00
& * m . get_one ::< OsString > ( " arg " ) . unwrap ( ) ,
2018-01-25 04:05:05 +00:00
& * OsString ::from_vec ( vec! [ 0xe9 ] )
) ;
2016-01-22 17:58:56 +00:00
}
#[ test ]
fn invalid_utf8_option_long_equals ( ) {
2022-02-12 03:48:29 +00:00
let r = Command ::new ( " bad_utf8 " )
2021-08-10 12:27:49 +00:00
. arg (
Arg ::new ( " arg " )
. short ( 'a' )
. long ( " arg " )
. takes_value ( true )
2022-05-23 12:23:16 +00:00
. value_parser ( value_parser! ( OsString ) ) ,
2021-08-10 12:27:49 +00:00
)
2018-10-19 20:42:13 +00:00
. try_get_matches_from ( vec! [
2018-01-25 04:05:05 +00:00
OsString ::from ( " " ) ,
OsString ::from_vec ( vec! [ 0x2d , 0x2d , 0x61 , 0x72 , 0x67 , 0x3d , 0xe9 ] ) ,
] ) ;
2021-12-27 19:57:38 +00:00
assert! ( r . is_ok ( ) , " {} " , r . unwrap_err ( ) ) ;
2016-01-22 17:58:56 +00:00
let m = r . unwrap ( ) ;
assert! ( m . is_present ( " arg " ) ) ;
2018-01-25 04:05:05 +00:00
assert_eq! (
2022-05-23 12:23:16 +00:00
& * m . get_one ::< OsString > ( " arg " ) . unwrap ( ) ,
2018-01-25 04:05:05 +00:00
& * OsString ::from_vec ( vec! [ 0xe9 ] )
) ;
2016-01-22 17:58:56 +00:00
}
2020-11-08 18:21:59 +00:00
#[ test ]
fn refuse_invalid_utf8_subcommand_with_allow_external_subcommands ( ) {
2022-02-12 03:48:29 +00:00
let m = Command ::new ( " bad_utf8 " )
2022-02-10 17:51:40 +00:00
. allow_external_subcommands ( true )
2020-11-08 18:21:59 +00:00
. try_get_matches_from ( vec! [
OsString ::from ( " " ) ,
OsString ::from_vec ( vec! [ 0xe9 ] ) ,
OsString ::from ( " normal " ) ,
] ) ;
assert! ( m . is_err ( ) ) ;
2022-01-25 22:19:28 +00:00
assert_eq! ( m . unwrap_err ( ) . kind ( ) , ErrorKind ::InvalidUtf8 ) ;
2020-11-08 18:21:59 +00:00
}
#[ test ]
2021-08-10 12:27:49 +00:00
fn refuse_invalid_utf8_subcommand_when_args_are_allowed_with_allow_external_subcommands ( ) {
2022-02-12 03:48:29 +00:00
let m = Command ::new ( " bad_utf8 " )
2022-02-10 17:51:40 +00:00
. allow_external_subcommands ( true )
. allow_invalid_utf8_for_external_subcommands ( true )
2020-11-08 18:21:59 +00:00
. try_get_matches_from ( vec! [
OsString ::from ( " " ) ,
OsString ::from_vec ( vec! [ 0xe9 ] ) ,
OsString ::from ( " normal " ) ,
] ) ;
2021-08-10 12:27:49 +00:00
assert! ( m . is_err ( ) ) ;
2022-01-25 22:19:28 +00:00
assert_eq! ( m . unwrap_err ( ) . kind ( ) , ErrorKind ::InvalidUtf8 ) ;
2020-11-08 18:21:59 +00:00
}
#[ test ]
fn refuse_invalid_utf8_subcommand_args_with_allow_external_subcommands ( ) {
2022-02-12 03:48:29 +00:00
let m = Command ::new ( " bad_utf8 " )
2022-02-10 17:51:40 +00:00
. allow_external_subcommands ( true )
2020-11-08 18:21:59 +00:00
. try_get_matches_from ( vec! [
OsString ::from ( " " ) ,
OsString ::from ( " subcommand " ) ,
OsString ::from ( " normal " ) ,
OsString ::from_vec ( vec! [ 0xe9 ] ) ,
OsString ::from ( " --another_normal " ) ,
] ) ;
assert! ( m . is_err ( ) ) ;
2022-01-25 22:19:28 +00:00
assert_eq! ( m . unwrap_err ( ) . kind ( ) , ErrorKind ::InvalidUtf8 ) ;
2020-11-08 18:21:59 +00:00
}
#[ test ]
fn allow_invalid_utf8_subcommand_args_with_allow_external_subcommands ( ) {
2022-02-12 03:48:29 +00:00
let m = Command ::new ( " bad_utf8 " )
2022-02-10 17:51:40 +00:00
. allow_external_subcommands ( true )
. allow_invalid_utf8_for_external_subcommands ( true )
2020-11-08 18:21:59 +00:00
. try_get_matches_from ( vec! [
OsString ::from ( " " ) ,
OsString ::from ( " subcommand " ) ,
OsString ::from ( " normal " ) ,
OsString ::from_vec ( vec! [ 0xe9 ] ) ,
OsString ::from ( " --another_normal " ) ,
] ) ;
2021-12-27 19:57:38 +00:00
assert! ( m . is_ok ( ) , " {} " , m . unwrap_err ( ) ) ;
2020-11-08 18:21:59 +00:00
let m = m . unwrap ( ) ;
let ( subcommand , args ) = m . subcommand ( ) . unwrap ( ) ;
2022-05-24 15:16:50 +00:00
let args = args
. get_many ::< OsString > ( " " )
. unwrap ( )
. cloned ( )
. collect ::< Vec < _ > > ( ) ;
2020-11-08 18:21:59 +00:00
assert_eq! ( subcommand , OsString ::from ( " subcommand " ) ) ;
assert_eq! (
args ,
vec! [
OsString ::from ( " normal " ) ,
OsString ::from_vec ( vec! [ 0xe9 ] ) ,
OsString ::from ( " --another_normal " ) ,
]
) ;
}
2021-11-29 19:16:03 +00:00
#[ test ]
fn allow_validated_utf8_value_of ( ) {
2022-05-23 12:23:16 +00:00
#![ allow(deprecated) ]
2022-02-12 03:48:29 +00:00
let a = Command ::new ( " test " ) . arg ( arg! ( - - name < NAME > ) ) ;
2021-11-29 19:16:03 +00:00
let m = a . try_get_matches_from ( [ " test " , " --name " , " me " ] ) . unwrap ( ) ;
2022-05-24 15:16:50 +00:00
let _ = m . get_one ::< String > ( " name " ) . map ( | v | v . as_str ( ) ) ;
2021-11-29 19:16:03 +00:00
}
#[ test ]
2021-12-11 11:26:07 +00:00
#[ should_panic = " Must use `Arg::allow_invalid_utf8` with `_os` lookups at `name` " ]
2021-11-29 19:16:03 +00:00
fn panic_validated_utf8_value_of_os ( ) {
2022-05-23 12:23:16 +00:00
#![ allow(deprecated) ]
2022-02-12 03:48:29 +00:00
let a = Command ::new ( " test " ) . arg ( arg! ( - - name < NAME > ) ) ;
2021-11-29 19:16:03 +00:00
let m = a . try_get_matches_from ( [ " test " , " --name " , " me " ] ) . unwrap ( ) ;
let _ = m . value_of_os ( " name " ) ;
}
#[ test ]
2022-05-12 08:44:02 +00:00
#[ should_panic = " Must use `Arg::allow_invalid_utf8` with `_os` lookups at `value` " ]
fn panic_validated_utf8_with_defaults ( ) {
2022-05-23 12:23:16 +00:00
#![ allow(deprecated) ]
2022-02-12 03:48:29 +00:00
let a = Command ::new ( " test " ) . arg ( arg! ( - - value < VALUE > ) . required ( false ) . default_value ( " foo " ) ) ;
2021-11-29 19:16:03 +00:00
let m = a . try_get_matches_from ( [ " test " ] ) . unwrap ( ) ;
2022-05-24 15:16:50 +00:00
let _ = m . get_one ::< String > ( " value " ) . map ( | v | v . as_str ( ) ) ;
2021-11-29 19:16:03 +00:00
let _ = m . value_of_os ( " value " ) ;
}
#[ test ]
fn allow_invalid_utf8_value_of_os ( ) {
2022-05-23 12:23:16 +00:00
#![ allow(deprecated) ]
2022-02-12 03:48:29 +00:00
let a = Command ::new ( " test " ) . arg ( arg! ( - - name < NAME > ) . allow_invalid_utf8 ( true ) ) ;
2021-11-29 19:16:03 +00:00
let m = a . try_get_matches_from ( [ " test " , " --name " , " me " ] ) . unwrap ( ) ;
let _ = m . value_of_os ( " name " ) ;
}
#[ test ]
2022-05-24 15:16:50 +00:00
#[ should_panic = " Mismatch between definition and access of `name`. Could not downcast to alloc::string::String, need to downcast to std::ffi::os_str::OsString " ]
2021-11-29 19:16:03 +00:00
fn panic_invalid_utf8_value_of ( ) {
2022-05-23 12:23:16 +00:00
#![ allow(deprecated) ]
2022-02-12 03:48:29 +00:00
let a = Command ::new ( " test " ) . arg ( arg! ( - - name < NAME > ) . allow_invalid_utf8 ( true ) ) ;
2021-11-29 19:16:03 +00:00
let m = a . try_get_matches_from ( [ " test " , " --name " , " me " ] ) . unwrap ( ) ;
2022-05-24 15:16:50 +00:00
let _ = m . get_one ::< String > ( " name " ) . map ( | v | v . as_str ( ) ) ;
2021-11-29 19:16:03 +00:00
}
#[ test ]
2022-05-24 15:16:50 +00:00
#[ should_panic = " Mismatch between definition and access of `value`. Could not downcast to alloc::string::String, need to downcast to std::ffi::os_str::OsString " ]
2022-05-12 08:44:02 +00:00
fn panic_invalid_utf8_with_defaults ( ) {
2022-05-23 12:23:16 +00:00
#![ allow(deprecated) ]
2022-02-12 03:48:29 +00:00
let a = Command ::new ( " test " ) . arg (
2021-11-29 19:16:03 +00:00
arg! ( - - value < VALUE > )
. required ( false )
. default_value ( " foo " )
. allow_invalid_utf8 ( true ) ,
) ;
let m = a . try_get_matches_from ( [ " test " ] ) . unwrap ( ) ;
let _ = m . value_of_os ( " value " ) ;
2022-05-24 15:16:50 +00:00
let _ = m . get_one ::< String > ( " value " ) . map ( | v | v . as_str ( ) ) ;
2021-11-29 19:16:03 +00:00
}
#[ test ]
fn allow_validated_utf8_external_subcommand_values_of ( ) {
2022-02-12 03:48:29 +00:00
let a = Command ::new ( " test " ) . allow_external_subcommands ( true ) ;
2021-11-29 19:16:03 +00:00
let m = a . try_get_matches_from ( vec! [ " test " , " cmd " , " arg " ] ) . unwrap ( ) ;
let ( _ext , args ) = m . subcommand ( ) . unwrap ( ) ;
2022-05-24 15:16:50 +00:00
args . get_many ::< String > ( " " ) . unwrap_or_default ( ) . count ( ) ;
2021-11-29 19:16:03 +00:00
}
#[ test ]
2022-05-24 15:16:50 +00:00
#[ should_panic = " Mismatch between definition and access of ``. Could not downcast to std::ffi::os_str::OsString, need to downcast to alloc::string::String " ]
2021-11-29 19:16:03 +00:00
fn panic_validated_utf8_external_subcommand_values_of_os ( ) {
2022-02-12 03:48:29 +00:00
let a = Command ::new ( " test " ) . allow_external_subcommands ( true ) ;
2021-11-29 19:16:03 +00:00
let m = a . try_get_matches_from ( vec! [ " test " , " cmd " , " arg " ] ) . unwrap ( ) ;
let ( _ext , args ) = m . subcommand ( ) . unwrap ( ) ;
2022-05-24 15:16:50 +00:00
args . get_many ::< OsString > ( " " ) . unwrap_or_default ( ) . count ( ) ;
2021-11-29 19:16:03 +00:00
}
#[ test ]
fn allow_invalid_utf8_external_subcommand_values_of_os ( ) {
2022-02-12 03:48:29 +00:00
let a = Command ::new ( " test " )
2022-02-10 17:51:40 +00:00
. allow_external_subcommands ( true )
. allow_invalid_utf8_for_external_subcommands ( true ) ;
2021-11-29 19:16:03 +00:00
let m = a . try_get_matches_from ( vec! [ " test " , " cmd " , " arg " ] ) . unwrap ( ) ;
let ( _ext , args ) = m . subcommand ( ) . unwrap ( ) ;
2022-05-24 15:16:50 +00:00
args . get_many ::< OsString > ( " " ) . unwrap_or_default ( ) . count ( ) ;
2021-11-29 19:16:03 +00:00
}
#[ test ]
2022-05-24 15:16:50 +00:00
#[ should_panic = " Mismatch between definition and access of ``. Could not downcast to alloc::string::String, need to downcast to std::ffi::os_str::OsString " ]
2021-11-29 19:16:03 +00:00
fn panic_invalid_utf8_external_subcommand_values_of ( ) {
2022-02-12 03:48:29 +00:00
let a = Command ::new ( " test " )
2022-02-10 17:51:40 +00:00
. allow_external_subcommands ( true )
. allow_invalid_utf8_for_external_subcommands ( true ) ;
2021-11-29 19:16:03 +00:00
let m = a . try_get_matches_from ( vec! [ " test " , " cmd " , " arg " ] ) . unwrap ( ) ;
let ( _ext , args ) = m . subcommand ( ) . unwrap ( ) ;
2022-05-24 15:16:50 +00:00
args . get_many ::< String > ( " " ) . unwrap_or_default ( ) . count ( ) ;
2021-11-29 19:16:03 +00:00
}