use super::utils; use clap::{arg, error::ErrorKind, Arg, ArgAction, ArgGroup, Command, PossibleValue}; fn setup() -> Command<'static> { Command::new("test") .author("Kevin K.") .about("tests stuff") .version("1.3") } fn empty_args() -> impl IntoIterator { std::iter::empty() } #[test] fn help_short() { let m = setup().try_get_matches_from(vec!["myprog", "-h"]); assert!(m.is_err()); assert_eq!(m.unwrap_err().kind(), ErrorKind::DisplayHelp); } #[test] fn help_long() { let m = setup().try_get_matches_from(vec!["myprog", "--help"]); assert!(m.is_err()); assert_eq!(m.unwrap_err().kind(), ErrorKind::DisplayHelp); } #[test] fn help_no_subcommand() { let m = setup().try_get_matches_from(vec!["myprog", "help"]); assert!(m.is_err()); assert_eq!(m.unwrap_err().kind(), ErrorKind::UnknownArgument); } #[test] fn help_subcommand() { let m = setup() .subcommand( Command::new("test") .about("tests things") .arg(arg!(-v --verbose "with verbosity")), ) .try_get_matches_from(vec!["myprog", "help"]); assert!(m.is_err()); assert_eq!(m.unwrap_err().kind(), ErrorKind::DisplayHelp); } #[test] fn help_multi_subcommand_error() { let cmd = Command::new("ctest").subcommand( Command::new("subcmd").subcommand( Command::new("multi") .about("tests subcommands") .author("Kevin K. ") .version("0.1") .arg(arg!( -f --flag "tests flags" )) .arg( arg!( -o --option "tests options" ) .required(false) .multiple_values(true) .action(ArgAction::Append), ), ), ); let err = cmd .try_get_matches_from(["ctest", "help", "subcmd", "multi", "foo"]) .unwrap_err(); static EXPECTED: &str = "error: The subcommand 'foo' wasn't recognized USAGE: ctest subcmd multi [OPTIONS] For more information try --help "; utils::assert_eq(EXPECTED, err.to_string()); } #[test] fn req_last_arg_usage() { static LAST_ARG_REQ_MULT: &str = "example 1.0 USAGE: example ... [--] ... ARGS: ... First ... Second OPTIONS: -h, --help Print help information -V, --version Print version information "; let cmd = Command::new("example") .version("1.0") .arg( Arg::new("FIRST") .help("First") .multiple_values(true) .required(true), ) .arg( Arg::new("SECOND") .help("Second") .multiple_values(true) .required(true) .last(true), ); utils::assert_output(cmd, "example --help", LAST_ARG_REQ_MULT, false); } #[test] fn args_with_last_usage() { static LAST_ARG_USAGE: &str = "flamegraph 0.1 USAGE: flamegraph [OPTIONS] [BINFILE] [-- ...] ARGS: The path of the binary to be profiled. for a binary. ... Any arguments you wish to pass to the being profiled. OPTIONS: -v, --verbose Prints out more stuff. -t, --timeout Timeout in seconds. -f, --frequency The sampling frequency. -h, --help Print help information -V, --version Print version information "; let cmd = Command::new("flamegraph") .version("0.1") .trailing_var_arg(true) .arg( Arg::new("verbose") .help("Prints out more stuff.") .short('v') .long("verbose") .action(ArgAction::SetTrue), ) .arg( Arg::new("timeout") .help("Timeout in seconds.") .short('t') .long("timeout") .value_name("SECONDS"), ) .arg( Arg::new("frequency") .help("The sampling frequency.") .short('f') .long("frequency") .value_name("HERTZ"), ) .arg( Arg::new("binary path") .help("The path of the binary to be profiled. for a binary.") .value_name("BINFILE"), ) .arg( Arg::new("pass through args") .help("Any arguments you wish to pass to the being profiled.") .action(ArgAction::Set) .multiple_values(true) .last(true) .value_name("ARGS"), ); utils::assert_output(cmd, "flamegraph --help", LAST_ARG_USAGE, false); } #[test] fn subcommand_short_help() { let m = utils::complex_app().try_get_matches_from(vec!["clap-test", "subcmd", "-h"]); assert!(m.is_err()); assert_eq!(m.unwrap_err().kind(), ErrorKind::DisplayHelp); } #[test] fn subcommand_long_help() { let m = utils::complex_app().try_get_matches_from(vec!["clap-test", "subcmd", "--help"]); assert!(m.is_err()); assert_eq!(m.unwrap_err().kind(), ErrorKind::DisplayHelp); } #[test] fn subcommand_help_rev() { let m = utils::complex_app().try_get_matches_from(vec!["clap-test", "help", "subcmd"]); assert!(m.is_err()); assert_eq!(m.unwrap_err().kind(), ErrorKind::DisplayHelp); } #[test] fn complex_help_output() { static HELP: &str = "clap-test v1.4.8 Kevin K. tests clap library USAGE: clap-test [OPTIONS] [ARGS] [SUBCOMMAND] ARGS: tests positionals tests positionals with exclusions ... tests specific values [possible values: vi, emacs] OPTIONS: -o, --option ... tests options -f, --flag tests flags -F tests flags with exclusions --long-option-2 tests long options with exclusions -O, --option3 specific vals [possible values: fast, slow] --multvals Tests multiple values, not mult occs --multvalsmo Tests multiple values, and mult occs --minvals2 ... Tests 2 min vals --maxvals3 ... Tests 3 max vals --optvaleq[=...] Tests optional value, require = sign --optvalnoeq [...] Tests optional value -h, --help Print help information -V, --version Print version information SUBCOMMANDS: subcmd tests subcommands help Print this message or the help of the given subcommand(s) "; utils::assert_output(utils::complex_app(), "clap-test --help", HELP, false); } #[test] fn after_and_before_help_output() { static AFTER_HELP: &str = "some text that comes before the help clap-test v1.4.8 tests clap library USAGE: clap-test OPTIONS: -h, --help Print help information -V, --version Print version information some text that comes after the help "; let cmd = Command::new("clap-test") .version("v1.4.8") .about("tests clap library") .before_help("some text that comes before the help") .after_help("some text that comes after the help"); utils::assert_output(cmd.clone(), "clap-test -h", AFTER_HELP, false); utils::assert_output(cmd, "clap-test --help", AFTER_HELP, false); } #[test] fn after_and_before_long_help_output() { static AFTER_HELP: &str = "some text that comes before the help clap-test v1.4.8 tests clap library USAGE: clap-test OPTIONS: -h, --help Print help information -V, --version Print version information some text that comes after the help "; static AFTER_LONG_HELP: &str = "some longer text that comes before the help clap-test v1.4.8 tests clap library USAGE: clap-test OPTIONS: -h, --help Print help information -V, --version Print version information some longer text that comes after the help "; let cmd = Command::new("clap-test") .version("v1.4.8") .about("tests clap library") .before_help("some text that comes before the help") .after_help("some text that comes after the help") .before_long_help("some longer text that comes before the help") .after_long_help("some longer text that comes after the help"); utils::assert_output(cmd.clone(), "clap-test --help", AFTER_LONG_HELP, false); utils::assert_output(cmd, "clap-test -h", AFTER_HELP, false); } static MULTI_SC_HELP: &str = "ctest-subcmd-multi 0.1 Kevin K. tests subcommands USAGE: ctest subcmd multi [OPTIONS] OPTIONS: -f, --flag tests flags -o, --option ... tests options -h, --help Print help information -V, --version Print version information "; #[test] fn multi_level_sc_help() { let cmd = Command::new("ctest").subcommand( Command::new("subcmd").subcommand( Command::new("multi") .about("tests subcommands") .author("Kevin K. ") .version("0.1") .arg(arg!( -f --flag "tests flags" )) .arg( arg!( -o --option "tests options" ) .required(false) .multiple_values(true) .action(ArgAction::Append), ), ), ); utils::assert_output(cmd, "ctest help subcmd multi", MULTI_SC_HELP, false); } #[test] fn no_wrap_help() { let cmd = Command::new("ctest") .term_width(0) .override_help(MULTI_SC_HELP); utils::assert_output(cmd, "ctest --help", &format!("{}\n", MULTI_SC_HELP), false); } #[test] fn no_wrap_default_help() { static DEFAULT_HELP: &str = "ctest 1.0 USAGE: ctest OPTIONS: -h, --help Print help information -V, --version Print version information "; let cmd = Command::new("ctest").version("1.0").term_width(0); utils::assert_output(cmd, "ctest --help", DEFAULT_HELP, false); } #[test] #[cfg(feature = "wrap_help")] fn wrapped_help() { static WRAPPED_HELP: &str = "test USAGE: test [OPTIONS] OPTIONS: -a, --all Also do versioning for private crates (will not be published) --exact Specify inter dependency version numbers exactly with `=` --no-git-commit Do not commit version changes --no-git-push Do not push generated commit and tags to git remote -h, --help Print help information "; let cmd = Command::new("test") .term_width(67) .arg( Arg::new("all") .short('a') .long("all") .help("Also do versioning for private crates (will not be published)"), ) .arg( Arg::new("exact") .long("exact") .help("Specify inter dependency version numbers exactly with `=`"), ) .arg( Arg::new("no_git_commit") .long("no-git-commit") .help("Do not commit version changes"), ) .arg( Arg::new("no_git_push") .long("no-git-push") .help("Do not push generated commit and tags to git remote"), ); utils::assert_output(cmd, "test --help", WRAPPED_HELP, false); } #[test] #[cfg(feature = "wrap_help")] fn unwrapped_help() { static UNWRAPPED_HELP: &str = "test USAGE: test [OPTIONS] OPTIONS: -a, --all Also do versioning for private crates (will not be published) --exact Specify inter dependency version numbers exactly with `=` --no-git-commit Do not commit version changes --no-git-push Do not push generated commit and tags to git remote -h, --help Print help information "; let cmd = Command::new("test") .term_width(68) .arg( Arg::new("all") .short('a') .long("all") .help("Also do versioning for private crates (will not be published)"), ) .arg( Arg::new("exact") .long("exact") .help("Specify inter dependency version numbers exactly with `=`"), ) .arg( Arg::new("no_git_commit") .long("no-git-commit") .help("Do not commit version changes"), ) .arg( Arg::new("no_git_push") .long("no-git-push") .help("Do not push generated commit and tags to git remote"), ); utils::assert_output(cmd, "test --help", UNWRAPPED_HELP, false); } #[test] #[cfg(all(feature = "wrap_help"))] fn possible_value_wrapped_help() { static WRAPPED_HELP: &str = "test USAGE: test [OPTIONS] OPTIONS: --possible-values Possible values: - short_name: Long enough help message, barely warrant wrapping - second: Short help gets handled the same --possible-values-with-new-line Possible values: - long enough name to trigger new line: Really long enough help message to clearly warrant wrapping believe me - second --possible-values-without-new-line Possible values: - name: Short enough help message with no wrapping - second: short help -h, --help Print help information "; let cmd = Command::new("test") .term_width(67) .arg( Arg::new("possible_values") .long("possible-values") .action(ArgAction::Set) .value_parser([ PossibleValue::new("short_name") .help("Long enough help message, barely warrant wrapping"), PossibleValue::new("second").help("Short help gets handled the same"), ]), ) .arg( Arg::new("possible_values_with_new_line") .long("possible-values-with-new-line") .action(ArgAction::Set) .value_parser([ PossibleValue::new("long enough name to trigger new line").help( "Really long enough help message to clearly warrant wrapping believe me", ), PossibleValue::new("second"), ]), ) .arg( Arg::new("possible_values_without_new_line") .long("possible-values-without-new-line") .action(ArgAction::Set) .value_parser([ PossibleValue::new("name").help("Short enough help message with no wrapping"), PossibleValue::new("second").help("short help"), ]), ); utils::assert_output(cmd, "test --help", WRAPPED_HELP, false); } #[test] fn complex_subcommand_help_output() { static SC_HELP: &str = "clap-test-subcmd 0.1 Kevin K. tests subcommands USAGE: clap-test subcmd [OPTIONS] [--] [scpositional] ARGS: tests positionals OPTIONS: -o, --option ... tests options -f, --flag tests flags -s, --subcmdarg tests other args -h, --help Print help information -V, --version Print version information "; let a = utils::complex_app(); utils::assert_output(a, "clap-test subcmd --help", SC_HELP, false); } #[test] fn issue_626_unicode_cutoff() { static ISSUE_626_CUTOFF: &str = "ctest 0.1 USAGE: ctest [OPTIONS] OPTIONS: -c, --cafe A coffeehouse, coffee shop, or café is an establishment which primarily serves hot coffee, related coffee beverages (e.g., café latte, cappuccino, espresso), tea, and other hot beverages. Some coffeehouses also serve cold beverages such as iced coffee and iced tea. Many cafés also serve some type of food, such as light snacks, muffins, or pastries. -h, --help Print help information -V, --version Print version information "; let cmd = Command::new("ctest").version("0.1").term_width(70).arg( Arg::new("cafe") .short('c') .long("cafe") .value_name("FILE") .help( "A coffeehouse, coffee shop, or café is an establishment \ which primarily serves hot coffee, related coffee beverages \ (e.g., café latte, cappuccino, espresso), tea, and other hot \ beverages. Some coffeehouses also serve cold beverages such as \ iced coffee and iced tea. Many cafés also serve some type of \ food, such as light snacks, muffins, or pastries.", ) .action(ArgAction::Set), ); utils::assert_output(cmd, "ctest --help", ISSUE_626_CUTOFF, false); } static HIDE_POS_VALS: &str = "ctest 0.1 USAGE: ctest [OPTIONS] OPTIONS: -p, --pos Some vals [possible values: fast, slow] -c, --cafe A coffeehouse, coffee shop, or café. -h, --help Print help information -V, --version Print version information "; #[test] fn hide_possible_vals() { let cmd = Command::new("ctest") .version("0.1") .arg( Arg::new("pos") .short('p') .long("pos") .value_name("VAL") .value_parser(["fast", "slow"]) .help("Some vals") .action(ArgAction::Set), ) .arg( Arg::new("cafe") .short('c') .long("cafe") .value_name("FILE") .hide_possible_values(true) .value_parser(["fast", "slow"]) .help("A coffeehouse, coffee shop, or café.") .action(ArgAction::Set), ); utils::assert_output(cmd, "ctest --help", HIDE_POS_VALS, false); } #[test] fn hide_single_possible_val() { let cmd = Command::new("ctest") .version("0.1") .arg( Arg::new("pos") .short('p') .long("pos") .value_name("VAL") .value_parser([ "fast".into(), "slow".into(), PossibleValue::new("secret speed").hide(true), ]) .help("Some vals") .action(ArgAction::Set), ) .arg( Arg::new("cafe") .short('c') .long("cafe") .value_name("FILE") .help("A coffeehouse, coffee shop, or café.") .action(ArgAction::Set), ); utils::assert_output(cmd, "ctest --help", HIDE_POS_VALS, false); } #[test] fn possible_vals_with_help() { static POS_VALS_HELP: &str = "ctest 0.1 USAGE: ctest [OPTIONS] OPTIONS: -p, --pos Some vals Possible values: - fast - slow: not as fast -c, --cafe A coffeehouse, coffee shop, or café. -h, --help Print help information -V, --version Print version information "; let app = Command::new("ctest") .version("0.1") .arg( Arg::new("pos") .short('p') .long("pos") .value_name("VAL") .value_parser([ PossibleValue::new("fast"), PossibleValue::new("slow").help("not as fast"), PossibleValue::new("secret speed").hide(true), ]) .help("Some vals") .action(ArgAction::Set), ) .arg( Arg::new("cafe") .short('c') .long("cafe") .value_name("FILE") .help("A coffeehouse, coffee shop, or café.") .action(ArgAction::Set), ); utils::assert_output(app, "ctest --help", POS_VALS_HELP, false); } #[test] fn issue_626_panic() { static ISSUE_626_PANIC: &str = "ctest 0.1 USAGE: ctest [OPTIONS] OPTIONS: -c, --cafe La culture du café est très développée dans de nombreux pays à climat chaud d\'Amérique, d\'Afrique et d\'Asie, dans des plantations qui sont cultivées pour les marchés d\'exportation. Le café est souvent une contribution majeure aux exportations des régions productrices. -h, --help Print help information -V, --version Print version information "; let cmd = Command::new("ctest") .version("0.1") .term_width(52) .arg(Arg::new("cafe") .short('c') .long("cafe") .value_name("FILE") .help("La culture du café est très développée dans de nombreux pays à climat chaud d'Amérique, \ d'Afrique et d'Asie, dans des plantations qui sont cultivées pour les marchés d'exportation. \ Le café est souvent une contribution majeure aux exportations des régions productrices.") .action(ArgAction::Set)); utils::assert_output(cmd, "ctest --help", ISSUE_626_PANIC, false); } #[test] fn issue_626_variable_panic() { for i in 10..320 { let _ = Command::new("ctest") .version("0.1") .term_width(i) .arg(Arg::new("cafe") .short('c') .long("cafe") .value_name("FILE") .help("La culture du café est très développée dans de nombreux pays à climat chaud d'Amérique, \ d'Afrique et d'Asie, dans des plantations qui sont cultivées pour les marchés d'exportation. \ Le café est souvent une contribution majeure aux exportations des régions productrices.") .action(ArgAction::Set)) .try_get_matches_from(vec!["ctest", "--help"]); } } #[test] fn final_word_wrapping() { static FINAL_WORD_WRAPPING: &str = "ctest 0.1 USAGE: ctest OPTIONS: -h, --help Print help information -V, --version Print version information "; let cmd = Command::new("ctest").version("0.1").term_width(24); utils::assert_output(cmd, "ctest --help", FINAL_WORD_WRAPPING, false); } static WRAPPING_NEWLINE_CHARS: &str = "ctest 0.1 USAGE: ctest [mode] ARGS: x, max, maximum 20 characters, contains symbols. l, long Copy-friendly, 14 characters, contains symbols. m, med, medium Copy-friendly, 8 characters, contains symbols. OPTIONS: -h, --help Print help information -V, --version Print version information "; #[test] fn wrapping_newline_chars() { let cmd = Command::new("ctest") .version("0.1") .term_width(60) .arg(Arg::new("mode").help( "x, max, maximum 20 characters, contains symbols.\n\ l, long Copy-friendly, 14 characters, contains symbols.\n\ m, med, medium Copy-friendly, 8 characters, contains symbols.\n", )); utils::assert_output(cmd, "ctest --help", WRAPPING_NEWLINE_CHARS, false); } #[test] fn wrapping_newline_variables() { let cmd = Command::new("ctest") .version("0.1") .term_width(60) .arg(Arg::new("mode").help( "x, max, maximum 20 characters, contains symbols.{n}\ l, long Copy-friendly, 14 characters, contains symbols.{n}\ m, med, medium Copy-friendly, 8 characters, contains symbols.{n}", )); utils::assert_output(cmd, "ctest --help", WRAPPING_NEWLINE_CHARS, false); } #[test] fn dont_wrap_urls() { let cmd = Command::new("Example") .term_width(30) .subcommand(Command::new("update").arg( Arg::new("force-non-host") .help("Install toolchains that require an emulator. See https://github.com/rust-lang/rustup/wiki/Non-host-toolchains") .long("force-non-host") .takes_value(false)) ); const EXPECTED: &str = "\ Example-update USAGE: Example update [OPTIONS] OPTIONS: --force-non-host Install toolchains that require an emulator. See https://github.com/rust-lang/rustup/wiki/Non-host-toolchains -h, --help Print help information "; utils::assert_output(cmd, "Example update --help", EXPECTED, false); } static OLD_NEWLINE_CHARS: &str = "ctest 0.1 USAGE: ctest [OPTIONS] OPTIONS: -m Some help with some wrapping (Defaults to something) -h, --help Print help information -V, --version Print version information "; #[test] fn old_newline_chars() { let cmd = Command::new("ctest").version("0.1").arg( Arg::new("mode") .short('m') .action(ArgAction::SetTrue) .help("Some help with some wrapping\n(Defaults to something)"), ); utils::assert_output(cmd, "ctest --help", OLD_NEWLINE_CHARS, false); } #[test] fn old_newline_variables() { let cmd = Command::new("ctest").version("0.1").arg( Arg::new("mode") .short('m') .action(ArgAction::SetTrue) .help("Some help with some wrapping{n}(Defaults to something)"), ); utils::assert_output(cmd, "ctest --help", OLD_NEWLINE_CHARS, false); } #[test] fn issue_688_hide_pos_vals() { static ISSUE_688: &str = "ctest 0.1 USAGE: ctest [OPTIONS] OPTIONS: --filter Sets the filter, or sampling method, to use for interpolation when resizing the particle images. The default is Linear (Bilinear). [possible values: Nearest, Linear, Cubic, Gaussian, Lanczos3] -h, --help Print help information -V, --version Print version information "; let filter_values = ["Nearest", "Linear", "Cubic", "Gaussian", "Lanczos3"]; let app1 = Command::new("ctest") .version("0.1") .term_width(120) .hide_possible_values(true) .arg(Arg::new("filter") .help("Sets the filter, or sampling method, to use for interpolation when resizing the particle \ images. The default is Linear (Bilinear). [possible values: Nearest, Linear, Cubic, Gaussian, Lanczos3]") .long("filter") .value_parser(filter_values) .action(ArgAction::Set)); utils::assert_output(app1, "ctest --help", ISSUE_688, false); let app2 = Command::new("ctest") .version("0.1") .term_width(120) .arg(Arg::new("filter") .help("Sets the filter, or sampling method, to use for interpolation when resizing the particle \ images. The default is Linear (Bilinear).") .long("filter") .value_parser(filter_values) .action(ArgAction::Set)); utils::assert_output(app2, "ctest --help", ISSUE_688, false); let app3 = Command::new("ctest") .version("0.1") .term_width(120) .arg(Arg::new("filter") .help("Sets the filter, or sampling method, to use for interpolation when resizing the particle \ images. The default is Linear (Bilinear). [possible values: Nearest, Linear, Cubic, Gaussian, Lanczos3]") .long("filter") .action(ArgAction::Set)); utils::assert_output(app3, "ctest --help", ISSUE_688, false); } #[test] fn issue_702_multiple_values() { static ISSUE_702: &str = "myapp 1.0 foo bar USAGE: myapp [OPTIONS] [--] [ARGS] ARGS: some option ... some option OPTIONS: -s, --some some option -o, --other some other option -l, --label