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");
|
2021-11-19 16:19:18 +00:00
|
|
|
assert_eq!(arg.get_long(), Some("bar"));
|
|
|
|
assert_eq!(arg.get_value_names(), Some(vec!["NUM"].as_slice()));
|
2022-02-09 17:16:34 +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");
|
2021-11-19 16:19:18 +00:00
|
|
|
assert_eq!(arg.get_long(), Some("bar"));
|
|
|
|
assert_eq!(arg.get_value_names(), Some(vec!["NUM"].as_slice()));
|
2022-02-09 17:16:34 +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");
|
2021-11-19 16:19:18 +00:00
|
|
|
assert_eq!(arg.get_long(), None);
|
|
|
|
assert_eq!(arg.get_value_names(), Some(vec!["NUM"].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-06-07 18:48:48 +00:00
|
|
|
#[allow(deprecated)]
|
|
|
|
{
|
|
|
|
assert!(!arg.is_multiple_occurrences_set());
|
|
|
|
}
|
2022-02-09 17:16:34 +00:00
|
|
|
assert!(!arg.is_required_set());
|
2021-11-19 16:19:18 +00:00
|
|
|
assert_eq!(arg.get_help(), None);
|
|
|
|
|
|
|
|
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-06-07 18:48:48 +00:00
|
|
|
#[allow(deprecated)]
|
|
|
|
{
|
|
|
|
assert!(!arg.is_multiple_occurrences_set());
|
|
|
|
}
|
2022-02-09 17:16:34 +00:00
|
|
|
assert!(!arg.is_required_set());
|
2021-11-19 16:19:18 +00:00
|
|
|
assert_eq!(arg.get_help(), None);
|
|
|
|
|
|
|
|
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-06-07 18:48:48 +00:00
|
|
|
#[allow(deprecated)]
|
|
|
|
{
|
|
|
|
assert!(arg.is_multiple_occurrences_set());
|
|
|
|
}
|
2022-02-09 17:16:34 +00:00
|
|
|
assert!(!arg.is_required_set());
|
2021-11-19 16:19:18 +00:00
|
|
|
assert_eq!(arg.get_help(), None);
|
|
|
|
|
|
|
|
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-06-07 18:48:48 +00:00
|
|
|
#[allow(deprecated)]
|
|
|
|
{
|
|
|
|
assert!(!arg.is_multiple_occurrences_set());
|
|
|
|
}
|
2022-02-09 17:16:34 +00:00
|
|
|
assert!(!arg.is_required_set());
|
2021-11-19 16:19:18 +00:00
|
|
|
assert_eq!(arg.get_help(), Some("How to use it"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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");
|
2021-11-19 16:19:18 +00:00
|
|
|
assert_eq!(arg.get_long(), Some("hello"));
|
|
|
|
assert_eq!(arg.get_short(), Some('b'));
|
2022-06-07 18:48:48 +00:00
|
|
|
#[allow(deprecated)]
|
|
|
|
{
|
|
|
|
assert!(!arg.is_multiple_occurrences_set());
|
|
|
|
}
|
2022-02-09 17:16:34 +00:00
|
|
|
assert!(!arg.is_required_set());
|
2021-11-19 16:19:18 +00:00
|
|
|
assert_eq!(arg.get_help(), None);
|
|
|
|
|
|
|
|
let arg = clap::arg!(foo: -'b' --hello);
|
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_long(), Some("hello"));
|
|
|
|
assert_eq!(arg.get_short(), Some('b'));
|
2022-06-07 18:48:48 +00:00
|
|
|
#[allow(deprecated)]
|
|
|
|
{
|
|
|
|
assert!(!arg.is_multiple_occurrences_set());
|
|
|
|
}
|
2022-02-09 17:16:34 +00:00
|
|
|
assert!(!arg.is_required_set());
|
2021-11-19 16:19:18 +00:00
|
|
|
assert_eq!(arg.get_help(), None);
|
|
|
|
|
|
|
|
let arg = clap::arg!(foo: -b --hello ...);
|
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_long(), Some("hello"));
|
|
|
|
assert_eq!(arg.get_short(), Some('b'));
|
2022-06-07 18:48:48 +00:00
|
|
|
#[allow(deprecated)]
|
|
|
|
{
|
|
|
|
assert!(arg.is_multiple_occurrences_set());
|
|
|
|
}
|
2022-02-09 17:16:34 +00:00
|
|
|
assert!(!arg.is_required_set());
|
2021-11-19 16:19:18 +00:00
|
|
|
assert_eq!(arg.get_help(), None);
|
|
|
|
|
|
|
|
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");
|
2021-11-19 16:19:18 +00:00
|
|
|
assert_eq!(arg.get_long(), Some("hello"));
|
|
|
|
assert_eq!(arg.get_short(), Some('b'));
|
2022-06-07 18:48:48 +00:00
|
|
|
#[allow(deprecated)]
|
|
|
|
{
|
|
|
|
assert!(!arg.is_multiple_occurrences_set());
|
|
|
|
}
|
2022-02-09 17:16:34 +00:00
|
|
|
assert!(!arg.is_required_set());
|
2021-11-19 16:19:18 +00:00
|
|
|
assert_eq!(arg.get_help(), Some("How to use it"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn positional() {
|
|
|
|
let arg = clap::arg!(<NUM>);
|
2022-02-11 20:10:29 +00:00
|
|
|
assert_eq!(arg.get_id(), "NUM");
|
2021-11-19 16:19:18 +00:00
|
|
|
assert_eq!(arg.get_value_names(), Some(vec!["NUM"].as_slice()));
|
2022-06-07 18:48:48 +00:00
|
|
|
#[allow(deprecated)]
|
|
|
|
{
|
|
|
|
assert!(!arg.is_multiple_occurrences_set());
|
|
|
|
}
|
2022-02-09 17:16:34 +00:00
|
|
|
assert!(arg.is_required_set());
|
2021-11-19 16:19:18 +00:00
|
|
|
assert_eq!(arg.get_help(), None);
|
|
|
|
|
|
|
|
let arg = clap::arg!([NUM]);
|
2022-02-11 20:10:29 +00:00
|
|
|
assert_eq!(arg.get_id(), "NUM");
|
2021-11-19 16:19:18 +00:00
|
|
|
assert_eq!(arg.get_value_names(), Some(vec!["NUM"].as_slice()));
|
2022-06-07 18:48:48 +00:00
|
|
|
#[allow(deprecated)]
|
|
|
|
{
|
|
|
|
assert!(!arg.is_multiple_occurrences_set());
|
|
|
|
}
|
2022-02-09 17:16:34 +00:00
|
|
|
assert!(!arg.is_required_set());
|
2021-11-19 16:19:18 +00:00
|
|
|
assert_eq!(arg.get_help(), None);
|
|
|
|
|
|
|
|
let arg = clap::arg!(<NUM>);
|
2022-02-11 20:10:29 +00:00
|
|
|
assert_eq!(arg.get_id(), "NUM");
|
2021-11-19 16:19:18 +00:00
|
|
|
assert_eq!(arg.get_value_names(), Some(vec!["NUM"].as_slice()));
|
2022-06-07 18:48:48 +00:00
|
|
|
#[allow(deprecated)]
|
|
|
|
{
|
|
|
|
assert!(!arg.is_multiple_occurrences_set());
|
|
|
|
}
|
2022-02-09 17:16:34 +00:00
|
|
|
assert!(arg.is_required_set());
|
2021-11-19 16:19:18 +00:00
|
|
|
assert_eq!(arg.get_help(), None);
|
|
|
|
|
|
|
|
let arg = clap::arg!(foo: <NUM>);
|
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_value_names(), Some(vec!["NUM"].as_slice()));
|
2022-06-07 18:48:48 +00:00
|
|
|
#[allow(deprecated)]
|
|
|
|
{
|
|
|
|
assert!(!arg.is_multiple_occurrences_set());
|
|
|
|
}
|
2022-02-09 17:16:34 +00:00
|
|
|
assert!(arg.is_required_set());
|
2021-11-19 16:19:18 +00:00
|
|
|
assert_eq!(arg.get_help(), None);
|
|
|
|
|
|
|
|
let arg = clap::arg!(<NUM> ...);
|
2022-02-11 20:10:29 +00:00
|
|
|
assert_eq!(arg.get_id(), "NUM");
|
2021-11-19 16:19:18 +00:00
|
|
|
assert_eq!(arg.get_value_names(), Some(vec!["NUM"].as_slice()));
|
2022-06-07 18:48:48 +00:00
|
|
|
#[allow(deprecated)]
|
|
|
|
{
|
|
|
|
assert!(arg.is_multiple_occurrences_set());
|
|
|
|
}
|
2022-02-09 17:16:34 +00:00
|
|
|
assert!(arg.is_required_set());
|
2021-11-19 16:19:18 +00:00
|
|
|
assert_eq!(arg.get_help(), None);
|
|
|
|
|
|
|
|
let arg = clap::arg!(<NUM> "How to use it");
|
2022-02-11 20:10:29 +00:00
|
|
|
assert_eq!(arg.get_id(), "NUM");
|
2021-11-19 16:19:18 +00:00
|
|
|
assert_eq!(arg.get_value_names(), Some(vec!["NUM"].as_slice()));
|
2022-06-07 18:48:48 +00:00
|
|
|
#[allow(deprecated)]
|
|
|
|
{
|
|
|
|
assert!(!arg.is_multiple_occurrences_set());
|
|
|
|
}
|
2022-02-09 17:16:34 +00:00
|
|
|
assert!(arg.is_required_set());
|
2021-11-19 16:19:18 +00:00
|
|
|
assert_eq!(arg.get_help(), Some("How to use it"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-06-10 01:03:28 +00:00
|
|
|
#[allow(deprecated)]
|
|
|
|
{
|
|
|
|
assert!(m.is_present("some-arg"));
|
|
|
|
}
|
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-06-10 01:03:28 +00:00
|
|
|
#[allow(deprecated)]
|
|
|
|
{
|
|
|
|
assert!(m.is_present("some-val"));
|
|
|
|
}
|
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-06-10 01:03:28 +00:00
|
|
|
#[allow(deprecated)]
|
|
|
|
{
|
|
|
|
assert!(m.is_present("arg"));
|
|
|
|
}
|
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
|
|
|
}
|