2022-08-08 21:08:47 +00:00
use clap ::{ error ::ErrorKind , ArgAction , Command } ;
2016-01-13 17:03:27 +00:00
2022-08-31 19:09:01 +00:00
use crate ::utils ;
2022-08-15 19:29:46 +00:00
fn common ( ) -> Command {
2022-08-31 14:29:00 +00:00
Command ::new ( " foo " ) . help_template ( utils ::FULL_TEMPLATE )
2016-01-13 17:03:27 +00:00
}
2022-08-15 19:29:46 +00:00
fn with_version ( ) -> Command {
2021-10-08 17:05:40 +00:00
common ( ) . version ( " 3.0 " )
2016-01-13 17:03:27 +00:00
}
2016-05-30 08:49:13 +00:00
2022-08-15 19:29:46 +00:00
fn with_long_version ( ) -> Command {
2021-10-08 17:05:40 +00:00
common ( ) . long_version ( " 3.0 (abcdefg) " )
2016-05-30 08:49:13 +00:00
}
2017-04-04 23:54:20 +00:00
2022-08-31 19:09:01 +00:00
fn with_both ( ) -> Command {
common ( ) . version ( " 3.0 " ) . long_version ( " 3.0 (abcdefg) " )
}
2022-08-15 19:29:46 +00:00
fn with_subcommand ( ) -> Command {
2023-03-25 04:22:23 +00:00
with_version ( ) . subcommand ( Command ::new ( " bar " ) . defer ( | cmd | cmd . subcommand ( Command ::new ( " baz " ) ) ) )
2021-02-07 11:58:43 +00:00
}
#[ test ]
2022-08-31 19:09:01 +00:00
fn version_short_flag_no_version ( ) {
2021-11-17 20:23:22 +00:00
let res = common ( ) . try_get_matches_from ( " foo -V " . split ( ' ' ) ) ;
2021-02-07 11:58:43 +00:00
2021-10-08 17:05:40 +00:00
assert! ( res . is_err ( ) ) ;
let err = res . unwrap_err ( ) ;
2024-05-04 19:59:40 +00:00
assert_eq! ( err . kind ( ) , ErrorKind ::UnknownArgument ) ;
2021-02-07 11:58:43 +00:00
}
#[ test ]
2022-08-31 19:09:01 +00:00
fn version_long_flag_no_version ( ) {
2021-11-17 20:23:22 +00:00
let res = common ( ) . try_get_matches_from ( " foo --version " . split ( ' ' ) ) ;
2021-02-07 11:58:43 +00:00
2021-10-08 17:05:40 +00:00
assert! ( res . is_err ( ) ) ;
let err = res . unwrap_err ( ) ;
2024-05-04 19:59:40 +00:00
assert_eq! ( err . kind ( ) , ErrorKind ::UnknownArgument ) ;
2021-02-07 11:58:43 +00:00
}
#[ test ]
2022-08-31 19:09:01 +00:00
fn version_short_flag_with_version ( ) {
2021-11-17 20:23:22 +00:00
let res = with_version ( ) . try_get_matches_from ( " foo -V " . split ( ' ' ) ) ;
2021-02-07 11:58:43 +00:00
2021-10-08 17:05:40 +00:00
assert! ( res . is_err ( ) ) ;
let err = res . unwrap_err ( ) ;
2022-01-25 22:19:28 +00:00
assert_eq! ( err . kind ( ) , ErrorKind ::DisplayVersion ) ;
2021-10-08 17:05:40 +00:00
assert_eq! ( err . to_string ( ) , " foo 3.0 \n " ) ;
2021-02-07 11:58:43 +00:00
}
2021-10-08 17:05:40 +00:00
#[ test ]
2022-08-31 19:09:01 +00:00
fn version_long_flag_with_version ( ) {
2021-11-17 20:23:22 +00:00
let res = with_version ( ) . try_get_matches_from ( " foo --version " . split ( ' ' ) ) ;
2020-10-26 08:37:45 +00:00
2021-10-08 17:05:40 +00:00
assert! ( res . is_err ( ) ) ;
let err = res . unwrap_err ( ) ;
2022-01-25 22:19:28 +00:00
assert_eq! ( err . kind ( ) , ErrorKind ::DisplayVersion ) ;
2021-10-08 17:05:40 +00:00
assert_eq! ( err . to_string ( ) , " foo 3.0 \n " ) ;
}
2020-10-26 08:37:45 +00:00
2017-04-04 23:54:20 +00:00
#[ test ]
2022-08-31 19:09:01 +00:00
fn version_short_flag_with_long_version ( ) {
2021-11-17 20:23:22 +00:00
let res = with_long_version ( ) . try_get_matches_from ( " foo -V " . split ( ' ' ) ) ;
2020-10-26 08:37:45 +00:00
2021-10-08 17:05:40 +00:00
assert! ( res . is_err ( ) ) ;
let err = res . unwrap_err ( ) ;
2022-01-25 22:19:28 +00:00
assert_eq! ( err . kind ( ) , ErrorKind ::DisplayVersion ) ;
2021-10-08 17:05:40 +00:00
assert_eq! ( err . to_string ( ) , " foo 3.0 (abcdefg) \n " ) ;
}
2020-10-26 08:37:45 +00:00
2021-10-08 17:05:40 +00:00
#[ test ]
2022-08-31 19:09:01 +00:00
fn version_long_flag_with_long_version ( ) {
2021-11-17 20:23:22 +00:00
let res = with_long_version ( ) . try_get_matches_from ( " foo --version " . split ( ' ' ) ) ;
2020-10-26 08:37:45 +00:00
2021-10-08 17:05:40 +00:00
assert! ( res . is_err ( ) ) ;
let err = res . unwrap_err ( ) ;
2022-01-25 22:19:28 +00:00
assert_eq! ( err . kind ( ) , ErrorKind ::DisplayVersion ) ;
2021-10-08 17:05:40 +00:00
assert_eq! ( err . to_string ( ) , " foo 3.0 (abcdefg) \n " ) ;
2020-10-26 08:37:45 +00:00
}
2022-08-31 19:09:01 +00:00
#[ test ]
fn version_short_flag_with_both ( ) {
let res = with_both ( ) . try_get_matches_from ( " foo -V " . split ( ' ' ) ) ;
assert! ( res . is_err ( ) ) ;
let err = res . unwrap_err ( ) ;
assert_eq! ( err . kind ( ) , ErrorKind ::DisplayVersion ) ;
assert_eq! ( err . to_string ( ) , " foo 3.0 \n " ) ;
}
#[ test ]
fn version_long_flag_with_both ( ) {
let res = with_both ( ) . try_get_matches_from ( " foo --version " . split ( ' ' ) ) ;
assert! ( res . is_err ( ) ) ;
let err = res . unwrap_err ( ) ;
assert_eq! ( err . kind ( ) , ErrorKind ::DisplayVersion ) ;
assert_eq! ( err . to_string ( ) , " foo 3.0 (abcdefg) \n " ) ;
}
#[ test ]
fn help_short_flag_no_version ( ) {
static EXPECTED : & str = " \
foo
2022-09-07 16:03:55 +00:00
Usage : foo
2022-08-31 19:09:01 +00:00
Options :
2023-01-03 16:49:43 +00:00
- h , - - help Print help
2022-08-31 19:09:01 +00:00
" ;
let cmd = common ( ) ;
utils ::assert_output ( cmd , " foo -h " , EXPECTED , false ) ;
}
#[ test ]
fn help_long_flag_no_version ( ) {
static EXPECTED : & str = " \
foo
2022-09-07 16:03:55 +00:00
Usage : foo
2022-08-31 19:09:01 +00:00
Options :
2023-01-03 16:49:43 +00:00
- h , - - help Print help
2022-08-31 19:09:01 +00:00
" ;
let cmd = common ( ) ;
utils ::assert_output ( cmd , " foo --help " , EXPECTED , false ) ;
}
#[ test ]
fn help_short_flag_with_version ( ) {
static EXPECTED : & str = " \
foo 3.0
2022-09-07 16:03:55 +00:00
Usage : foo
2022-08-31 19:09:01 +00:00
Options :
2023-01-03 16:49:43 +00:00
- h , - - help Print help
- V , - - version Print version
2022-08-31 19:09:01 +00:00
" ;
let cmd = with_version ( ) ;
utils ::assert_output ( cmd , " foo -h " , EXPECTED , false ) ;
}
#[ test ]
fn help_long_flag_with_version ( ) {
static EXPECTED : & str = " \
foo 3.0
2022-09-07 16:03:55 +00:00
Usage : foo
2022-08-31 19:09:01 +00:00
Options :
2023-01-03 16:49:43 +00:00
- h , - - help Print help
- V , - - version Print version
2022-08-31 19:09:01 +00:00
" ;
let cmd = with_version ( ) ;
utils ::assert_output ( cmd , " foo --help " , EXPECTED , false ) ;
}
#[ test ]
fn help_short_flag_with_long_version ( ) {
static EXPECTED : & str = " \
foo 3.0 ( abcdefg )
2022-09-07 16:03:55 +00:00
Usage : foo
2022-08-31 19:09:01 +00:00
Options :
2023-01-03 16:49:43 +00:00
- h , - - help Print help
- V , - - version Print version
2022-08-31 19:09:01 +00:00
" ;
let cmd = with_long_version ( ) ;
utils ::assert_output ( cmd , " foo -h " , EXPECTED , false ) ;
}
#[ test ]
fn help_long_flag_with_long_version ( ) {
static EXPECTED : & str = " \
foo 3.0 ( abcdefg )
2022-09-07 16:03:55 +00:00
Usage : foo
2022-08-31 19:09:01 +00:00
Options :
2023-01-03 16:49:43 +00:00
- h , - - help Print help
- V , - - version Print version
2022-08-31 19:09:01 +00:00
" ;
let cmd = with_long_version ( ) ;
utils ::assert_output ( cmd , " foo --help " , EXPECTED , false ) ;
}
#[ test ]
fn help_short_flag_with_both ( ) {
static EXPECTED : & str = " \
foo 3.0
2022-09-07 16:03:55 +00:00
Usage : foo
2022-08-31 19:09:01 +00:00
Options :
2023-01-03 16:49:43 +00:00
- h , - - help Print help
- V , - - version Print version
2022-08-31 19:09:01 +00:00
" ;
let cmd = with_both ( ) ;
utils ::assert_output ( cmd , " foo -h " , EXPECTED , false ) ;
}
#[ test ]
fn help_long_flag_with_both ( ) {
static EXPECTED : & str = " \
foo 3.0
2022-09-07 16:03:55 +00:00
Usage : foo
2022-08-31 19:09:01 +00:00
Options :
2023-01-03 16:49:43 +00:00
- h , - - help Print help
- V , - - version Print version
2022-08-31 19:09:01 +00:00
" ;
let cmd = with_both ( ) ;
utils ::assert_output ( cmd , " foo --help " , EXPECTED , false ) ;
}
2021-10-08 17:05:40 +00:00
#[ test ]
2022-08-08 21:08:47 +00:00
#[ cfg(debug_assertions) ]
#[ should_panic = " Command foo: Long option names must be unique for each argument, but '--version' is in use by both 'ver' and 'version' (call `cmd.disable_version_flag(true)` to remove the auto-generated `--version`) " ]
2021-10-08 17:05:40 +00:00
fn override_version_long_with_user_flag ( ) {
2022-08-08 21:08:47 +00:00
with_version ( )
. arg (
clap ::Arg ::new ( " ver " )
. long ( " version " )
. action ( ArgAction ::SetTrue ) ,
)
. debug_assert ( ) ;
2021-10-08 17:05:40 +00:00
}
2020-10-26 08:37:45 +00:00
2021-10-08 17:05:40 +00:00
#[ test ]
2022-08-08 21:08:47 +00:00
#[ cfg(debug_assertions) ]
#[ should_panic = " Command foo: Short option names must be unique for each argument, but '-V' is in use by both 'ver' and 'version' (call `cmd.disable_version_flag(true)` to remove the auto-generated `--version`) " ]
2021-10-08 17:05:40 +00:00
fn override_version_short_with_user_flag ( ) {
2022-08-08 21:08:47 +00:00
with_version ( )
. arg ( clap ::Arg ::new ( " ver " ) . short ( 'V' ) . action ( ArgAction ::SetTrue ) )
. debug_assert ( ) ;
2021-02-07 16:14:07 +00:00
}
2021-10-08 17:05:40 +00:00
#[ test ]
fn no_propagation_by_default_long ( ) {
// Version Flag should not be propagated to subcommands
2021-11-17 20:23:22 +00:00
let res = with_subcommand ( ) . try_get_matches_from ( " foo bar --version " . split ( ' ' ) ) ;
2021-02-07 16:14:07 +00:00
2021-10-08 17:05:40 +00:00
assert! ( res . is_err ( ) ) ;
let err = res . unwrap_err ( ) ;
2022-01-25 22:19:28 +00:00
assert_eq! ( err . kind ( ) , ErrorKind ::UnknownArgument ) ;
2021-10-08 17:05:40 +00:00
}
2021-02-07 16:14:07 +00:00
#[ test ]
2021-10-08 17:05:40 +00:00
fn no_propagation_by_default_short ( ) {
2021-11-17 20:23:22 +00:00
let res = with_subcommand ( ) . try_get_matches_from ( " foo bar -V " . split ( ' ' ) ) ;
2021-02-07 16:14:07 +00:00
2021-10-08 17:05:40 +00:00
assert! ( res . is_err ( ) ) ;
let err = res . unwrap_err ( ) ;
2022-01-25 22:19:28 +00:00
assert_eq! ( err . kind ( ) , ErrorKind ::UnknownArgument ) ;
2018-01-25 04:05:05 +00:00
}
2021-10-04 14:01:09 +00:00
#[ test ]
2021-10-08 17:05:40 +00:00
fn propagate_version_long ( ) {
let res = with_subcommand ( )
2022-02-10 17:51:40 +00:00
. propagate_version ( true )
2021-11-17 20:23:22 +00:00
. try_get_matches_from ( " foo bar --version " . split ( ' ' ) ) ;
2021-10-08 17:05:40 +00:00
2021-10-04 14:01:09 +00:00
assert! ( res . is_err ( ) ) ;
let err = res . unwrap_err ( ) ;
2022-01-25 22:19:28 +00:00
assert_eq! ( err . kind ( ) , ErrorKind ::DisplayVersion ) ;
2021-10-04 14:01:09 +00:00
}
#[ test ]
2021-10-08 17:05:40 +00:00
fn propagate_version_short ( ) {
let res = with_subcommand ( )
2022-02-10 17:51:40 +00:00
. propagate_version ( true )
2021-11-17 20:23:22 +00:00
. try_get_matches_from ( " foo bar -V " . split ( ' ' ) ) ;
2021-10-08 17:05:40 +00:00
2021-10-04 14:01:09 +00:00
assert! ( res . is_err ( ) ) ;
let err = res . unwrap_err ( ) ;
2022-01-25 22:19:28 +00:00
assert_eq! ( err . kind ( ) , ErrorKind ::DisplayVersion ) ;
2021-10-04 14:01:09 +00:00
}
2021-10-08 17:05:40 +00:00
#[ cfg(debug_assertions) ]
2021-10-04 14:01:09 +00:00
#[ test ]
2022-06-08 16:58:15 +00:00
#[ should_panic = " `ArgAction::Version` used without providing Command::version or Command::long_version " ]
2022-08-10 20:13:44 +00:00
fn version_required ( ) {
2021-10-08 17:05:40 +00:00
let _res = common ( )
2022-08-10 20:13:44 +00:00
. arg ( clap ::arg! ( - - version ) . action ( ArgAction ::Version ) )
2021-11-17 20:23:22 +00:00
. try_get_matches_from ( " foo -z " . split ( ' ' ) ) ;
2021-10-08 17:05:40 +00:00
}
#[ test ]
2022-09-16 16:54:02 +00:00
#[ should_panic = " Argument `version` is undefined " ]
2021-10-08 17:05:40 +00:00
fn mut_arg_version_no_auto_version ( ) {
2022-09-16 16:54:02 +00:00
let _ = common ( ) . mut_arg ( " version " , | v | v . short ( 'z' ) . action ( ArgAction ::SetTrue ) ) ;
2021-10-04 14:01:09 +00:00
}
2021-10-08 17:05:40 +00:00
#[ cfg(debug_assertions) ]
2021-10-04 14:01:09 +00:00
#[ test ]
2022-02-12 03:48:29 +00:00
#[ should_panic = " No version information via Command::version or Command::long_version to propagate " ]
2021-10-08 17:05:40 +00:00
fn propagate_version_no_version_info ( ) {
let _res = common ( )
2022-02-10 17:51:40 +00:00
. propagate_version ( true )
2022-02-12 03:48:29 +00:00
. subcommand ( Command ::new ( " bar " ) )
2021-11-17 20:23:22 +00:00
. try_get_matches_from ( " foo " . split ( ' ' ) ) ;
2021-10-04 14:01:09 +00:00
}