clap/tests/builder/app_settings.rs

1213 lines
34 KiB
Rust
Raw Normal View History

use std::ffi::OsString;
use super::utils;
use clap::{arg, error::ErrorKind, Arg, ArgAction, Command};
static ALLOW_EXT_SC: &str = "\
Usage: clap-test [COMMAND]
Options:
-h, --help Print help information
-V, --version Print version information
";
static DONT_COLLAPSE_ARGS: &str = "\
Usage: clap-test [arg1] [arg2] [arg3]
2016-12-31 03:40:28 +00:00
Arguments:
[arg1] some
[arg2] some
[arg3] some
Options:
-h, --help Print help information
-V, --version Print version information
";
2016-12-31 03:40:28 +00:00
#[test]
fn sub_command_negate_required() {
2022-02-12 03:48:29 +00:00
Command::new("sub_command_negate")
.subcommand_negates_reqs(true)
.arg(Arg::new("test").required(true).index(1))
2022-02-12 03:48:29 +00:00
.subcommand(Command::new("sub1"))
.try_get_matches_from(vec!["myprog", "sub1"])
.unwrap();
}
#[test]
fn sub_command_negate_required_2() {
2022-02-12 03:48:29 +00:00
let result = Command::new("sub_command_negate")
.subcommand_negates_reqs(true)
.arg(Arg::new("test").required(true).index(1))
2022-02-12 03:48:29 +00:00
.subcommand(Command::new("sub1"))
.try_get_matches_from(vec![""]);
assert!(result.is_err());
let err = result.err().unwrap();
2022-01-25 22:19:28 +00:00
assert_eq!(err.kind(), ErrorKind::MissingRequiredArgument);
}
#[test]
fn sub_command_required() {
2022-02-12 03:48:29 +00:00
let result = Command::new("sc_required")
.subcommand_required(true)
2022-02-12 03:48:29 +00:00
.subcommand(Command::new("sub1"))
.try_get_matches_from(vec![""]);
assert!(result.is_err());
let err = result.err().unwrap();
2022-01-25 22:19:28 +00:00
assert_eq!(err.kind(), ErrorKind::MissingSubcommand);
}
#[test]
#[cfg(feature = "error-context")]
fn sub_command_required_error() {
static ERROR: &str = "\
error: 'sc_required' requires a subcommand but one was not provided
[subcommands: sub1, help]
Usage: sc_required <COMMAND>
For more information try '--help'
";
let cmd = Command::new("sc_required")
.subcommand_required(true)
.subcommand(Command::new("sub1"));
utils::assert_output(cmd, "sc_required", ERROR, true);
}
#[test]
fn arg_required_else_help() {
2022-02-12 03:48:29 +00:00
let result = Command::new("arg_required")
.arg_required_else_help(true)
.arg(Arg::new("test").index(1))
.try_get_matches_from(vec![""]);
assert!(result.is_err());
let err = result.err().unwrap();
assert_eq!(
2022-01-25 22:19:28 +00:00
err.kind(),
ErrorKind::DisplayHelpOnMissingArgumentOrSubcommand
);
}
#[test]
fn arg_required_else_help_over_req_arg() {
2022-02-12 03:48:29 +00:00
let result = Command::new("arg_required")
.arg_required_else_help(true)
.arg(Arg::new("test").index(1).required(true))
.try_get_matches_from(vec![""]);
assert!(result.is_err());
let err = result.err().unwrap();
assert_eq!(
2022-01-25 22:19:28 +00:00
err.kind(),
ErrorKind::DisplayHelpOnMissingArgumentOrSubcommand
);
}
#[test]
fn arg_required_else_help_over_req_subcommand() {
2022-02-12 03:48:29 +00:00
let result = Command::new("sub_required")
.arg_required_else_help(true)
.subcommand_required(true)
2022-02-12 03:48:29 +00:00
.subcommand(Command::new("sub1"))
.try_get_matches_from(vec![""]);
assert!(result.is_err());
let err = result.err().unwrap();
assert_eq!(
err.kind(),
ErrorKind::DisplayHelpOnMissingArgumentOrSubcommand
);
}
#[test]
fn arg_required_else_help_with_default() {
2022-02-12 03:48:29 +00:00
let result = Command::new("arg_required")
.arg_required_else_help(true)
.arg(arg!(--input <PATH>).default_value("-"))
.try_get_matches_from(vec![""]);
assert!(result.is_err());
let err = result.err().unwrap();
assert_eq!(
err.kind(),
ErrorKind::DisplayHelpOnMissingArgumentOrSubcommand
);
}
#[test]
fn arg_required_else_help_error_message() {
static ARG_REQUIRED_ELSE_HELP: &str = "\
Usage: test [OPTIONS]
Options:
-i, --info Provides more info
-h, --help Print help information
-V, --version Print version information
";
2022-02-14 21:47:20 +00:00
let cmd = Command::new("test")
.arg_required_else_help(true)
.version("1.0")
.arg(
Arg::new("info")
.help("Provides more info")
.short('i')
.long("info")
.action(ArgAction::SetTrue),
);
2022-04-29 20:32:25 +00:00
utils::assert_output(
2022-02-14 21:47:20 +00:00
cmd,
"test",
ARG_REQUIRED_ELSE_HELP,
2022-04-29 20:32:25 +00:00
true, // Unlike normal displaying of help, we should provide a fatal exit code
);
}
#[cfg(not(feature = "suggestions"))]
#[test]
fn infer_subcommands_fail_no_args() {
2022-02-12 03:48:29 +00:00
let m = Command::new("prog")
.infer_subcommands(true)
2022-02-12 03:48:29 +00:00
.subcommand(Command::new("test"))
.subcommand(Command::new("temp"))
.try_get_matches_from(vec!["prog", "te"]);
assert!(m.is_err(), "{:#?}", m.unwrap());
assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidSubcommand);
}
#[cfg(feature = "suggestions")]
#[test]
fn infer_subcommands_fail_no_args() {
2022-02-12 03:48:29 +00:00
let m = Command::new("prog")
.infer_subcommands(true)
2022-02-12 03:48:29 +00:00
.subcommand(Command::new("test"))
.subcommand(Command::new("temp"))
.try_get_matches_from(vec!["prog", "te"]);
assert!(m.is_err(), "{:#?}", m.unwrap());
2022-01-25 22:19:28 +00:00
assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidSubcommand);
}
#[test]
fn infer_subcommands_fail_with_args() {
2022-02-12 03:48:29 +00:00
let m = Command::new("prog")
.infer_subcommands(true)
.arg(Arg::new("some"))
2022-02-12 03:48:29 +00:00
.subcommand(Command::new("test"))
.subcommand(Command::new("temp"))
.try_get_matches_from(vec!["prog", "t"]);
2022-01-25 22:19:28 +00:00
assert!(m.is_ok(), "{:?}", m.unwrap_err().kind());
assert_eq!(
m.unwrap().get_one::<String>("some").map(|v| v.as_str()),
Some("t")
);
}
#[test]
fn infer_subcommands_fail_with_args2() {
2022-02-12 03:48:29 +00:00
let m = Command::new("prog")
.infer_subcommands(true)
.arg(Arg::new("some"))
2022-02-12 03:48:29 +00:00
.subcommand(Command::new("test"))
.subcommand(Command::new("temp"))
.try_get_matches_from(vec!["prog", "te"]);
2022-01-25 22:19:28 +00:00
assert!(m.is_ok(), "{:?}", m.unwrap_err().kind());
assert_eq!(
m.unwrap().get_one::<String>("some").map(|v| v.as_str()),
Some("te")
);
}
#[test]
fn infer_subcommands_pass() {
2022-02-12 03:48:29 +00:00
let m = Command::new("prog")
.infer_subcommands(true)
2022-02-12 03:48:29 +00:00
.subcommand(Command::new("test"))
.try_get_matches_from(vec!["prog", "te"])
.unwrap();
assert_eq!(m.subcommand_name(), Some("test"));
}
#[test]
fn infer_subcommands_pass_close() {
2022-02-12 03:48:29 +00:00
let m = Command::new("prog")
.infer_subcommands(true)
2022-02-12 03:48:29 +00:00
.subcommand(Command::new("test"))
.subcommand(Command::new("temp"))
.try_get_matches_from(vec!["prog", "tes"])
.unwrap();
assert_eq!(m.subcommand_name(), Some("test"));
}
#[test]
fn infer_subcommands_pass_exact_match() {
2022-02-12 03:48:29 +00:00
let m = Command::new("prog")
.infer_subcommands(true)
2022-02-12 03:48:29 +00:00
.subcommand(Command::new("test"))
.subcommand(Command::new("testa"))
.subcommand(Command::new("testb"))
.try_get_matches_from(vec!["prog", "test"])
.unwrap();
assert_eq!(m.subcommand_name(), Some("test"));
}
#[cfg(feature = "suggestions")]
#[test]
fn infer_subcommands_fail_suggestions() {
2022-02-12 03:48:29 +00:00
let m = Command::new("prog")
.infer_subcommands(true)
2022-02-12 03:48:29 +00:00
.subcommand(Command::new("test"))
.subcommand(Command::new("temp"))
.try_get_matches_from(vec!["prog", "temps"]);
assert!(m.is_err(), "{:#?}", m.unwrap());
2022-01-25 22:19:28 +00:00
assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidSubcommand);
}
#[cfg(not(feature = "suggestions"))]
#[test]
fn infer_subcommands_fail_suggestions() {
2022-02-12 03:48:29 +00:00
let m = Command::new("prog")
.infer_subcommands(true)
2022-02-12 03:48:29 +00:00
.subcommand(Command::new("test"))
.subcommand(Command::new("temp"))
.try_get_matches_from(vec!["prog", "temps"]);
assert!(m.is_err(), "{:#?}", m.unwrap());
assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidSubcommand);
}
#[test]
fn no_bin_name() {
2022-02-12 03:48:29 +00:00
let result = Command::new("arg_required")
.no_binary_name(true)
.arg(Arg::new("test").required(true).index(1))
.try_get_matches_from(vec!["testing"]);
2021-12-27 19:57:38 +00:00
assert!(result.is_ok(), "{}", result.unwrap_err());
let matches = result.unwrap();
assert_eq!(
matches
.get_one::<String>("test")
.map(|v| v.as_str())
.unwrap(),
"testing"
);
}
#[test]
fn skip_possible_values() {
static SKIP_POS_VALS: &str = "\
tests stuff
Usage: test [OPTIONS] [arg1]
Arguments:
[arg1] some pos arg
Options:
-o, --opt <opt> some option
-h, --help Print help information
-V, --version Print version information
";
2022-02-14 21:47:20 +00:00
let cmd = Command::new("test")
.author("Kevin K.")
.about("tests stuff")
.version("1.3")
.hide_possible_values(true)
2022-11-24 13:54:25 +00:00
.args([
arg!(-o --opt <opt> "some option").value_parser(["one", "two"]),
2022-05-24 01:16:02 +00:00
arg!([arg1] "some pos arg").value_parser(["three", "four"]),
2018-01-25 04:05:05 +00:00
]);
2022-04-29 20:32:25 +00:00
utils::assert_output(cmd, "test --help", SKIP_POS_VALS, false);
}
#[test]
fn stop_delim_values_only_pos_follows() {
2022-02-12 03:48:29 +00:00
let r = Command::new("onlypos")
.dont_delimit_trailing_values(true)
2022-11-24 13:54:25 +00:00
.args([arg!(f: -f <flag> "some opt"), arg!([arg] ... "some arg")])
.try_get_matches_from(vec!["", "--", "-f", "-g,x"]);
2021-12-27 19:57:38 +00:00
assert!(r.is_ok(), "{}", r.unwrap_err());
let m = r.unwrap();
assert!(m.contains_id("arg"));
assert!(!m.contains_id("f"));
2018-01-25 04:05:05 +00:00
assert_eq!(
m.get_many::<String>("arg")
.unwrap()
.map(|v| v.as_str())
.collect::<Vec<_>>(),
2022-11-24 13:54:25 +00:00
["-f", "-g,x"]
2018-01-25 04:05:05 +00:00
);
}
#[test]
fn dont_delim_values_trailingvararg() {
2022-02-12 03:48:29 +00:00
let m = Command::new("positional")
.dont_delimit_trailing_values(true)
.arg(arg!([opt] ... "some pos").trailing_var_arg(true))
.try_get_matches_from(vec!["", "test", "--foo", "-Wl,-bar"])
.unwrap();
assert!(m.contains_id("opt"));
2018-01-25 04:05:05 +00:00
assert_eq!(
m.get_many::<String>("opt")
.unwrap()
.map(|v| v.as_str())
.collect::<Vec<_>>(),
2022-11-24 13:54:25 +00:00
["test", "--foo", "-Wl,-bar"]
2018-01-25 04:05:05 +00:00
);
}
#[test]
fn delim_values_only_pos_follows() {
2022-02-12 03:48:29 +00:00
let r = Command::new("onlypos")
2022-11-24 13:54:25 +00:00
.args([arg!(f: -f [flag] "some opt"), arg!([arg] ... "some arg")])
.try_get_matches_from(vec!["", "--", "-f", "-g,x"]);
2021-12-27 19:57:38 +00:00
assert!(r.is_ok(), "{}", r.unwrap_err());
let m = r.unwrap();
assert!(m.contains_id("arg"));
assert!(!m.contains_id("f"));
2018-01-25 04:05:05 +00:00
assert_eq!(
m.get_many::<String>("arg")
.unwrap()
.map(|v| v.as_str())
.collect::<Vec<_>>(),
2022-11-24 13:54:25 +00:00
["-f", "-g,x"]
2018-01-25 04:05:05 +00:00
);
}
#[test]
fn delim_values_trailingvararg() {
2022-02-12 03:48:29 +00:00
let m = Command::new("positional")
.arg(arg!([opt] ... "some pos").trailing_var_arg(true))
.try_get_matches_from(vec!["", "test", "--foo", "-Wl,-bar"])
.unwrap();
assert!(m.contains_id("opt"));
2018-01-25 04:05:05 +00:00
assert_eq!(
m.get_many::<String>("opt")
.unwrap()
.map(|v| v.as_str())
.collect::<Vec<_>>(),
2022-11-24 13:54:25 +00:00
["test", "--foo", "-Wl,-bar"]
2018-01-25 04:05:05 +00:00
);
}
#[test]
fn delim_values_only_pos_follows_with_delim() {
2022-02-12 03:48:29 +00:00
let r = Command::new("onlypos")
2022-11-24 13:54:25 +00:00
.args([
2021-11-19 20:33:11 +00:00
arg!(f: -f [flag] "some opt"),
arg!([arg] ... "some arg").value_delimiter(','),
2018-01-25 04:05:05 +00:00
])
.try_get_matches_from(vec!["", "--", "-f", "-g,x"]);
2021-12-27 19:57:38 +00:00
assert!(r.is_ok(), "{}", r.unwrap_err());
let m = r.unwrap();
assert!(m.contains_id("arg"));
assert!(!m.contains_id("f"));
2018-01-25 04:05:05 +00:00
assert_eq!(
m.get_many::<String>("arg")
.unwrap()
.map(|v| v.as_str())
.collect::<Vec<_>>(),
2022-11-24 13:54:25 +00:00
["-f", "-g", "x"]
2018-01-25 04:05:05 +00:00
);
}
#[test]
fn delim_values_trailingvararg_with_delim() {
2022-02-12 03:48:29 +00:00
let m = Command::new("positional")
.arg(
arg!([opt] ... "some pos")
.value_delimiter(',')
.trailing_var_arg(true),
)
.try_get_matches_from(vec!["", "test", "--foo", "-Wl,-bar"])
.unwrap();
assert!(m.contains_id("opt"));
2018-01-25 04:05:05 +00:00
assert_eq!(
m.get_many::<String>("opt")
.unwrap()
.map(|v| v.as_str())
.collect::<Vec<_>>(),
2022-11-24 13:54:25 +00:00
["test", "--foo", "-Wl", "-bar"]
2018-01-25 04:05:05 +00:00
);
}
#[test]
fn leading_hyphen_short() {
2022-02-12 03:48:29 +00:00
let res = Command::new("leadhy")
.arg(Arg::new("some").allow_hyphen_values(true))
.arg(Arg::new("other").short('o').action(ArgAction::SetTrue))
.try_get_matches_from(vec!["", "-bar", "-o"]);
2022-01-25 22:19:28 +00:00
assert!(res.is_ok(), "Error: {:?}", res.unwrap_err().kind());
let m = res.unwrap();
assert!(m.contains_id("some"));
assert!(m.contains_id("other"));
assert_eq!(
m.get_one::<String>("some").map(|v| v.as_str()).unwrap(),
"-bar"
);
assert_eq!(
*m.get_one::<bool>("other").expect("defaulted by clap"),
true
);
}
#[test]
fn leading_hyphen_long() {
2022-02-12 03:48:29 +00:00
let res = Command::new("leadhy")
.arg(Arg::new("some").allow_hyphen_values(true))
.arg(Arg::new("other").short('o').action(ArgAction::SetTrue))
.try_get_matches_from(vec!["", "--bar", "-o"]);
2022-01-25 22:19:28 +00:00
assert!(res.is_ok(), "Error: {:?}", res.unwrap_err().kind());
let m = res.unwrap();
assert!(m.contains_id("some"));
assert!(m.contains_id("other"));
assert_eq!(
m.get_one::<String>("some").map(|v| v.as_str()).unwrap(),
"--bar"
);
assert_eq!(
*m.get_one::<bool>("other").expect("defaulted by clap"),
true
);
}
#[test]
fn leading_hyphen_opt() {
2022-02-12 03:48:29 +00:00
let res = Command::new("leadhy")
.arg(
Arg::new("some")
.action(ArgAction::Set)
.long("opt")
.allow_hyphen_values(true),
)
.arg(Arg::new("other").short('o').action(ArgAction::SetTrue))
.try_get_matches_from(vec!["", "--opt", "--bar", "-o"]);
2022-01-25 22:19:28 +00:00
assert!(res.is_ok(), "Error: {:?}", res.unwrap_err().kind());
let m = res.unwrap();
assert!(m.contains_id("some"));
assert!(m.contains_id("other"));
assert_eq!(
m.get_one::<String>("some").map(|v| v.as_str()).unwrap(),
"--bar"
);
assert_eq!(
*m.get_one::<bool>("other").expect("defaulted by clap"),
true
);
}
#[test]
fn allow_negative_numbers_success() {
2022-02-12 03:48:29 +00:00
let res = Command::new("negnum")
.arg(Arg::new("panum").allow_negative_numbers(true))
.arg(
Arg::new("onum")
.short('o')
.action(ArgAction::Set)
.allow_negative_numbers(true),
)
.try_get_matches_from(vec!["negnum", "-20", "-o", "-1.2"]);
2022-01-25 22:19:28 +00:00
assert!(res.is_ok(), "Error: {:?}", res.unwrap_err().kind());
let m = res.unwrap();
assert_eq!(
m.get_one::<String>("panum").map(|v| v.as_str()).unwrap(),
"-20"
);
assert_eq!(
m.get_one::<String>("onum").map(|v| v.as_str()).unwrap(),
"-1.2"
);
}
#[test]
fn allow_negative_numbers_fail() {
2022-02-12 03:48:29 +00:00
let res = Command::new("negnum")
.arg(Arg::new("panum").allow_negative_numbers(true))
.arg(
Arg::new("onum")
.short('o')
.action(ArgAction::Set)
.allow_negative_numbers(true),
)
.try_get_matches_from(vec!["negnum", "--foo", "-o", "-1.2"]);
assert!(res.is_err());
2022-01-25 22:19:28 +00:00
assert_eq!(res.unwrap_err().kind(), ErrorKind::UnknownArgument)
}
#[test]
fn trailing_var_arg_with_hyphen_values_escape_first() {
assert_trailing_var_args(&["test", "--", "foo", "bar"], Some(&["foo", "bar"]), false);
}
#[test]
fn trailing_var_arg_with_hyphen_values_escape_middle() {
assert_trailing_var_args(
&["test", "foo", "--", "bar"],
Some(&["foo", "--", "bar"]),
false,
2018-01-25 04:05:05 +00:00
);
}
#[test]
fn trailing_var_arg_with_hyphen_values_short_first() {
assert_trailing_var_args(&["test", "-p", "foo", "bar"], Some(&["foo", "bar"]), true);
}
#[test]
fn trailing_var_arg_with_hyphen_values_short_middle() {
assert_trailing_var_args(
&["test", "foo", "-p", "bar"],
Some(&["foo", "-p", "bar"]),
false,
);
}
#[test]
fn trailing_var_arg_with_hyphen_values_long_first() {
assert_trailing_var_args(
&["test", "--prog", "foo", "bar"],
Some(&["foo", "bar"]),
true,
);
}
#[test]
fn trailing_var_arg_with_hyphen_values_long_middle() {
assert_trailing_var_args(
&["test", "foo", "--prog", "bar"],
Some(&["foo", "--prog", "bar"]),
false,
);
}
#[track_caller]
fn assert_trailing_var_args(
input: &[&str],
expected_var_arg: Option<&[&str]>,
expected_flag: bool,
) {
let cmd = Command::new("test").arg(arg!(-p - -prog)).arg(
arg!([opt] ... "some pos")
.trailing_var_arg(true)
.allow_hyphen_values(true),
);
let m = cmd.try_get_matches_from(input);
assert!(
m.is_ok(),
"failed with args {:?}: {}",
input,
m.unwrap_err()
);
let m = m.unwrap();
let actual_var_args = m
.get_many::<String>("opt")
.map(|v| v.map(|s| s.as_str()).collect::<Vec<_>>());
assert_eq!(actual_var_args.as_deref(), expected_var_arg);
assert_eq!(m.get_flag("prog"), expected_flag);
}
#[test]
fn disable_help_subcommand() {
2022-02-12 03:48:29 +00:00
let result = Command::new("disablehelp")
.disable_help_subcommand(true)
2022-02-12 03:48:29 +00:00
.subcommand(Command::new("sub1"))
.try_get_matches_from(vec!["", "help"]);
assert!(result.is_err());
let err = result.err().unwrap();
assert_eq!(err.kind(), ErrorKind::InvalidSubcommand);
2016-12-31 03:40:28 +00:00
}
#[test]
fn dont_collapse_args() {
2022-11-24 13:54:25 +00:00
let cmd = Command::new("clap-test").version("v1.4.8").args([
Arg::new("arg1").help("some"),
Arg::new("arg2").help("some"),
Arg::new("arg3").help("some"),
]);
2022-04-29 20:32:25 +00:00
utils::assert_output(cmd, "clap-test --help", DONT_COLLAPSE_ARGS, false);
2016-12-31 04:22:09 +00:00
}
#[test]
fn require_eq() {
static REQUIRE_EQUALS: &str = "\
Usage: clap-test --opt=<FILE>
Options:
-o, --opt=<FILE> some
-h, --help Print help information
-V, --version Print version information
";
2022-02-14 21:47:20 +00:00
let cmd = Command::new("clap-test").version("v1.4.8").arg(
Arg::new("opt")
.long("opt")
.short('o')
.required(true)
.require_equals(true)
.value_name("FILE")
.help("some"),
2018-01-25 04:05:05 +00:00
);
2022-04-29 20:32:25 +00:00
utils::assert_output(cmd, "clap-test --help", REQUIRE_EQUALS, false);
}
#[test]
fn propagate_vals_down() {
2022-02-12 03:48:29 +00:00
let m = Command::new("myprog")
2021-11-19 20:33:11 +00:00
.arg(arg!([cmd] "command to run").global(true))
2022-02-12 03:48:29 +00:00
.subcommand(Command::new("foo"))
.try_get_matches_from(vec!["myprog", "set", "foo"]);
2022-01-25 22:19:28 +00:00
assert!(m.is_ok(), "{:?}", m.unwrap_err().kind());
let m = m.unwrap();
assert_eq!(m.get_one::<String>("cmd").map(|v| v.as_str()), Some("set"));
let sub_m = m.subcommand_matches("foo").unwrap();
assert_eq!(
sub_m.get_one::<String>("cmd").map(|v| v.as_str()),
Some("set")
);
}
#[test]
fn allow_missing_positional() {
2022-02-12 03:48:29 +00:00
let m = Command::new("test")
.allow_missing_positional(true)
2021-11-19 20:33:11 +00:00
.arg(arg!([src] "some file").default_value("src"))
.arg(arg!(<dest> "some file"))
.try_get_matches_from(vec!["test", "file"]);
2022-01-25 22:19:28 +00:00
assert!(m.is_ok(), "{:?}", m.unwrap_err().kind());
let m = m.unwrap();
assert_eq!(m.get_one::<String>("src").map(|v| v.as_str()), Some("src"));
assert_eq!(
m.get_one::<String>("dest").map(|v| v.as_str()),
Some("file")
);
}
#[test]
fn allow_missing_positional_no_default() {
2022-02-12 03:48:29 +00:00
let m = Command::new("test")
.allow_missing_positional(true)
2021-11-19 20:33:11 +00:00
.arg(arg!([src] "some file"))
.arg(arg!(<dest> "some file"))
.try_get_matches_from(vec!["test", "file"]);
2022-01-25 22:19:28 +00:00
assert!(m.is_ok(), "{:?}", m.unwrap_err().kind());
let m = m.unwrap();
assert_eq!(m.get_one::<String>("src").map(|v| v.as_str()), None);
assert_eq!(
m.get_one::<String>("dest").map(|v| v.as_str()),
Some("file")
);
}
#[test]
fn missing_positional_no_hyphen() {
2022-02-12 03:48:29 +00:00
let r = Command::new("bench")
.allow_missing_positional(true)
2021-11-19 20:33:11 +00:00
.arg(arg!([BENCH] "some bench"))
.arg(arg!([ARGS] ... "some args"))
.try_get_matches_from(vec!["bench", "foo", "arg1", "arg2", "arg3"]);
2022-01-25 22:19:28 +00:00
assert!(r.is_ok(), "{:?}", r.unwrap_err().kind());
let m = r.unwrap();
let expected_bench = Some("foo");
let expected_args = vec!["arg1", "arg2", "arg3"];
2018-08-02 03:13:51 +00:00
assert_eq!(
m.get_one::<String>("BENCH").map(|v| v.as_str()),
expected_bench
);
assert_eq!(
m.get_many::<String>("ARGS")
.unwrap()
.map(|v| v.as_str())
.collect::<Vec<_>>(),
2018-08-02 03:13:51 +00:00
&*expected_args
);
}
#[test]
fn missing_positional_hyphen() {
2022-02-12 03:48:29 +00:00
let r = Command::new("bench")
.allow_missing_positional(true)
2021-11-19 20:33:11 +00:00
.arg(arg!([BENCH] "some bench"))
.arg(arg!([ARGS] ... "some args"))
.try_get_matches_from(vec!["bench", "--", "arg1", "arg2", "arg3"]);
2022-01-25 22:19:28 +00:00
assert!(r.is_ok(), "{:?}", r.unwrap_err().kind());
let m = r.unwrap();
let expected_bench = None;
let expected_args = vec!["arg1", "arg2", "arg3"];
2018-08-02 03:13:51 +00:00
assert_eq!(
m.get_one::<String>("BENCH").map(|v| v.as_str()),
expected_bench
);
assert_eq!(
m.get_many::<String>("ARGS")
.unwrap()
.map(|v| v.as_str())
.collect::<Vec<_>>(),
2018-08-02 03:13:51 +00:00
&*expected_args
);
}
#[test]
fn missing_positional_hyphen_far_back() {
2022-02-12 03:48:29 +00:00
let r = Command::new("bench")
.allow_missing_positional(true)
2021-11-19 20:33:11 +00:00
.arg(arg!([BENCH1] "some bench"))
.arg(arg!([BENCH2] "some bench"))
.arg(arg!([BENCH3] "some bench"))
.arg(arg!([ARGS] ... "some args"))
.try_get_matches_from(vec!["bench", "foo", "--", "arg1", "arg2", "arg3"]);
2022-01-25 22:19:28 +00:00
assert!(r.is_ok(), "{:?}", r.unwrap_err().kind());
let m = r.unwrap();
let expected_bench1 = Some("foo");
let expected_bench2 = None;
let expected_bench3 = None;
let expected_args = vec!["arg1", "arg2", "arg3"];
2018-08-02 03:13:51 +00:00
assert_eq!(
m.get_one::<String>("BENCH1").map(|v| v.as_str()),
expected_bench1
);
assert_eq!(
m.get_one::<String>("BENCH2").map(|v| v.as_str()),
expected_bench2
);
assert_eq!(
m.get_one::<String>("BENCH3").map(|v| v.as_str()),
expected_bench3
);
assert_eq!(
m.get_many::<String>("ARGS")
.unwrap()
.map(|v| v.as_str())
.collect::<Vec<_>>(),
2018-08-02 03:13:51 +00:00
&*expected_args
);
}
#[test]
fn missing_positional_hyphen_req_error() {
2022-02-12 03:48:29 +00:00
let r = Command::new("bench")
.allow_missing_positional(true)
2021-11-19 20:33:11 +00:00
.arg(arg!([BENCH1] "some bench"))
.arg(arg!(<BENCH2> "some bench"))
.arg(arg!([ARGS] ... "some args"))
.try_get_matches_from(vec!["bench", "foo", "--", "arg1", "arg2", "arg3"]);
assert!(r.is_err());
2022-01-25 22:19:28 +00:00
assert_eq!(r.unwrap_err().kind(), ErrorKind::MissingRequiredArgument);
}
#[test]
fn issue_1066_allow_leading_hyphen_and_unknown_args_option() {
2022-02-12 03:48:29 +00:00
let res = Command::new("prog")
.arg(
arg!(--"some-argument" <val>)
.required(true)
.allow_hyphen_values(true),
)
.try_get_matches_from(vec!["prog", "-fish"]);
assert!(res.is_err());
2022-01-25 22:19:28 +00:00
assert_eq!(res.unwrap_err().kind(), ErrorKind::UnknownArgument);
}
#[test]
fn issue_1437_allow_hyphen_values_for_positional_arg() {
2022-02-12 03:48:29 +00:00
let m = Command::new("tmp")
.arg(
Arg::new("pat")
.allow_hyphen_values(true)
.required(true)
.action(ArgAction::Set),
)
.try_get_matches_from(["tmp", "-file"])
.unwrap();
assert_eq!(
m.get_one::<String>("pat").map(|v| v.as_str()),
Some("-file")
);
}
#[test]
fn issue_3880_allow_long_flag_hyphen_value_for_positional_arg() {
let m = Command::new("prog")
.arg(
Arg::new("pat")
.allow_hyphen_values(true)
.required(true)
.action(ArgAction::Set),
)
.try_get_matches_from(["", "--file"])
.unwrap();
assert_eq!(
m.get_one::<String>("pat").map(|v| v.as_str()),
Some("--file")
);
}
#[test]
fn issue_1093_allow_ext_sc() {
2022-02-14 21:47:20 +00:00
let cmd = Command::new("clap-test")
.version("v1.4.8")
.allow_external_subcommands(true);
2022-04-29 20:32:25 +00:00
utils::assert_output(cmd, "clap-test --help", ALLOW_EXT_SC, false);
2018-01-25 04:05:05 +00:00
}
#[test]
fn allow_ext_sc_empty_args() {
let res = Command::new("clap-test")
.version("v1.4.8")
.allow_external_subcommands(true)
.try_get_matches_from(vec!["clap-test", "external-cmd"]);
assert!(res.is_ok(), "{}", res.unwrap_err());
match res.unwrap().subcommand() {
Some((name, args)) => {
assert_eq!(name, "external-cmd");
2022-05-26 20:52:35 +00:00
assert_eq!(
args.get_many::<OsString>("").unwrap().collect::<Vec<_>>(),
Vec::<&OsString>::new(),
);
}
_ => unreachable!(),
}
}
#[test]
fn allow_ext_sc_when_sc_required() {
2022-02-12 03:48:29 +00:00
let res = Command::new("clap-test")
.version("v1.4.8")
.allow_external_subcommands(true)
.subcommand_required(true)
.try_get_matches_from(vec!["clap-test", "external-cmd", "foo"]);
2021-12-27 19:57:38 +00:00
assert!(res.is_ok(), "{}", res.unwrap_err());
match res.unwrap().subcommand() {
2020-08-05 12:36:02 +00:00
Some((name, args)) => {
assert_eq!(name, "external-cmd");
assert_eq!(
args.get_many::<OsString>("")
.unwrap()
.cloned()
.collect::<Vec<_>>(),
vec![OsString::from("foo")]
);
}
2020-05-12 13:34:05 +00:00
_ => unreachable!(),
}
}
#[test]
fn external_subcommand_looks_like_built_in() {
2022-02-12 03:48:29 +00:00
let res = Command::new("cargo")
.version("1.26.0")
.allow_external_subcommands(true)
2022-02-12 03:48:29 +00:00
.subcommand(Command::new("install"))
.try_get_matches_from(vec!["cargo", "install-update", "foo"]);
2021-12-27 19:57:38 +00:00
assert!(res.is_ok(), "{}", res.unwrap_err());
let m = res.unwrap();
match m.subcommand() {
2020-08-05 12:36:02 +00:00
Some((name, args)) => {
assert_eq!(name, "install-update");
assert_eq!(
args.get_many::<OsString>("")
.unwrap()
.cloned()
.collect::<Vec<_>>(),
vec![OsString::from("foo")]
);
}
2020-03-05 10:34:29 +00:00
_ => panic!("external_subcommand didn't work"),
}
}
#[test]
fn built_in_subcommand_escaped() {
let res = Command::new("cargo")
.version("1.26.0")
.allow_external_subcommands(true)
.subcommand(Command::new("install"))
.try_get_matches_from(vec!["cargo", "--", "install", "foo"]);
assert!(res.is_ok(), "{}", res.unwrap_err());
let m = res.unwrap();
match m.subcommand() {
Some((name, args)) => {
assert_eq!(name, "install");
assert_eq!(
args.get_many::<OsString>("")
.unwrap()
.cloned()
.collect::<Vec<_>>(),
vec![OsString::from("foo")]
);
}
_ => panic!("external_subcommand didn't work"),
}
}
#[test]
fn aaos_opts_w_other_overrides() {
// opts with other overrides
2022-02-12 03:48:29 +00:00
let res = Command::new("posix")
.args_override_self(true)
.arg(arg!(--opt <val> "some option").action(ArgAction::Set))
2021-11-19 20:33:11 +00:00
.arg(
arg!(--other <val> "some other option")
.overrides_with("opt")
.action(ArgAction::Set),
2021-11-19 20:33:11 +00:00
)
.try_get_matches_from(vec!["", "--opt=some", "--other=test", "--opt=other"]);
2021-12-27 19:57:38 +00:00
assert!(res.is_ok(), "{}", res.unwrap_err());
let m = res.unwrap();
assert!(m.contains_id("opt"));
assert!(!m.contains_id("other"));
assert_eq!(
m.get_one::<String>("opt").map(|v| v.as_str()),
Some("other")
);
}
#[test]
fn aaos_opts_w_other_overrides_rev() {
// opts with other overrides, rev
2022-02-12 03:48:29 +00:00
let res = Command::new("posix")
.args_override_self(true)
.arg(
arg!(--opt <val> "some option")
.required(true)
.action(ArgAction::Set),
)
2021-11-19 20:33:11 +00:00
.arg(
arg!(--other <val> "some other option")
.required(true)
.overrides_with("opt")
.action(ArgAction::Set),
2021-11-19 20:33:11 +00:00
)
.try_get_matches_from(vec!["", "--opt=some", "--opt=other", "--other=val"]);
2021-12-27 19:57:38 +00:00
assert!(res.is_ok(), "{}", res.unwrap_err());
let m = res.unwrap();
assert!(!m.contains_id("opt"));
assert!(m.contains_id("other"));
assert_eq!(
m.get_one::<String>("other").map(|v| v.as_str()),
Some("val")
);
}
#[test]
fn aaos_opts_w_other_overrides_2() {
// opts with other overrides
2022-02-12 03:48:29 +00:00
let res = Command::new("posix")
.args_override_self(true)
2021-11-19 20:33:11 +00:00
.arg(
arg!(--opt <val> "some option")
.overrides_with("other")
.action(ArgAction::Set),
)
.arg(arg!(--other <val> "some other option").action(ArgAction::Set))
.try_get_matches_from(vec!["", "--opt=some", "--other=test", "--opt=other"]);
2021-12-27 19:57:38 +00:00
assert!(res.is_ok(), "{}", res.unwrap_err());
let m = res.unwrap();
assert!(m.contains_id("opt"));
assert!(!m.contains_id("other"));
assert_eq!(
m.get_one::<String>("opt").map(|v| v.as_str()),
Some("other")
);
}
#[test]
fn aaos_opts_w_other_overrides_rev_2() {
// opts with other overrides, rev
2022-02-12 03:48:29 +00:00
let res = Command::new("posix")
.args_override_self(true)
2021-11-19 20:33:11 +00:00
.arg(
arg!(--opt <val> "some option")
.required(true)
.overrides_with("other")
.action(ArgAction::Set),
)
.arg(
arg!(--other <val> "some other option")
.required(true)
.action(ArgAction::Set),
2021-11-19 20:33:11 +00:00
)
.try_get_matches_from(vec!["", "--opt=some", "--opt=other", "--other=val"]);
2021-12-27 19:57:38 +00:00
assert!(res.is_ok(), "{}", res.unwrap_err());
let m = res.unwrap();
assert!(!m.contains_id("opt"));
assert!(m.contains_id("other"));
assert_eq!(
m.get_one::<String>("other").map(|v| v.as_str()),
Some("val")
);
}
#[test]
fn aaos_opts_w_override_as_conflict_1() {
// opts with other overrides, rev
let res = Command::new("posix")
.arg(
arg!(--opt <val> "some option")
.required(true)
.overrides_with("other")
.action(ArgAction::Set),
)
.arg(
arg!(--other <val> "some other option")
.required(true)
.action(ArgAction::Set),
)
.try_get_matches_from(vec!["", "--opt=some"]);
assert!(res.is_ok(), "{}", res.unwrap_err());
let m = res.unwrap();
assert!(m.contains_id("opt"));
assert!(!m.contains_id("other"));
assert_eq!(m.get_one::<String>("opt").map(|v| v.as_str()), Some("some"));
}
#[test]
fn aaos_opts_w_override_as_conflict_2() {
// opts with other overrides, rev
let res = Command::new("posix")
.arg(
arg!(--opt <val> "some option")
.required(true)
.overrides_with("other")
.action(ArgAction::Set),
)
.arg(
arg!(--other <val> "some other option")
.required(true)
.action(ArgAction::Set),
)
.try_get_matches_from(vec!["", "--other=some"]);
assert!(res.is_ok(), "{}", res.unwrap_err());
let m = res.unwrap();
assert!(!m.contains_id("opt"));
assert!(m.contains_id("other"));
assert_eq!(
m.get_one::<String>("other").map(|v| v.as_str()),
Some("some")
);
}
#[test]
fn aaos_opts_mult_req_delims() {
// opts with multiple and require delims
2022-02-12 03:48:29 +00:00
let res = Command::new("posix")
.arg(
2021-11-19 20:33:11 +00:00
arg!(--opt <val> ... "some option")
.action(ArgAction::Set)
.value_delimiter(',')
.action(ArgAction::Append),
)
.try_get_matches_from(vec!["", "--opt=some", "--opt=other", "--opt=one,two"]);
2021-12-27 19:57:38 +00:00
assert!(res.is_ok(), "{}", res.unwrap_err());
let m = res.unwrap();
assert!(m.contains_id("opt"));
assert_eq!(
m.get_many::<String>("opt")
.unwrap()
.map(|v| v.as_str())
.collect::<Vec<_>>(),
2022-11-24 13:54:25 +00:00
["some", "other", "one", "two"]
);
}
#[test]
fn aaos_opts_mult() {
// opts with multiple
2022-02-12 03:48:29 +00:00
let res = Command::new("posix")
.arg(
arg!(--opt <val> ... "some option")
.num_args(1..)
.action(ArgAction::Append),
)
.try_get_matches_from(vec![
"",
"--opt",
"first",
"overrides",
"--opt",
"some",
"other",
"val",
]);
2021-12-27 19:57:38 +00:00
assert!(res.is_ok(), "{}", res.unwrap_err());
let m = res.unwrap();
assert!(m.contains_id("opt"));
assert_eq!(
m.get_many::<String>("opt")
.unwrap()
.map(|v| v.as_str())
.collect::<Vec<_>>(),
2022-11-24 13:54:25 +00:00
["first", "overrides", "some", "other", "val"]
);
}
#[test]
fn aaos_pos_mult() {
// opts with multiple
2022-02-12 03:48:29 +00:00
let res = Command::new("posix")
2021-11-19 20:33:11 +00:00
.arg(arg!([val] ... "some pos"))
.try_get_matches_from(vec!["", "some", "other", "value"]);
2021-12-27 19:57:38 +00:00
assert!(res.is_ok(), "{}", res.unwrap_err());
let m = res.unwrap();
assert!(m.contains_id("val"));
assert_eq!(
m.get_many::<String>("val")
.unwrap()
.map(|v| v.as_str())
.collect::<Vec<_>>(),
2022-11-24 13:54:25 +00:00
["some", "other", "value"]
);
}
#[test]
fn aaos_option_use_delim_false() {
2022-02-12 03:48:29 +00:00
let m = Command::new("posix")
.args_override_self(true)
.arg(
arg!(--opt <val> "some option")
.required(true)
.action(ArgAction::Set),
)
.try_get_matches_from(vec!["", "--opt=some,other", "--opt=one,two"])
.unwrap();
assert!(m.contains_id("opt"));
assert_eq!(
m.get_many::<String>("opt")
.unwrap()
.map(|v| v.as_str())
.collect::<Vec<_>>(),
2022-11-24 13:54:25 +00:00
["one,two"]
);
}
#[test]
#[cfg(feature = "color")]
fn color_is_global() {
2022-02-14 21:47:20 +00:00
let mut cmd = Command::new("myprog")
.color(clap::ColorChoice::Never)
2022-02-12 03:48:29 +00:00
.subcommand(Command::new("foo"));
cmd.build();
2022-02-14 21:47:20 +00:00
assert_eq!(cmd.get_color(), clap::ColorChoice::Never);
2022-02-14 21:47:20 +00:00
let sub = cmd.get_subcommands().collect::<Vec<_>>()[0];
assert_eq!(sub.get_color(), clap::ColorChoice::Never);
}