2022-08-15 15:00:42 +00:00
use clap ::{ arg , value_parser , Command } ;
2021-12-13 15:28:42 +00:00
#[ cfg(debug_assertions) ]
2022-08-15 15:00:42 +00:00
use clap ::{ Arg , ArgAction } ;
#[ test ]
fn ids ( ) {
let m = Command ::new ( " test " )
2022-09-12 21:59:57 +00:00
. arg ( arg! ( - - color < when > ) . value_parser ( [ " auto " , " always " , " never " ] ) )
. arg ( arg! ( - - config < path > ) . value_parser ( value_parser! ( std ::path ::PathBuf ) ) )
2022-08-15 15:00:42 +00:00
. try_get_matches_from ( [ " test " , " --config=config.toml " , " --color=auto " ] )
. unwrap ( ) ;
assert_eq! (
m . ids ( ) . map ( | id | id . as_str ( ) ) . collect ::< Vec < _ > > ( ) ,
[ " config " , " color " ]
) ;
assert_eq! ( m . ids ( ) . len ( ) , 2 ) ;
}
#[ test ]
fn ids_ignore_unused ( ) {
let m = Command ::new ( " test " )
2022-09-12 21:59:57 +00:00
. arg ( arg! ( - - color < when > ) . value_parser ( [ " auto " , " always " , " never " ] ) )
. arg ( arg! ( - - config < path > ) . value_parser ( value_parser! ( std ::path ::PathBuf ) ) )
2022-08-15 15:00:42 +00:00
. try_get_matches_from ( [ " test " , " --config=config.toml " ] )
. unwrap ( ) ;
assert_eq! (
m . ids ( ) . map ( | id | id . as_str ( ) ) . collect ::< Vec < _ > > ( ) ,
[ " config " ]
) ;
assert_eq! ( m . ids ( ) . len ( ) , 1 ) ;
}
#[ test ]
fn ids_ignore_overridden ( ) {
let m = Command ::new ( " test " )
2022-09-12 21:59:57 +00:00
. arg ( arg! ( - - color < when > ) . value_parser ( [ " auto " , " always " , " never " ] ) )
2022-08-15 15:00:42 +00:00
. arg (
arg! ( - - config < path > )
. value_parser ( value_parser! ( std ::path ::PathBuf ) )
. overrides_with ( " color " ) ,
)
. try_get_matches_from ( [ " test " , " --config=config.toml " , " --color=auto " ] )
. unwrap ( ) ;
assert_eq! ( m . ids ( ) . map ( | id | id . as_str ( ) ) . collect ::< Vec < _ > > ( ) , [ " color " ] ) ;
assert_eq! ( m . ids ( ) . len ( ) , 1 ) ;
}
2021-10-26 22:22:35 +00:00
#[ test ]
#[ cfg(debug_assertions) ]
2022-06-10 01:03:28 +00:00
#[ should_panic = " Unknown argument or group id. Make sure you are using the argument id and not the short or long flags " ]
2021-10-26 22:22:35 +00:00
fn arg_matches_if_present_wrong_arg ( ) {
2022-02-12 03:48:29 +00:00
let m = Command ::new ( " test " )
2022-06-10 01:03:28 +00:00
. arg ( Arg ::new ( " flag " ) . short ( 'f' ) . action ( ArgAction ::SetTrue ) )
2021-12-27 18:56:12 +00:00
. try_get_matches_from ( & [ " test " , " -f " ] )
. unwrap ( ) ;
2021-10-26 22:22:35 +00:00
2022-06-10 01:03:28 +00:00
assert! ( * m . get_one ::< bool > ( " flag " ) . expect ( " defaulted by clap " ) ) ;
m . contains_id ( " f " ) ;
2021-10-26 22:22:35 +00:00
}
#[ test ]
#[ cfg(debug_assertions) ]
2022-05-24 15:16:50 +00:00
#[ should_panic = " Mismatch between definition and access of `o`. Unknown argument or group id. Make sure you are using the argument id and not the short or long flags " ]
2021-10-26 22:22:35 +00:00
fn arg_matches_value_of_wrong_arg ( ) {
2022-02-12 03:48:29 +00:00
let m = Command ::new ( " test " )
2022-07-26 00:17:01 +00:00
. arg ( Arg ::new ( " opt " ) . short ( 'o' ) . action ( ArgAction ::Set ) )
2021-12-27 18:56:12 +00:00
. try_get_matches_from ( & [ " test " , " -o " , " val " ] )
. unwrap ( ) ;
2021-10-26 22:22:35 +00:00
2022-05-24 15:16:50 +00:00
assert_eq! ( m . get_one ::< String > ( " opt " ) . map ( | v | v . as_str ( ) ) , Some ( " val " ) ) ;
m . get_one ::< String > ( " o " ) . map ( | v | v . as_str ( ) ) ;
2021-10-26 22:22:35 +00:00
}
#[ test ]
#[ cfg(debug_assertions) ]
2022-01-04 22:10:26 +00:00
#[ should_panic = " `seed` is not a name of a subcommand. " ]
2021-10-26 22:22:35 +00:00
fn arg_matches_subcommand_matches_wrong_sub ( ) {
2022-02-12 03:48:29 +00:00
let m = Command ::new ( " test " )
. subcommand ( Command ::new ( " speed " ) )
2021-12-27 18:56:12 +00:00
. try_get_matches_from ( & [ " test " , " speed " ] )
. unwrap ( ) ;
2021-10-26 22:22:35 +00:00
assert! ( m . subcommand_matches ( " speed " ) . is_some ( ) ) ;
m . subcommand_matches ( " seed " ) ;
}