2020-07-09 01:37:56 +00:00
mod utils ;
2020-07-08 04:11:28 +00:00
use clap ::{ App , AppSettings , Arg , ErrorKind } ;
2020-06-13 15:32:24 +00:00
#[ test ]
fn flag_subcommand_normal ( ) {
let matches = App ::new ( " test " )
. subcommand (
2020-06-16 01:52:36 +00:00
App ::new ( " some " ) . short_flag ( 'S' ) . long_flag ( " some " ) . arg (
2020-06-13 15:32:24 +00:00
Arg ::new ( " test " )
. short ( 't' )
. long ( " test " )
. about ( " testing testing " ) ,
) ,
)
. get_matches_from ( vec! [ " myprog " , " some " , " --test " ] ) ;
assert_eq! ( matches . subcommand_name ( ) . unwrap ( ) , " some " ) ;
let sub_matches = matches . subcommand_matches ( " some " ) . unwrap ( ) ;
assert! ( sub_matches . is_present ( " test " ) ) ;
}
#[ test ]
fn flag_subcommand_normal_with_alias ( ) {
let matches = App ::new ( " test " )
. subcommand (
2020-06-16 01:52:36 +00:00
App ::new ( " some " )
. short_flag ( 'S' )
. long_flag ( " S " )
2020-06-13 15:32:24 +00:00
. arg (
Arg ::new ( " test " )
. short ( 't' )
. long ( " test " )
. about ( " testing testing " ) ,
)
. alias ( " result " ) ,
)
. get_matches_from ( vec! [ " myprog " , " result " , " --test " ] ) ;
assert_eq! ( matches . subcommand_name ( ) . unwrap ( ) , " some " ) ;
let sub_matches = matches . subcommand_matches ( " some " ) . unwrap ( ) ;
assert! ( sub_matches . is_present ( " test " ) ) ;
}
#[ test ]
fn flag_subcommand_short ( ) {
let matches = App ::new ( " test " )
. subcommand (
2020-06-16 01:52:36 +00:00
App ::new ( " some " ) . short_flag ( 'S' ) . arg (
2020-06-13 15:32:24 +00:00
Arg ::new ( " test " )
. short ( 't' )
. long ( " test " )
. about ( " testing testing " ) ,
) ,
)
. get_matches_from ( vec! [ " myprog " , " -S " , " --test " ] ) ;
assert_eq! ( matches . subcommand_name ( ) . unwrap ( ) , " some " ) ;
let sub_matches = matches . subcommand_matches ( " some " ) . unwrap ( ) ;
assert! ( sub_matches . is_present ( " test " ) ) ;
}
#[ test ]
fn flag_subcommand_short_with_args ( ) {
let matches = App ::new ( " test " )
. subcommand (
2020-06-16 01:52:36 +00:00
App ::new ( " some " ) . short_flag ( 'S' ) . arg (
2020-06-13 15:32:24 +00:00
Arg ::new ( " test " )
. short ( 't' )
. long ( " test " )
. about ( " testing testing " ) ,
) ,
)
. get_matches_from ( vec! [ " myprog " , " -St " ] ) ;
assert_eq! ( matches . subcommand_name ( ) . unwrap ( ) , " some " ) ;
let sub_matches = matches . subcommand_matches ( " some " ) . unwrap ( ) ;
assert! ( sub_matches . is_present ( " test " ) ) ;
}
#[ test ]
fn flag_subcommand_short_with_alias ( ) {
let matches = App ::new ( " test " )
. subcommand (
2020-06-16 01:52:36 +00:00
App ::new ( " some " )
. short_flag ( 'S' )
2020-06-13 15:32:24 +00:00
. arg (
Arg ::new ( " test " )
. short ( 't' )
. long ( " test " )
. about ( " testing testing " ) ,
)
2020-06-16 04:15:47 +00:00
. short_flag_alias ( 'M' )
. short_flag_alias ( 'B' ) ,
2020-06-13 15:32:24 +00:00
)
. get_matches_from ( vec! [ " myprog " , " -Bt " ] ) ;
assert_eq! ( matches . subcommand_name ( ) . unwrap ( ) , " some " ) ;
let sub_matches = matches . subcommand_matches ( " some " ) . unwrap ( ) ;
assert! ( sub_matches . is_present ( " test " ) ) ;
}
2020-07-11 18:25:47 +00:00
#[ test ]
fn flag_subcommand_short_with_alias_same_as_short_flag ( ) {
let matches = App ::new ( " test " )
. subcommand ( App ::new ( " some " ) . short_flag ( 'S' ) . short_flag_alias ( 'S' ) )
. get_matches_from ( vec! [ " myprog " , " -S " ] ) ;
assert_eq! ( matches . subcommand_name ( ) . unwrap ( ) , " some " ) ;
}
#[ test ]
fn flag_subcommand_long_with_alias_same_as_long_flag ( ) {
let matches = App ::new ( " test " )
. subcommand ( App ::new ( " some " ) . long_flag ( " sync " ) . long_flag_alias ( " sync " ) )
. get_matches_from ( vec! [ " myprog " , " --sync " ] ) ;
assert_eq! ( matches . subcommand_name ( ) . unwrap ( ) , " some " ) ;
}
2020-06-16 04:15:47 +00:00
#[ test ]
fn flag_subcommand_short_with_aliases_vis_and_hidden ( ) {
let app = App ::new ( " test " ) . subcommand (
App ::new ( " some " )
. short_flag ( 'S' )
. arg (
Arg ::new ( " test " )
. short ( 't' )
. long ( " test " )
. about ( " testing testing " ) ,
)
. visible_short_flag_aliases ( & [ 'M' , 'B' ] )
. short_flag_alias ( 'C' ) ,
) ;
let app1 = app . clone ( ) ;
let matches1 = app1 . get_matches_from ( vec! [ " test " , " -M " ] ) ;
assert_eq! ( matches1 . subcommand_name ( ) . unwrap ( ) , " some " ) ;
let app2 = app . clone ( ) ;
let matches2 = app2 . get_matches_from ( vec! [ " test " , " -C " ] ) ;
assert_eq! ( matches2 . subcommand_name ( ) . unwrap ( ) , " some " ) ;
let app3 = app . clone ( ) ;
let matches3 = app3 . get_matches_from ( vec! [ " test " , " -B " ] ) ;
assert_eq! ( matches3 . subcommand_name ( ) . unwrap ( ) , " some " ) ;
}
#[ test ]
fn flag_subcommand_short_with_aliases ( ) {
let matches = App ::new ( " test " )
. subcommand (
App ::new ( " some " )
. short_flag ( 'S' )
. arg (
Arg ::new ( " test " )
. short ( 't' )
. long ( " test " )
. about ( " testing testing " ) ,
)
. short_flag_aliases ( & [ 'M' , 'B' ] ) ,
)
. get_matches_from ( vec! [ " myprog " , " -Bt " ] ) ;
assert_eq! ( matches . subcommand_name ( ) . unwrap ( ) , " some " ) ;
let sub_matches = matches . subcommand_matches ( " some " ) . unwrap ( ) ;
assert! ( sub_matches . is_present ( " test " ) ) ;
}
#[ test ]
#[ should_panic ]
fn flag_subcommand_short_with_alias_hyphen ( ) {
let _ = App ::new ( " test " )
. subcommand (
App ::new ( " some " )
. short_flag ( 'S' )
. arg (
Arg ::new ( " test " )
. short ( 't' )
. long ( " test " )
. about ( " testing testing " ) ,
)
. short_flag_alias ( '-' ) ,
)
. get_matches_from ( vec! [ " myprog " , " -Bt " ] ) ;
}
#[ test ]
#[ should_panic ]
fn flag_subcommand_short_with_aliases_hyphen ( ) {
let _ = App ::new ( " test " )
. subcommand (
App ::new ( " some " )
. short_flag ( 'S' )
. arg (
Arg ::new ( " test " )
. short ( 't' )
. long ( " test " )
. about ( " testing testing " ) ,
)
. short_flag_aliases ( & [ '-' , '-' , '-' ] ) ,
)
. get_matches_from ( vec! [ " myprog " , " -Bt " ] ) ;
}
2020-06-13 15:32:24 +00:00
#[ test ]
fn flag_subcommand_long ( ) {
let matches = App ::new ( " test " )
. subcommand (
2020-06-16 01:52:36 +00:00
App ::new ( " some " ) . long_flag ( " some " ) . arg (
2020-06-13 15:32:24 +00:00
Arg ::new ( " test " )
. short ( 't' )
. long ( " test " )
. about ( " testing testing " ) ,
) ,
)
. get_matches_from ( vec! [ " myprog " , " --some " , " --test " ] ) ;
assert_eq! ( matches . subcommand_name ( ) . unwrap ( ) , " some " ) ;
let sub_matches = matches . subcommand_matches ( " some " ) . unwrap ( ) ;
assert! ( sub_matches . is_present ( " test " ) ) ;
}
#[ test ]
fn flag_subcommand_long_with_alias ( ) {
let matches = App ::new ( " test " )
. subcommand (
2020-06-16 01:52:36 +00:00
App ::new ( " some " )
. long_flag ( " some " )
2020-06-13 15:32:24 +00:00
. arg (
Arg ::new ( " test " )
. short ( 't' )
. long ( " test " )
. about ( " testing testing " ) ,
)
2020-07-10 13:27:14 +00:00
. long_flag_alias ( " result " ) ,
)
. get_matches_from ( vec! [ " myprog " , " --result " , " --test " ] ) ;
assert_eq! ( matches . subcommand_name ( ) . unwrap ( ) , " some " ) ;
let sub_matches = matches . subcommand_matches ( " some " ) . unwrap ( ) ;
assert! ( sub_matches . is_present ( " test " ) ) ;
}
#[ test ]
fn flag_subcommand_long_with_aliases ( ) {
let matches = App ::new ( " test " )
. subcommand (
App ::new ( " some " )
. long_flag ( " some " )
. arg (
Arg ::new ( " test " )
. short ( 't' )
. long ( " test " )
. about ( " testing testing " ) ,
)
. long_flag_aliases ( & [ " result " , " someall " ] ) ,
2020-06-13 15:32:24 +00:00
)
. get_matches_from ( vec! [ " myprog " , " --result " , " --test " ] ) ;
assert_eq! ( matches . subcommand_name ( ) . unwrap ( ) , " some " ) ;
let sub_matches = matches . subcommand_matches ( " some " ) . unwrap ( ) ;
assert! ( sub_matches . is_present ( " test " ) ) ;
}
#[ test ]
fn flag_subcommand_multiple ( ) {
let matches = App ::new ( " test " )
. subcommand (
2020-06-16 01:52:36 +00:00
App ::new ( " some " )
. short_flag ( 'S' )
. long_flag ( " some " )
2020-06-13 15:32:24 +00:00
. arg ( Arg ::from ( " -f, --flag 'some flag' " ) )
. arg ( Arg ::from ( " -p, --print 'print something' " ) )
. subcommand (
2020-06-16 01:52:36 +00:00
App ::new ( " result " )
. short_flag ( 'R' )
. long_flag ( " result " )
2020-06-13 15:32:24 +00:00
. arg ( Arg ::from ( " -f, --flag 'some flag' " ) )
. arg ( Arg ::from ( " -p, --print 'print something' " ) ) ,
) ,
)
. get_matches_from ( vec! [ " myprog " , " -SfpRfp " ] ) ;
assert_eq! ( matches . subcommand_name ( ) . unwrap ( ) , " some " ) ;
let sub_matches = matches . subcommand_matches ( " some " ) . unwrap ( ) ;
assert! ( sub_matches . is_present ( " flag " ) ) ;
assert! ( sub_matches . is_present ( " print " ) ) ;
assert_eq! ( sub_matches . subcommand_name ( ) . unwrap ( ) , " result " ) ;
let result_matches = sub_matches . subcommand_matches ( " result " ) . unwrap ( ) ;
assert! ( result_matches . is_present ( " flag " ) ) ;
assert! ( result_matches . is_present ( " print " ) ) ;
}
2020-06-16 04:15:47 +00:00
2020-06-17 05:53:53 +00:00
#[ cfg(debug_assertions) ]
2020-06-17 03:45:21 +00:00
#[ test ]
#[ should_panic = " Short option names must be unique for each argument, but \' -f \' is used by both an App named \' some \' and an Arg named \' test \' " ]
fn flag_subcommand_short_conflict_with_arg ( ) {
let _ = App ::new ( " test " )
. subcommand ( App ::new ( " some " ) . short_flag ( 'f' ) . long_flag ( " some " ) )
. arg ( Arg ::new ( " test " ) . short ( 'f' ) )
2020-06-17 05:14:48 +00:00
. get_matches_from ( vec! [ " myprog " , " -f " ] ) ;
2020-06-17 03:45:21 +00:00
}
2020-06-16 04:15:47 +00:00
2020-07-11 18:25:47 +00:00
#[ cfg(debug_assertions) ]
#[ test ]
#[ should_panic = " Short option names must be unique for each argument, but \' -f \' is used by both an App named \' some \' and an App named \' result \' " ]
fn flag_subcommand_short_conflict_with_alias ( ) {
let _ = App ::new ( " test " )
. subcommand ( App ::new ( " some " ) . short_flag ( 'f' ) . long_flag ( " some " ) )
. subcommand ( App ::new ( " result " ) . short_flag ( 't' ) . short_flag_alias ( 'f' ) )
. get_matches_from ( vec! [ " myprog " , " -f " ] ) ;
}
#[ cfg(debug_assertions) ]
#[ test ]
#[ should_panic = " Long option names must be unique for each argument, but \' --flag \' is used by both an App named \' some \' and an App named \' result \' " ]
fn flag_subcommand_long_conflict_with_alias ( ) {
let _ = App ::new ( " test " )
. subcommand ( App ::new ( " some " ) . long_flag ( " flag " ) )
. subcommand ( App ::new ( " result " ) . long_flag ( " test " ) . long_flag_alias ( " flag " ) )
. get_matches_from ( vec! [ " myprog " , " --flag " ] ) ;
}
#[ cfg(debug_assertions) ]
#[ test ]
#[ should_panic = " Short option names must be unique for each argument, but \' -f \' is used by both an App named \' some \' and an Arg named \' test \' " ]
fn flag_subcommand_short_conflict_with_arg_alias ( ) {
let _ = App ::new ( " test " )
. subcommand ( App ::new ( " some " ) . short_flag ( 'f' ) . long_flag ( " some " ) )
. arg ( Arg ::new ( " test " ) . short ( 't' ) . short_alias ( 'f' ) )
. get_matches_from ( vec! [ " myprog " , " -f " ] ) ;
}
#[ cfg(debug_assertions) ]
#[ test ]
#[ should_panic = " Long option names must be unique for each argument, but \' --some \' is used by both an App named \' some \' and an Arg named \' test \' " ]
fn flag_subcommand_long_conflict_with_arg_alias ( ) {
let _ = App ::new ( " test " )
. subcommand ( App ::new ( " some " ) . short_flag ( 'f' ) . long_flag ( " some " ) )
. arg ( Arg ::new ( " test " ) . long ( " test " ) . alias ( " some " ) )
. get_matches_from ( vec! [ " myprog " , " --some " ] ) ;
}
2020-06-17 05:53:53 +00:00
#[ cfg(debug_assertions) ]
2020-06-17 03:45:21 +00:00
#[ test ]
#[ should_panic = " Long option names must be unique for each argument, but \' --flag \' is used by both an App named \' some \' and an Arg named \' flag \' " ]
fn flag_subcommand_long_conflict_with_arg ( ) {
let _ = App ::new ( " test " )
. subcommand ( App ::new ( " some " ) . short_flag ( 'a' ) . long_flag ( " flag " ) )
. arg ( Arg ::new ( " flag " ) . long ( " flag " ) )
2020-06-17 04:56:05 +00:00
. get_matches_from ( vec! [ " myprog " , " --flag " ] ) ;
2020-06-17 03:45:21 +00:00
}
2020-06-16 04:15:47 +00:00
2020-06-17 03:45:21 +00:00
#[ test ]
fn flag_subcommand_conflict_with_help ( ) {
let _ = App ::new ( " test " )
. subcommand ( App ::new ( " help " ) . short_flag ( 'h' ) . long_flag ( " help " ) )
. get_matches_from ( vec! [ " myprog " , " --help " ] ) ;
}
2020-06-16 04:15:47 +00:00
2020-06-17 03:45:21 +00:00
#[ test ]
fn flag_subcommand_conflict_with_version ( ) {
let _ = App ::new ( " test " )
. subcommand ( App ::new ( " ver " ) . short_flag ( 'V' ) . long_flag ( " version " ) )
. get_matches_from ( vec! [ " myprog " , " --version " ] ) ;
}
2020-07-08 04:11:28 +00:00
#[ test ]
fn flag_subcommand_long_infer_pass ( ) {
let m = App ::new ( " prog " )
. setting ( AppSettings ::InferSubcommands )
. subcommand ( App ::new ( " test " ) . long_flag ( " test " ) )
. get_matches_from ( vec! [ " prog " , " --te " ] ) ;
assert_eq! ( m . subcommand_name ( ) , Some ( " test " ) ) ;
}
#[ cfg(not(feature = " suggestions " )) ]
#[ test ]
fn flag_subcommand_long_infer_fail ( ) {
let m = App ::new ( " prog " )
. setting ( AppSettings ::InferSubcommands )
. subcommand ( App ::new ( " test " ) . long_flag ( " test " ) )
. subcommand ( App ::new ( " temp " ) . long_flag ( " temp " ) )
. try_get_matches_from ( vec! [ " prog " , " --te " ] ) ;
assert! ( m . is_err ( ) , " {:#?} " , m . unwrap ( ) ) ;
assert_eq! ( m . unwrap_err ( ) . kind , ErrorKind ::UnknownArgument ) ;
}
#[ cfg(feature = " suggestions " ) ]
#[ test ]
fn flag_subcommand_long_infer_fail ( ) {
let m = App ::new ( " prog " )
. setting ( AppSettings ::InferSubcommands )
. subcommand ( App ::new ( " test " ) . long_flag ( " test " ) )
. subcommand ( App ::new ( " temp " ) . long_flag ( " temp " ) )
. try_get_matches_from ( vec! [ " prog " , " --te " ] ) ;
assert! ( m . is_err ( ) , " {:#?} " , m . unwrap ( ) ) ;
assert_eq! ( m . unwrap_err ( ) . kind , ErrorKind ::UnknownArgument ) ;
}
#[ test ]
2020-07-09 01:37:56 +00:00
fn flag_subcommand_long_infer_pass_close ( ) {
2020-07-08 04:11:28 +00:00
let m = App ::new ( " prog " )
. setting ( AppSettings ::InferSubcommands )
. subcommand ( App ::new ( " test " ) . long_flag ( " test " ) )
. subcommand ( App ::new ( " temp " ) . long_flag ( " temp " ) )
. get_matches_from ( vec! [ " prog " , " --tes " ] ) ;
assert_eq! ( m . subcommand_name ( ) , Some ( " test " ) ) ;
}
#[ test ]
2020-07-09 01:37:56 +00:00
fn flag_subcommand_long_infer_exact_match ( ) {
2020-07-08 04:11:28 +00:00
let m = App ::new ( " prog " )
. setting ( AppSettings ::InferSubcommands )
. subcommand ( App ::new ( " test " ) . long_flag ( " test " ) )
. subcommand ( App ::new ( " testa " ) . long_flag ( " testa " ) )
. subcommand ( App ::new ( " testb " ) . long_flag ( " testb " ) )
. get_matches_from ( vec! [ " prog " , " --test " ] ) ;
assert_eq! ( m . subcommand_name ( ) , Some ( " test " ) ) ;
}
2020-07-09 01:37:56 +00:00
static FLAG_SUBCOMMAND_HELP : & str = " pacman-query
Query the package database .
USAGE :
pacman { query , - - query , - Q } [ OPTIONS ]
FLAGS :
- h , - - help Prints help information
- V , - - version Prints version information
OPTIONS :
- i , - - info < info > .. . view package information
2020-07-18 17:47:04 +00:00
- s , - - search < search > .. . search locally installed packages for matching strings " ;
2020-07-09 01:37:56 +00:00
#[ test ]
fn flag_subcommand_long_short_normal_usage_string ( ) {
let app = App ::new ( " pacman " )
. about ( " package manager utility " )
. version ( " 5.2.1 " )
. setting ( AppSettings ::SubcommandRequiredElseHelp )
. author ( " Pacman Development Team " )
// Query subcommand
//
// Only a few of its arguments are implemented below.
. subcommand (
App ::new ( " query " )
. short_flag ( 'Q' )
. long_flag ( " query " )
. about ( " Query the package database. " )
. arg (
Arg ::new ( " search " )
. short ( 's' )
. long ( " search " )
2020-07-18 17:47:04 +00:00
. about ( " search locally installed packages for matching strings " )
2020-07-09 01:37:56 +00:00
. conflicts_with ( " info " )
. multiple_values ( true ) ,
)
. arg (
Arg ::new ( " info " )
. long ( " info " )
. short ( 'i' )
. conflicts_with ( " search " )
. about ( " view package information " )
. multiple_values ( true ) ,
) ,
) ;
assert! ( utils ::compare_output (
app ,
" pacman -Qh " ,
FLAG_SUBCOMMAND_HELP ,
false
) ) ;
}
static FLAG_SUBCOMMAND_NO_SHORT_HELP : & str = " pacman-query
Query the package database .
USAGE :
pacman { query , - - query } [ OPTIONS ]
FLAGS :
- h , - - help Prints help information
- V , - - version Prints version information
OPTIONS :
- i , - - info < info > .. . view package information
2020-07-18 17:47:04 +00:00
- s , - - search < search > .. . search locally installed packages for matching strings " ;
2020-07-09 01:37:56 +00:00
#[ test ]
fn flag_subcommand_long_normal_usage_string ( ) {
let app = App ::new ( " pacman " )
. about ( " package manager utility " )
. version ( " 5.2.1 " )
. setting ( AppSettings ::SubcommandRequiredElseHelp )
. author ( " Pacman Development Team " )
// Query subcommand
//
// Only a few of its arguments are implemented below.
. subcommand (
App ::new ( " query " )
. long_flag ( " query " )
. about ( " Query the package database. " )
. arg (
Arg ::new ( " search " )
. short ( 's' )
. long ( " search " )
2020-07-18 17:47:04 +00:00
. about ( " search locally installed packages for matching strings " )
2020-07-09 01:37:56 +00:00
. conflicts_with ( " info " )
. multiple_values ( true ) ,
)
. arg (
Arg ::new ( " info " )
. long ( " info " )
. short ( 'i' )
. conflicts_with ( " search " )
. about ( " view package information " )
. multiple_values ( true ) ,
) ,
) ;
assert! ( utils ::compare_output (
app ,
" pacman query --help " ,
FLAG_SUBCOMMAND_NO_SHORT_HELP ,
false
) ) ;
}
static FLAG_SUBCOMMAND_NO_LONG_HELP : & str = " pacman-query
Query the package database .
USAGE :
pacman { query , - Q } [ OPTIONS ]
FLAGS :
- h , - - help Prints help information
- V , - - version Prints version information
OPTIONS :
- i , - - info < info > .. . view package information
2020-07-18 17:47:04 +00:00
- s , - - search < search > .. . search locally installed packages for matching strings " ;
2020-07-09 01:37:56 +00:00
#[ test ]
fn flag_subcommand_short_normal_usage_string ( ) {
let app = App ::new ( " pacman " )
. about ( " package manager utility " )
. version ( " 5.2.1 " )
. setting ( AppSettings ::SubcommandRequiredElseHelp )
. author ( " Pacman Development Team " )
// Query subcommand
//
// Only a few of its arguments are implemented below.
. subcommand (
App ::new ( " query " )
. short_flag ( 'Q' )
. about ( " Query the package database. " )
. arg (
Arg ::new ( " search " )
. short ( 's' )
. long ( " search " )
2020-07-18 17:47:04 +00:00
. about ( " search locally installed packages for matching strings " )
2020-07-09 01:37:56 +00:00
. conflicts_with ( " info " )
. multiple_values ( true ) ,
)
. arg (
Arg ::new ( " info " )
. long ( " info " )
. short ( 'i' )
. conflicts_with ( " search " )
. about ( " view package information " )
. multiple_values ( true ) ,
) ,
) ;
assert! ( utils ::compare_output (
app ,
" pacman query --help " ,
FLAG_SUBCOMMAND_NO_LONG_HELP ,
false
) ) ;
}