2021-11-19 16:19:18 +00:00
|
|
|
mod arg {
|
|
|
|
#[test]
|
|
|
|
fn name_explicit() {
|
|
|
|
let arg = clap::arg!(foo: --bar <NUM>);
|
2022-02-11 20:10:29 +00:00
|
|
|
assert_eq!(arg.get_id(), "foo");
|
2022-08-22 18:48:39 +00:00
|
|
|
assert_eq!(arg.get_long(), Some("bar"));
|
2022-08-15 19:29:46 +00:00
|
|
|
assert_eq!(arg.get_value_names(), Some(vec!["NUM".into()].as_slice()));
|
2022-09-12 21:59:57 +00:00
|
|
|
assert!(!arg.is_required_set());
|
2021-11-19 16:19:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn name_from_long() {
|
|
|
|
let arg = clap::arg!(--bar <NUM>);
|
2022-02-11 20:10:29 +00:00
|
|
|
assert_eq!(arg.get_id(), "bar");
|
2022-08-22 18:48:39 +00:00
|
|
|
assert_eq!(arg.get_long(), Some("bar"));
|
2022-08-15 19:29:46 +00:00
|
|
|
assert_eq!(arg.get_value_names(), Some(vec!["NUM".into()].as_slice()));
|
2022-09-12 21:59:57 +00:00
|
|
|
assert!(!arg.is_required_set());
|
2021-11-19 16:19:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn name_from_value() {
|
|
|
|
let arg = clap::arg!(<NUM>);
|
2022-02-11 20:10:29 +00:00
|
|
|
assert_eq!(arg.get_id(), "NUM");
|
2022-08-22 18:48:39 +00:00
|
|
|
assert_eq!(arg.get_long(), None);
|
2022-08-15 19:29:46 +00:00
|
|
|
assert_eq!(arg.get_value_names(), Some(vec!["NUM".into()].as_slice()));
|
2022-02-09 17:16:34 +00:00
|
|
|
assert!(arg.is_required_set());
|
2021-11-19 16:19:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn name_none_fails() {
|
|
|
|
clap::arg!("Help");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn short_only_fails() {
|
|
|
|
clap::arg!(-b);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn short() {
|
|
|
|
let arg = clap::arg!(foo: -b);
|
2022-02-11 20:10:29 +00:00
|
|
|
assert_eq!(arg.get_id(), "foo");
|
2021-11-19 16:19:18 +00:00
|
|
|
assert_eq!(arg.get_short(), Some('b'));
|
2022-07-21 21:36:54 +00:00
|
|
|
assert!(matches!(arg.get_action(), clap::ArgAction::SetTrue));
|
2022-08-04 19:53:42 +00:00
|
|
|
assert_eq!(arg.get_num_args(), None);
|
2022-02-09 17:16:34 +00:00
|
|
|
assert!(!arg.is_required_set());
|
2022-08-24 15:24:15 +00:00
|
|
|
assert_eq!(arg.get_help().map(|s| s.to_string()), None);
|
2021-11-19 16:19:18 +00:00
|
|
|
|
|
|
|
let arg = clap::arg!(foo: -'b');
|
2022-02-11 20:10:29 +00:00
|
|
|
assert_eq!(arg.get_id(), "foo");
|
2021-11-19 16:19:18 +00:00
|
|
|
assert_eq!(arg.get_short(), Some('b'));
|
2022-07-21 21:36:54 +00:00
|
|
|
assert!(matches!(arg.get_action(), clap::ArgAction::SetTrue));
|
2022-08-04 19:53:42 +00:00
|
|
|
assert_eq!(arg.get_num_args(), None);
|
2022-02-09 17:16:34 +00:00
|
|
|
assert!(!arg.is_required_set());
|
2022-08-24 15:24:15 +00:00
|
|
|
assert_eq!(arg.get_help().map(|s| s.to_string()), None);
|
2021-11-19 16:19:18 +00:00
|
|
|
|
|
|
|
let arg = clap::arg!(foo: -b ...);
|
2022-02-11 20:10:29 +00:00
|
|
|
assert_eq!(arg.get_id(), "foo");
|
2021-11-19 16:19:18 +00:00
|
|
|
assert_eq!(arg.get_short(), Some('b'));
|
2022-07-21 21:36:54 +00:00
|
|
|
assert!(matches!(arg.get_action(), clap::ArgAction::Count));
|
2022-08-04 19:53:42 +00:00
|
|
|
assert_eq!(arg.get_num_args(), None);
|
2022-02-09 17:16:34 +00:00
|
|
|
assert!(!arg.is_required_set());
|
2022-08-24 15:24:15 +00:00
|
|
|
assert_eq!(arg.get_help().map(|s| s.to_string()), None);
|
2021-11-19 16:19:18 +00:00
|
|
|
|
|
|
|
let arg = clap::arg!(foo: -b "How to use it");
|
2022-02-11 20:10:29 +00:00
|
|
|
assert_eq!(arg.get_id(), "foo");
|
2021-11-19 16:19:18 +00:00
|
|
|
assert_eq!(arg.get_short(), Some('b'));
|
2022-07-21 21:36:54 +00:00
|
|
|
assert!(matches!(arg.get_action(), clap::ArgAction::SetTrue));
|
2022-08-04 19:53:42 +00:00
|
|
|
assert_eq!(arg.get_num_args(), None);
|
2022-02-09 17:16:34 +00:00
|
|
|
assert!(!arg.is_required_set());
|
2022-08-24 15:24:15 +00:00
|
|
|
assert_eq!(
|
|
|
|
arg.get_help().map(|s| s.to_string()),
|
|
|
|
Some("How to use it".to_owned())
|
|
|
|
);
|
2021-11-19 16:19:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn short_and_long() {
|
|
|
|
let arg = clap::arg!(foo: -b --hello);
|
2022-02-11 20:10:29 +00:00
|
|
|
assert_eq!(arg.get_id(), "foo");
|
2022-08-22 18:48:39 +00:00
|
|
|
assert_eq!(arg.get_long(), Some("hello"));
|
2021-11-19 16:19:18 +00:00
|
|
|
assert_eq!(arg.get_short(), Some('b'));
|
2022-07-21 21:36:54 +00:00
|
|
|
assert!(matches!(arg.get_action(), clap::ArgAction::SetTrue));
|
2022-08-04 19:53:42 +00:00
|
|
|
assert_eq!(arg.get_num_args(), None);
|
2022-02-09 17:16:34 +00:00
|
|
|
assert!(!arg.is_required_set());
|
2022-08-24 15:24:15 +00:00
|
|
|
assert_eq!(arg.get_help().map(|s| s.to_string()), None);
|
2021-11-19 16:19:18 +00:00
|
|
|
|
|
|
|
let arg = clap::arg!(foo: -'b' --hello);
|
2022-02-11 20:10:29 +00:00
|
|
|
assert_eq!(arg.get_id(), "foo");
|
2022-08-22 18:48:39 +00:00
|
|
|
assert_eq!(arg.get_long(), Some("hello"));
|
2021-11-19 16:19:18 +00:00
|
|
|
assert_eq!(arg.get_short(), Some('b'));
|
2022-07-21 21:36:54 +00:00
|
|
|
assert!(matches!(arg.get_action(), clap::ArgAction::SetTrue));
|
2022-08-04 19:53:42 +00:00
|
|
|
assert_eq!(arg.get_num_args(), None);
|
2022-02-09 17:16:34 +00:00
|
|
|
assert!(!arg.is_required_set());
|
2022-08-24 15:24:15 +00:00
|
|
|
assert_eq!(arg.get_help().map(|s| s.to_string()), None);
|
2021-11-19 16:19:18 +00:00
|
|
|
|
|
|
|
let arg = clap::arg!(foo: -b --hello ...);
|
2022-02-11 20:10:29 +00:00
|
|
|
assert_eq!(arg.get_id(), "foo");
|
2022-08-22 18:48:39 +00:00
|
|
|
assert_eq!(arg.get_long(), Some("hello"));
|
2021-11-19 16:19:18 +00:00
|
|
|
assert_eq!(arg.get_short(), Some('b'));
|
2022-07-21 21:36:54 +00:00
|
|
|
assert!(matches!(arg.get_action(), clap::ArgAction::Count));
|
2022-08-04 19:53:42 +00:00
|
|
|
assert_eq!(arg.get_num_args(), None);
|
2022-02-09 17:16:34 +00:00
|
|
|
assert!(!arg.is_required_set());
|
2022-08-24 15:24:15 +00:00
|
|
|
assert_eq!(arg.get_help().map(|s| s.to_string()), None);
|
2021-11-19 16:19:18 +00:00
|
|
|
|
|
|
|
let arg = clap::arg!(foo: -b --hello "How to use it");
|
2022-02-11 20:10:29 +00:00
|
|
|
assert_eq!(arg.get_id(), "foo");
|
2022-08-22 18:48:39 +00:00
|
|
|
assert_eq!(arg.get_long(), Some("hello"));
|
2021-11-19 16:19:18 +00:00
|
|
|
assert_eq!(arg.get_short(), Some('b'));
|
2022-07-21 21:36:54 +00:00
|
|
|
assert!(matches!(arg.get_action(), clap::ArgAction::SetTrue));
|
2022-08-04 19:53:42 +00:00
|
|
|
assert_eq!(arg.get_num_args(), None);
|
2022-02-09 17:16:34 +00:00
|
|
|
assert!(!arg.is_required_set());
|
2022-08-24 15:24:15 +00:00
|
|
|
assert_eq!(
|
|
|
|
arg.get_help().map(|s| s.to_string()),
|
|
|
|
Some("How to use it".to_owned())
|
|
|
|
);
|
2021-11-19 16:19:18 +00:00
|
|
|
}
|
|
|
|
|
2022-08-05 19:09:22 +00:00
|
|
|
#[test]
|
|
|
|
fn short_help() {
|
|
|
|
let arg = clap::arg!(help: -b);
|
2022-08-10 20:13:44 +00:00
|
|
|
assert!(matches!(arg.get_action(), clap::ArgAction::SetTrue));
|
2022-08-05 19:09:22 +00:00
|
|
|
|
2022-08-10 20:13:44 +00:00
|
|
|
let mut cmd = clap::Command::new("cmd")
|
|
|
|
.disable_help_flag(true)
|
|
|
|
.arg(clap::arg!(help: -b).action(clap::ArgAction::Help));
|
|
|
|
cmd.build();
|
|
|
|
let arg = cmd
|
|
|
|
.get_arguments()
|
|
|
|
.find(|arg| arg.get_id() == "help")
|
|
|
|
.unwrap();
|
|
|
|
assert!(matches!(arg.get_action(), clap::ArgAction::Help));
|
2022-08-05 19:09:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn long_help() {
|
|
|
|
let arg = clap::arg!(-'?' - -help);
|
2022-08-10 20:13:44 +00:00
|
|
|
assert!(matches!(arg.get_action(), clap::ArgAction::SetTrue));
|
2022-08-05 19:09:22 +00:00
|
|
|
|
2022-08-10 20:13:44 +00:00
|
|
|
let mut cmd = clap::Command::new("cmd")
|
|
|
|
.disable_help_flag(true)
|
|
|
|
.arg(clap::arg!(-'?' - -help).action(clap::ArgAction::Help));
|
|
|
|
cmd.build();
|
|
|
|
let arg = cmd
|
|
|
|
.get_arguments()
|
|
|
|
.find(|arg| arg.get_id() == "help")
|
|
|
|
.unwrap();
|
|
|
|
assert!(matches!(arg.get_action(), clap::ArgAction::Help));
|
2022-08-05 19:09:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn short_version() {
|
|
|
|
let arg = clap::arg!(version: -b);
|
2022-08-10 20:13:44 +00:00
|
|
|
assert!(matches!(arg.get_action(), clap::ArgAction::SetTrue));
|
2022-08-05 19:09:22 +00:00
|
|
|
|
2022-08-10 20:13:44 +00:00
|
|
|
let mut cmd = clap::Command::new("cmd")
|
|
|
|
.disable_version_flag(true)
|
|
|
|
.version("1.0.0")
|
|
|
|
.arg(clap::arg!(version: -b).action(clap::ArgAction::Version));
|
|
|
|
cmd.build();
|
|
|
|
let arg = cmd
|
|
|
|
.get_arguments()
|
|
|
|
.find(|arg| arg.get_id() == "version")
|
|
|
|
.unwrap();
|
|
|
|
assert!(matches!(arg.get_action(), clap::ArgAction::Version));
|
2022-08-05 19:09:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn long_version() {
|
|
|
|
let arg = clap::arg!(-'?' - -version);
|
2022-08-10 20:13:44 +00:00
|
|
|
assert!(matches!(arg.get_action(), clap::ArgAction::SetTrue));
|
2022-08-05 19:09:22 +00:00
|
|
|
|
2022-08-10 20:13:44 +00:00
|
|
|
let mut cmd = clap::Command::new("cmd")
|
|
|
|
.disable_version_flag(true)
|
|
|
|
.version("1.0.0")
|
|
|
|
.arg(clap::arg!(-'?' - -version).action(clap::ArgAction::Version));
|
|
|
|
cmd.build();
|
|
|
|
let arg = cmd
|
|
|
|
.get_arguments()
|
|
|
|
.find(|arg| arg.get_id() == "version")
|
|
|
|
.unwrap();
|
|
|
|
assert!(matches!(arg.get_action(), clap::ArgAction::Version));
|
2022-08-05 19:09:22 +00:00
|
|
|
}
|
|
|
|
|
2022-07-21 21:36:54 +00:00
|
|
|
#[test]
|
|
|
|
fn short_with_value() {
|
|
|
|
let arg = clap::arg!(foo: -b <NUM>);
|
|
|
|
assert_eq!(arg.get_id(), "foo");
|
|
|
|
assert_eq!(arg.get_short(), Some('b'));
|
|
|
|
assert!(matches!(arg.get_action(), clap::ArgAction::Set));
|
2022-08-04 19:53:42 +00:00
|
|
|
assert_eq!(arg.get_num_args(), None);
|
2022-09-12 21:59:57 +00:00
|
|
|
assert!(!arg.is_required_set());
|
2022-08-24 15:24:15 +00:00
|
|
|
assert_eq!(arg.get_help().map(|s| s.to_string()), None);
|
2022-07-21 21:36:54 +00:00
|
|
|
|
|
|
|
let arg = clap::arg!(foo: -'b' <NUM>);
|
|
|
|
assert_eq!(arg.get_id(), "foo");
|
|
|
|
assert_eq!(arg.get_short(), Some('b'));
|
|
|
|
assert!(matches!(arg.get_action(), clap::ArgAction::Set));
|
2022-08-04 19:53:42 +00:00
|
|
|
assert_eq!(arg.get_num_args(), None);
|
2022-09-12 21:59:57 +00:00
|
|
|
assert!(!arg.is_required_set());
|
2022-08-24 15:24:15 +00:00
|
|
|
assert_eq!(arg.get_help().map(|s| s.to_string()), None);
|
2022-07-21 21:36:54 +00:00
|
|
|
|
|
|
|
let arg = clap::arg!(foo: -b <NUM> ...);
|
|
|
|
assert_eq!(arg.get_id(), "foo");
|
|
|
|
assert_eq!(arg.get_short(), Some('b'));
|
|
|
|
assert!(matches!(arg.get_action(), clap::ArgAction::Append));
|
2022-08-04 19:53:42 +00:00
|
|
|
assert_eq!(arg.get_num_args(), None);
|
2022-09-12 21:59:57 +00:00
|
|
|
assert!(!arg.is_required_set());
|
2022-08-24 15:24:15 +00:00
|
|
|
assert_eq!(arg.get_help().map(|s| s.to_string()), None);
|
2022-07-21 21:36:54 +00:00
|
|
|
|
|
|
|
let arg = clap::arg!(foo: -b <NUM> "How to use it");
|
|
|
|
assert_eq!(arg.get_id(), "foo");
|
|
|
|
assert_eq!(arg.get_short(), Some('b'));
|
|
|
|
assert!(matches!(arg.get_action(), clap::ArgAction::Set));
|
2022-08-04 19:53:42 +00:00
|
|
|
assert_eq!(arg.get_num_args(), None);
|
2022-09-12 21:59:57 +00:00
|
|
|
assert!(!arg.is_required_set());
|
2022-08-24 15:24:15 +00:00
|
|
|
assert_eq!(
|
|
|
|
arg.get_help().map(|s| s.to_string()),
|
|
|
|
Some("How to use it".to_owned())
|
|
|
|
);
|
2022-07-21 21:36:54 +00:00
|
|
|
}
|
|
|
|
|
2021-11-19 16:19:18 +00:00
|
|
|
#[test]
|
|
|
|
fn positional() {
|
|
|
|
let arg = clap::arg!(<NUM>);
|
2022-02-11 20:10:29 +00:00
|
|
|
assert_eq!(arg.get_id(), "NUM");
|
2022-08-15 19:29:46 +00:00
|
|
|
assert_eq!(arg.get_value_names(), Some(vec!["NUM".into()].as_slice()));
|
2022-07-21 21:36:54 +00:00
|
|
|
assert!(matches!(arg.get_action(), clap::ArgAction::Set));
|
2022-08-04 19:53:42 +00:00
|
|
|
assert_eq!(arg.get_num_args(), None);
|
2022-02-09 17:16:34 +00:00
|
|
|
assert!(arg.is_required_set());
|
2022-08-24 15:24:15 +00:00
|
|
|
assert_eq!(arg.get_help().map(|s| s.to_string()), None);
|
2021-11-19 16:19:18 +00:00
|
|
|
|
|
|
|
let arg = clap::arg!([NUM]);
|
2022-02-11 20:10:29 +00:00
|
|
|
assert_eq!(arg.get_id(), "NUM");
|
2022-08-15 19:29:46 +00:00
|
|
|
assert_eq!(arg.get_value_names(), Some(vec!["NUM".into()].as_slice()));
|
2022-07-21 21:36:54 +00:00
|
|
|
assert!(matches!(arg.get_action(), clap::ArgAction::Set));
|
2022-08-04 19:53:42 +00:00
|
|
|
assert_eq!(arg.get_num_args(), None);
|
2022-02-09 17:16:34 +00:00
|
|
|
assert!(!arg.is_required_set());
|
2022-08-24 15:24:15 +00:00
|
|
|
assert_eq!(arg.get_help().map(|s| s.to_string()), None);
|
2021-11-19 16:19:18 +00:00
|
|
|
|
|
|
|
let arg = clap::arg!(<NUM>);
|
2022-02-11 20:10:29 +00:00
|
|
|
assert_eq!(arg.get_id(), "NUM");
|
2022-08-15 19:29:46 +00:00
|
|
|
assert_eq!(arg.get_value_names(), Some(vec!["NUM".into()].as_slice()));
|
2022-07-21 21:36:54 +00:00
|
|
|
assert!(matches!(arg.get_action(), clap::ArgAction::Set));
|
2022-08-04 19:53:42 +00:00
|
|
|
assert_eq!(arg.get_num_args(), None);
|
2022-02-09 17:16:34 +00:00
|
|
|
assert!(arg.is_required_set());
|
2022-08-24 15:24:15 +00:00
|
|
|
assert_eq!(arg.get_help().map(|s| s.to_string()), None);
|
2021-11-19 16:19:18 +00:00
|
|
|
|
|
|
|
let arg = clap::arg!(foo: <NUM>);
|
2022-02-11 20:10:29 +00:00
|
|
|
assert_eq!(arg.get_id(), "foo");
|
2022-08-15 19:29:46 +00:00
|
|
|
assert_eq!(arg.get_value_names(), Some(vec!["NUM".into()].as_slice()));
|
2022-07-21 21:36:54 +00:00
|
|
|
assert!(matches!(arg.get_action(), clap::ArgAction::Set));
|
2022-08-04 19:53:42 +00:00
|
|
|
assert_eq!(arg.get_num_args(), None);
|
2022-02-09 17:16:34 +00:00
|
|
|
assert!(arg.is_required_set());
|
2022-08-24 15:24:15 +00:00
|
|
|
assert_eq!(arg.get_help().map(|s| s.to_string()), None);
|
2021-11-19 16:19:18 +00:00
|
|
|
|
|
|
|
let arg = clap::arg!(<NUM> ...);
|
2022-02-11 20:10:29 +00:00
|
|
|
assert_eq!(arg.get_id(), "NUM");
|
2022-08-15 19:29:46 +00:00
|
|
|
assert_eq!(arg.get_value_names(), Some(vec!["NUM".into()].as_slice()));
|
2022-07-22 21:30:40 +00:00
|
|
|
assert!(matches!(arg.get_action(), clap::ArgAction::Append));
|
2022-08-04 19:53:42 +00:00
|
|
|
assert_eq!(arg.get_num_args(), Some((1..).into()));
|
2022-02-09 17:16:34 +00:00
|
|
|
assert!(arg.is_required_set());
|
2022-08-24 15:24:15 +00:00
|
|
|
assert_eq!(arg.get_help().map(|s| s.to_string()), None);
|
2021-11-19 16:19:18 +00:00
|
|
|
|
|
|
|
let arg = clap::arg!(<NUM> "How to use it");
|
2022-02-11 20:10:29 +00:00
|
|
|
assert_eq!(arg.get_id(), "NUM");
|
2022-08-15 19:29:46 +00:00
|
|
|
assert_eq!(arg.get_value_names(), Some(vec!["NUM".into()].as_slice()));
|
2022-07-21 21:36:54 +00:00
|
|
|
assert!(matches!(arg.get_action(), clap::ArgAction::Set));
|
2022-08-04 19:53:42 +00:00
|
|
|
assert_eq!(arg.get_num_args(), None);
|
2022-02-09 17:16:34 +00:00
|
|
|
assert!(arg.is_required_set());
|
2022-08-24 15:24:15 +00:00
|
|
|
assert_eq!(
|
|
|
|
arg.get_help().map(|s| s.to_string()),
|
|
|
|
Some("How to use it".to_owned())
|
|
|
|
);
|
2021-11-19 16:19:18 +00:00
|
|
|
}
|
2022-07-28 18:27:28 +00:00
|
|
|
|
|
|
|
#[test]
|
2022-09-19 16:54:06 +00:00
|
|
|
#[cfg(feature = "help")]
|
2022-07-28 18:27:28 +00:00
|
|
|
fn optional_value() {
|
|
|
|
let mut cmd = clap::Command::new("test").arg(clap::arg!(port: -p [NUM]));
|
|
|
|
|
|
|
|
let r = cmd.try_get_matches_from_mut(["test", "-p42"]);
|
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.contains_id("port"));
|
|
|
|
assert_eq!(m.get_one::<String>("port").unwrap(), "42");
|
|
|
|
|
|
|
|
let r = cmd.try_get_matches_from_mut(["test", "-p"]);
|
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.contains_id("port"));
|
|
|
|
assert!(m.get_one::<String>("port").is_none());
|
|
|
|
|
|
|
|
let r = cmd.try_get_matches_from_mut(["test", "-p", "24", "-p", "42"]);
|
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.contains_id("port"));
|
|
|
|
assert_eq!(m.get_one::<String>("port").unwrap(), "42");
|
|
|
|
|
|
|
|
let mut help = Vec::new();
|
|
|
|
cmd.write_help(&mut help).unwrap();
|
|
|
|
const HELP: &str = "\
|
2022-09-07 16:03:55 +00:00
|
|
|
Usage: test [OPTIONS]
|
2022-07-28 18:27:28 +00:00
|
|
|
|
2022-08-26 14:40:23 +00:00
|
|
|
Options:
|
2022-09-07 20:29:15 +00:00
|
|
|
-p [<NUM>]
|
|
|
|
-h, --help Print help information
|
2022-07-28 18:27:28 +00:00
|
|
|
";
|
|
|
|
snapbox::assert_eq(HELP, help);
|
|
|
|
}
|
2021-11-19 16:19:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mod arg_impl {
|
|
|
|
#[test]
|
|
|
|
fn string_ident() {
|
|
|
|
let expected = "one";
|
|
|
|
let actual = clap::arg_impl! { @string one };
|
|
|
|
assert_eq!(actual, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn string_literal() {
|
|
|
|
let expected = "one";
|
|
|
|
let actual = clap::arg_impl! { @string "one" };
|
|
|
|
assert_eq!(actual, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn char_ident() {
|
|
|
|
let expected = 'o';
|
|
|
|
let actual = clap::arg_impl! { @char o };
|
|
|
|
assert_eq!(actual, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn char_literal() {
|
|
|
|
let expected = 'o';
|
|
|
|
let actual = clap::arg_impl! { @char 'o' };
|
|
|
|
assert_eq!(actual, expected);
|
|
|
|
}
|
2022-05-06 15:58:00 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
// allow double quoted dashed arg name in square brackets (e.g ["some-arg"])
|
|
|
|
fn arg_name_dashed() {
|
|
|
|
let arg = clap::arg!(["some-arg"] "some arg");
|
|
|
|
assert_eq!(arg, clap::Arg::new("some-arg").help("some arg"));
|
|
|
|
|
|
|
|
let m = clap::Command::new("flag")
|
|
|
|
.arg(arg)
|
|
|
|
.try_get_matches_from(vec!["", "some-val"])
|
|
|
|
.unwrap();
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(m.get_one::<String>("some-arg").unwrap(), "some-val");
|
2022-05-06 15:58:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
// allow double quoted dashed arg value in triangle brackets (e.g <"some-val">)
|
|
|
|
// test in combination with short argument name (e.g. -v)
|
|
|
|
fn arg_value_dashed_with_short_arg() {
|
|
|
|
let arg = clap::arg!(-a <"some-val"> "some arg");
|
|
|
|
assert_eq!(
|
|
|
|
arg,
|
|
|
|
clap::Arg::new("some-val")
|
|
|
|
.short('a')
|
|
|
|
.long("arg")
|
|
|
|
.value_name("some-val")
|
|
|
|
);
|
|
|
|
|
|
|
|
let m = clap::Command::new("cmd")
|
|
|
|
.arg(arg)
|
|
|
|
.try_get_matches_from(vec!["", "-a", "val"])
|
|
|
|
.unwrap();
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(m.get_one::<String>("some-val").unwrap(), "val");
|
2022-05-06 15:58:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
// allow double quoted dashed arg value in triangle brackets (e.g <"some-val">)
|
|
|
|
// test in combination with long argument name (e.g. --value)
|
|
|
|
fn arg_value_dashed_with_long_arg() {
|
|
|
|
let arg = clap::arg!(-a --arg <"some-val"> "some arg");
|
|
|
|
assert_eq!(
|
|
|
|
arg,
|
|
|
|
clap::Arg::new("arg")
|
|
|
|
.short('a')
|
|
|
|
.long("arg")
|
|
|
|
.value_name("some-val")
|
|
|
|
);
|
|
|
|
|
|
|
|
let m = clap::Command::new("cmd")
|
|
|
|
.arg(arg)
|
|
|
|
.try_get_matches_from(vec!["", "--arg", "some-val"])
|
|
|
|
.unwrap();
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(m.get_one::<String>("arg").unwrap(), "some-val");
|
2022-05-06 15:58:00 +00:00
|
|
|
}
|
2021-11-19 16:19:18 +00:00
|
|
|
}
|