2022-06-01 11:37:53 +00:00
|
|
|
#![allow(clippy::bool_assert_comparison)]
|
|
|
|
|
2022-08-16 16:52:10 +00:00
|
|
|
use clap::builder::ArgPredicate;
|
2022-06-01 01:44:25 +00:00
|
|
|
use clap::Arg;
|
2022-06-03 17:11:45 +00:00
|
|
|
use clap::ArgAction;
|
2022-06-01 01:44:25 +00:00
|
|
|
use clap::Command;
|
|
|
|
|
2022-06-01 15:12:44 +00:00
|
|
|
#[test]
|
|
|
|
fn set() {
|
|
|
|
let cmd = Command::new("test").arg(Arg::new("mammal").long("mammal").action(ArgAction::Set));
|
|
|
|
|
|
|
|
let matches = cmd.clone().try_get_matches_from(["test"]).unwrap();
|
|
|
|
assert_eq!(matches.get_one::<String>("mammal"), None);
|
2022-06-10 01:03:28 +00:00
|
|
|
assert_eq!(matches.contains_id("mammal"), false);
|
2022-06-01 15:12:44 +00:00
|
|
|
assert_eq!(matches.index_of("mammal"), None);
|
|
|
|
|
|
|
|
let matches = cmd
|
|
|
|
.clone()
|
|
|
|
.try_get_matches_from(["test", "--mammal", "dog"])
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(matches.get_one::<String>("mammal").unwrap(), "dog");
|
2022-06-10 01:03:28 +00:00
|
|
|
assert_eq!(matches.contains_id("mammal"), true);
|
2022-06-01 15:12:44 +00:00
|
|
|
assert_eq!(matches.index_of("mammal"), Some(2));
|
|
|
|
|
|
|
|
let matches = cmd
|
|
|
|
.clone()
|
|
|
|
.try_get_matches_from(["test", "--mammal", "dog", "--mammal", "cat"])
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(matches.get_one::<String>("mammal").unwrap(), "cat");
|
2022-06-10 01:03:28 +00:00
|
|
|
assert_eq!(matches.contains_id("mammal"), true);
|
2022-06-01 15:12:44 +00:00
|
|
|
assert_eq!(matches.index_of("mammal"), Some(4));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn append() {
|
|
|
|
let cmd = Command::new("test").arg(Arg::new("mammal").long("mammal").action(ArgAction::Append));
|
|
|
|
|
|
|
|
let matches = cmd.clone().try_get_matches_from(["test"]).unwrap();
|
|
|
|
assert_eq!(matches.get_one::<String>("mammal"), None);
|
2022-06-10 01:03:28 +00:00
|
|
|
assert_eq!(matches.contains_id("mammal"), false);
|
2022-06-01 15:12:44 +00:00
|
|
|
assert_eq!(matches.index_of("mammal"), None);
|
|
|
|
|
|
|
|
let matches = cmd
|
|
|
|
.clone()
|
|
|
|
.try_get_matches_from(["test", "--mammal", "dog"])
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(matches.get_one::<String>("mammal").unwrap(), "dog");
|
2022-06-10 01:03:28 +00:00
|
|
|
assert_eq!(matches.contains_id("mammal"), true);
|
2022-06-01 15:12:44 +00:00
|
|
|
assert_eq!(
|
|
|
|
matches.indices_of("mammal").unwrap().collect::<Vec<_>>(),
|
|
|
|
vec![2]
|
|
|
|
);
|
|
|
|
|
|
|
|
let matches = cmd
|
|
|
|
.clone()
|
|
|
|
.try_get_matches_from(["test", "--mammal", "dog", "--mammal", "cat"])
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
matches
|
|
|
|
.get_many::<String>("mammal")
|
|
|
|
.unwrap()
|
|
|
|
.map(|s| s.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
vec!["dog", "cat"]
|
|
|
|
);
|
2022-06-10 01:03:28 +00:00
|
|
|
assert_eq!(matches.contains_id("mammal"), true);
|
2022-06-01 15:12:44 +00:00
|
|
|
assert_eq!(
|
|
|
|
matches.indices_of("mammal").unwrap().collect::<Vec<_>>(),
|
|
|
|
vec![2, 4]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-06-01 01:57:32 +00:00
|
|
|
#[test]
|
|
|
|
fn set_true() {
|
|
|
|
let cmd =
|
|
|
|
Command::new("test").arg(Arg::new("mammal").long("mammal").action(ArgAction::SetTrue));
|
|
|
|
|
|
|
|
let matches = cmd.clone().try_get_matches_from(["test"]).unwrap();
|
2022-06-03 15:01:49 +00:00
|
|
|
assert_eq!(*matches.get_one::<bool>("mammal").unwrap(), false);
|
2022-06-10 01:03:28 +00:00
|
|
|
assert_eq!(matches.contains_id("mammal"), true);
|
2022-06-03 15:01:49 +00:00
|
|
|
assert_eq!(matches.index_of("mammal"), Some(1));
|
2022-06-01 01:57:32 +00:00
|
|
|
|
|
|
|
let matches = cmd
|
|
|
|
.clone()
|
|
|
|
.try_get_matches_from(["test", "--mammal"])
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(*matches.get_one::<bool>("mammal").unwrap(), true);
|
2022-06-10 01:03:28 +00:00
|
|
|
assert_eq!(matches.contains_id("mammal"), true);
|
2022-06-01 01:57:32 +00:00
|
|
|
assert_eq!(matches.index_of("mammal"), Some(1));
|
|
|
|
|
|
|
|
let matches = cmd
|
|
|
|
.clone()
|
|
|
|
.try_get_matches_from(["test", "--mammal", "--mammal"])
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(*matches.get_one::<bool>("mammal").unwrap(), true);
|
2022-06-10 01:03:28 +00:00
|
|
|
assert_eq!(matches.contains_id("mammal"), true);
|
2022-06-01 01:57:32 +00:00
|
|
|
assert_eq!(matches.index_of("mammal"), Some(2));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-06-03 15:01:49 +00:00
|
|
|
fn set_true_with_explicit_default_value() {
|
2022-06-01 01:57:32 +00:00
|
|
|
let cmd = Command::new("test").arg(
|
|
|
|
Arg::new("mammal")
|
|
|
|
.long("mammal")
|
|
|
|
.action(ArgAction::SetTrue)
|
|
|
|
.default_value("false"),
|
|
|
|
);
|
|
|
|
|
|
|
|
let matches = cmd
|
|
|
|
.clone()
|
|
|
|
.try_get_matches_from(["test", "--mammal"])
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(*matches.get_one::<bool>("mammal").unwrap(), true);
|
2022-06-10 01:03:28 +00:00
|
|
|
assert_eq!(matches.contains_id("mammal"), true);
|
2022-06-01 01:57:32 +00:00
|
|
|
assert_eq!(matches.index_of("mammal"), Some(1));
|
|
|
|
|
|
|
|
let matches = cmd.clone().try_get_matches_from(["test"]).unwrap();
|
|
|
|
assert_eq!(*matches.get_one::<bool>("mammal").unwrap(), false);
|
2022-06-10 01:03:28 +00:00
|
|
|
assert_eq!(matches.contains_id("mammal"), true);
|
2022-06-01 01:57:32 +00:00
|
|
|
assert_eq!(matches.index_of("mammal"), Some(1));
|
|
|
|
}
|
|
|
|
|
2022-06-01 01:44:25 +00:00
|
|
|
#[test]
|
|
|
|
fn set_true_with_default_value_if_present() {
|
|
|
|
let cmd = Command::new("test")
|
|
|
|
.arg(
|
|
|
|
Arg::new("mammal")
|
|
|
|
.long("mammal")
|
|
|
|
.action(ArgAction::SetTrue)
|
2022-08-16 16:52:10 +00:00
|
|
|
.default_value_if("dog", ArgPredicate::IsPresent, Some("true")),
|
2022-06-01 01:44:25 +00:00
|
|
|
)
|
|
|
|
.arg(Arg::new("dog").long("dog").action(ArgAction::SetTrue));
|
|
|
|
|
2022-06-03 15:01:49 +00:00
|
|
|
let matches = cmd.clone().try_get_matches_from(["test"]).unwrap();
|
|
|
|
assert_eq!(*matches.get_one::<bool>("dog").unwrap(), false);
|
|
|
|
assert_eq!(*matches.get_one::<bool>("mammal").unwrap(), false);
|
|
|
|
|
2022-06-01 01:44:25 +00:00
|
|
|
let matches = cmd.clone().try_get_matches_from(["test", "--dog"]).unwrap();
|
|
|
|
assert_eq!(*matches.get_one::<bool>("dog").unwrap(), true);
|
|
|
|
assert_eq!(*matches.get_one::<bool>("mammal").unwrap(), true);
|
|
|
|
|
|
|
|
let matches = cmd
|
|
|
|
.clone()
|
|
|
|
.try_get_matches_from(["test", "--mammal"])
|
|
|
|
.unwrap();
|
2022-06-03 15:01:49 +00:00
|
|
|
assert_eq!(*matches.get_one::<bool>("dog").unwrap(), false);
|
2022-06-01 01:44:25 +00:00
|
|
|
assert_eq!(*matches.get_one::<bool>("mammal").unwrap(), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn set_true_with_default_value_if_value() {
|
|
|
|
let cmd = Command::new("test")
|
|
|
|
.arg(
|
|
|
|
Arg::new("mammal")
|
|
|
|
.long("mammal")
|
|
|
|
.action(ArgAction::SetTrue)
|
2022-08-16 16:52:10 +00:00
|
|
|
.default_value_if("dog", "true", Some("true")),
|
2022-06-01 01:44:25 +00:00
|
|
|
)
|
|
|
|
.arg(Arg::new("dog").long("dog").action(ArgAction::SetTrue));
|
|
|
|
|
2022-06-03 15:01:49 +00:00
|
|
|
let matches = cmd.clone().try_get_matches_from(["test"]).unwrap();
|
|
|
|
assert_eq!(*matches.get_one::<bool>("dog").unwrap(), false);
|
|
|
|
assert_eq!(*matches.get_one::<bool>("mammal").unwrap(), false);
|
|
|
|
|
2022-06-01 01:44:25 +00:00
|
|
|
let matches = cmd.clone().try_get_matches_from(["test", "--dog"]).unwrap();
|
|
|
|
assert_eq!(*matches.get_one::<bool>("dog").unwrap(), true);
|
|
|
|
assert_eq!(*matches.get_one::<bool>("mammal").unwrap(), true);
|
|
|
|
|
|
|
|
let matches = cmd
|
|
|
|
.clone()
|
|
|
|
.try_get_matches_from(["test", "--mammal"])
|
|
|
|
.unwrap();
|
2022-06-03 15:01:49 +00:00
|
|
|
assert_eq!(*matches.get_one::<bool>("dog").unwrap(), false);
|
2022-06-01 01:44:25 +00:00
|
|
|
assert_eq!(*matches.get_one::<bool>("mammal").unwrap(), true);
|
|
|
|
}
|
2022-06-01 01:57:32 +00:00
|
|
|
|
2022-06-01 02:07:20 +00:00
|
|
|
#[test]
|
|
|
|
fn set_true_with_required_if_eq() {
|
|
|
|
let cmd = Command::new("test")
|
|
|
|
.arg(
|
|
|
|
Arg::new("mammal")
|
|
|
|
.long("mammal")
|
|
|
|
.action(ArgAction::SetTrue)
|
|
|
|
.required_if_eq("dog", "true"),
|
|
|
|
)
|
|
|
|
.arg(Arg::new("dog").long("dog").action(ArgAction::SetTrue));
|
|
|
|
|
|
|
|
let matches = cmd
|
|
|
|
.clone()
|
|
|
|
.try_get_matches_from(["test", "--mammal"])
|
|
|
|
.unwrap();
|
2022-06-03 15:01:49 +00:00
|
|
|
assert_eq!(*matches.get_one::<bool>("dog").unwrap(), false);
|
2022-06-01 02:07:20 +00:00
|
|
|
assert_eq!(*matches.get_one::<bool>("mammal").unwrap(), true);
|
|
|
|
|
2022-06-06 15:52:41 +00:00
|
|
|
cmd.clone()
|
|
|
|
.try_get_matches_from(["test", "--dog"])
|
|
|
|
.unwrap_err();
|
2022-06-01 02:07:20 +00:00
|
|
|
|
|
|
|
let matches = cmd
|
|
|
|
.clone()
|
|
|
|
.try_get_matches_from(["test", "--dog", "--mammal"])
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(*matches.get_one::<bool>("dog").unwrap(), true);
|
|
|
|
assert_eq!(*matches.get_one::<bool>("mammal").unwrap(), true);
|
|
|
|
}
|
|
|
|
|
2022-06-01 01:57:32 +00:00
|
|
|
#[test]
|
|
|
|
fn set_false() {
|
|
|
|
let cmd = Command::new("test").arg(
|
|
|
|
Arg::new("mammal")
|
|
|
|
.long("mammal")
|
|
|
|
.action(ArgAction::SetFalse),
|
|
|
|
);
|
|
|
|
|
|
|
|
let matches = cmd.clone().try_get_matches_from(["test"]).unwrap();
|
2022-06-03 15:01:49 +00:00
|
|
|
assert_eq!(*matches.get_one::<bool>("mammal").unwrap(), true);
|
2022-06-10 01:03:28 +00:00
|
|
|
assert_eq!(matches.contains_id("mammal"), true);
|
2022-06-03 15:01:49 +00:00
|
|
|
assert_eq!(matches.index_of("mammal"), Some(1));
|
2022-06-01 01:57:32 +00:00
|
|
|
|
|
|
|
let matches = cmd
|
|
|
|
.clone()
|
|
|
|
.try_get_matches_from(["test", "--mammal"])
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(*matches.get_one::<bool>("mammal").unwrap(), false);
|
2022-06-10 01:03:28 +00:00
|
|
|
assert_eq!(matches.contains_id("mammal"), true);
|
2022-06-01 01:57:32 +00:00
|
|
|
assert_eq!(matches.index_of("mammal"), Some(1));
|
|
|
|
|
|
|
|
let matches = cmd
|
|
|
|
.clone()
|
|
|
|
.try_get_matches_from(["test", "--mammal", "--mammal"])
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(*matches.get_one::<bool>("mammal").unwrap(), false);
|
2022-06-10 01:03:28 +00:00
|
|
|
assert_eq!(matches.contains_id("mammal"), true);
|
2022-06-01 01:57:32 +00:00
|
|
|
assert_eq!(matches.index_of("mammal"), Some(2));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-06-03 15:01:49 +00:00
|
|
|
fn set_false_with_explicit_default_value() {
|
2022-06-01 01:57:32 +00:00
|
|
|
let cmd = Command::new("test").arg(
|
|
|
|
Arg::new("mammal")
|
|
|
|
.long("mammal")
|
|
|
|
.action(ArgAction::SetFalse)
|
|
|
|
.default_value("true"),
|
|
|
|
);
|
|
|
|
|
|
|
|
let matches = cmd
|
|
|
|
.clone()
|
|
|
|
.try_get_matches_from(["test", "--mammal"])
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(*matches.get_one::<bool>("mammal").unwrap(), false);
|
2022-06-10 01:03:28 +00:00
|
|
|
assert_eq!(matches.contains_id("mammal"), true);
|
2022-06-01 01:57:32 +00:00
|
|
|
assert_eq!(matches.index_of("mammal"), Some(1));
|
|
|
|
|
|
|
|
let matches = cmd.clone().try_get_matches_from(["test"]).unwrap();
|
|
|
|
assert_eq!(*matches.get_one::<bool>("mammal").unwrap(), true);
|
2022-06-10 01:03:28 +00:00
|
|
|
assert_eq!(matches.contains_id("mammal"), true);
|
2022-06-01 01:57:32 +00:00
|
|
|
assert_eq!(matches.index_of("mammal"), Some(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn set_false_with_default_value_if_present() {
|
|
|
|
let cmd = Command::new("test")
|
|
|
|
.arg(
|
|
|
|
Arg::new("mammal")
|
|
|
|
.long("mammal")
|
|
|
|
.action(ArgAction::SetFalse)
|
2022-08-16 16:52:10 +00:00
|
|
|
.default_value_if("dog", ArgPredicate::IsPresent, Some("false")),
|
2022-06-01 01:57:32 +00:00
|
|
|
)
|
|
|
|
.arg(Arg::new("dog").long("dog").action(ArgAction::SetFalse));
|
|
|
|
|
2022-06-03 15:01:49 +00:00
|
|
|
let matches = cmd.clone().try_get_matches_from(["test"]).unwrap();
|
|
|
|
assert_eq!(*matches.get_one::<bool>("dog").unwrap(), true);
|
|
|
|
assert_eq!(*matches.get_one::<bool>("mammal").unwrap(), true);
|
|
|
|
|
2022-06-01 01:57:32 +00:00
|
|
|
let matches = cmd.clone().try_get_matches_from(["test", "--dog"]).unwrap();
|
|
|
|
assert_eq!(*matches.get_one::<bool>("dog").unwrap(), false);
|
|
|
|
assert_eq!(*matches.get_one::<bool>("mammal").unwrap(), false);
|
|
|
|
|
|
|
|
let matches = cmd
|
|
|
|
.clone()
|
|
|
|
.try_get_matches_from(["test", "--mammal"])
|
|
|
|
.unwrap();
|
2022-06-03 15:01:49 +00:00
|
|
|
assert_eq!(*matches.get_one::<bool>("dog").unwrap(), true);
|
2022-06-01 01:57:32 +00:00
|
|
|
assert_eq!(*matches.get_one::<bool>("mammal").unwrap(), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn set_false_with_default_value_if_value() {
|
|
|
|
let cmd = Command::new("test")
|
|
|
|
.arg(
|
|
|
|
Arg::new("mammal")
|
|
|
|
.long("mammal")
|
|
|
|
.action(ArgAction::SetFalse)
|
2022-08-16 16:52:10 +00:00
|
|
|
.default_value_if("dog", "false", Some("false")),
|
2022-06-01 01:57:32 +00:00
|
|
|
)
|
|
|
|
.arg(Arg::new("dog").long("dog").action(ArgAction::SetFalse));
|
|
|
|
|
2022-06-03 15:01:49 +00:00
|
|
|
let matches = cmd.clone().try_get_matches_from(["test"]).unwrap();
|
|
|
|
assert_eq!(*matches.get_one::<bool>("dog").unwrap(), true);
|
|
|
|
assert_eq!(*matches.get_one::<bool>("mammal").unwrap(), true);
|
|
|
|
|
2022-06-01 01:57:32 +00:00
|
|
|
let matches = cmd.clone().try_get_matches_from(["test", "--dog"]).unwrap();
|
|
|
|
assert_eq!(*matches.get_one::<bool>("dog").unwrap(), false);
|
|
|
|
assert_eq!(*matches.get_one::<bool>("mammal").unwrap(), false);
|
|
|
|
|
|
|
|
let matches = cmd
|
|
|
|
.clone()
|
|
|
|
.try_get_matches_from(["test", "--mammal"])
|
|
|
|
.unwrap();
|
2022-06-03 15:01:49 +00:00
|
|
|
assert_eq!(*matches.get_one::<bool>("dog").unwrap(), true);
|
2022-06-01 01:57:32 +00:00
|
|
|
assert_eq!(*matches.get_one::<bool>("mammal").unwrap(), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn count() {
|
|
|
|
let cmd = Command::new("test").arg(Arg::new("mammal").long("mammal").action(ArgAction::Count));
|
|
|
|
|
|
|
|
let matches = cmd.clone().try_get_matches_from(["test"]).unwrap();
|
2022-06-09 16:09:36 +00:00
|
|
|
assert_eq!(*matches.get_one::<u8>("mammal").unwrap(), 0);
|
2022-06-10 01:03:28 +00:00
|
|
|
assert_eq!(matches.contains_id("mammal"), true);
|
2022-06-03 15:01:49 +00:00
|
|
|
assert_eq!(matches.index_of("mammal"), Some(1));
|
2022-06-01 01:57:32 +00:00
|
|
|
|
|
|
|
let matches = cmd
|
|
|
|
.clone()
|
|
|
|
.try_get_matches_from(["test", "--mammal"])
|
|
|
|
.unwrap();
|
2022-06-09 16:09:36 +00:00
|
|
|
assert_eq!(*matches.get_one::<u8>("mammal").unwrap(), 1);
|
2022-06-10 01:03:28 +00:00
|
|
|
assert_eq!(matches.contains_id("mammal"), true);
|
2022-06-01 01:57:32 +00:00
|
|
|
assert_eq!(matches.index_of("mammal"), Some(1));
|
|
|
|
|
|
|
|
let matches = cmd
|
|
|
|
.clone()
|
|
|
|
.try_get_matches_from(["test", "--mammal", "--mammal"])
|
|
|
|
.unwrap();
|
2022-06-09 16:09:36 +00:00
|
|
|
assert_eq!(*matches.get_one::<u8>("mammal").unwrap(), 2);
|
2022-06-10 01:03:28 +00:00
|
|
|
assert_eq!(matches.contains_id("mammal"), true);
|
2022-06-01 01:57:32 +00:00
|
|
|
assert_eq!(matches.index_of("mammal"), Some(2));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-06-03 15:01:49 +00:00
|
|
|
fn count_with_explicit_default_value() {
|
2022-06-01 01:57:32 +00:00
|
|
|
let cmd = Command::new("test").arg(
|
|
|
|
Arg::new("mammal")
|
|
|
|
.long("mammal")
|
|
|
|
.action(ArgAction::Count)
|
|
|
|
.default_value("10"),
|
|
|
|
);
|
|
|
|
|
|
|
|
let matches = cmd
|
|
|
|
.clone()
|
|
|
|
.try_get_matches_from(["test", "--mammal"])
|
|
|
|
.unwrap();
|
2022-06-09 16:09:36 +00:00
|
|
|
assert_eq!(*matches.get_one::<u8>("mammal").unwrap(), 1);
|
2022-06-10 01:03:28 +00:00
|
|
|
assert_eq!(matches.contains_id("mammal"), true);
|
2022-06-01 01:57:32 +00:00
|
|
|
assert_eq!(matches.index_of("mammal"), Some(1));
|
|
|
|
|
|
|
|
let matches = cmd.clone().try_get_matches_from(["test"]).unwrap();
|
2022-06-09 16:09:36 +00:00
|
|
|
assert_eq!(*matches.get_one::<u8>("mammal").unwrap(), 10);
|
2022-06-10 01:03:28 +00:00
|
|
|
assert_eq!(matches.contains_id("mammal"), true);
|
2022-06-01 01:57:32 +00:00
|
|
|
assert_eq!(matches.index_of("mammal"), Some(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn count_with_default_value_if_present() {
|
|
|
|
let cmd = Command::new("test")
|
|
|
|
.arg(
|
|
|
|
Arg::new("mammal")
|
|
|
|
.long("mammal")
|
|
|
|
.action(ArgAction::Count)
|
2022-08-16 16:52:10 +00:00
|
|
|
.default_value_if("dog", ArgPredicate::IsPresent, Some("10")),
|
2022-06-01 01:57:32 +00:00
|
|
|
)
|
|
|
|
.arg(Arg::new("dog").long("dog").action(ArgAction::Count));
|
|
|
|
|
2022-06-03 15:01:49 +00:00
|
|
|
let matches = cmd.clone().try_get_matches_from(["test"]).unwrap();
|
2022-06-09 16:09:36 +00:00
|
|
|
assert_eq!(*matches.get_one::<u8>("dog").unwrap(), 0);
|
|
|
|
assert_eq!(*matches.get_one::<u8>("mammal").unwrap(), 0);
|
2022-06-03 15:01:49 +00:00
|
|
|
|
2022-06-01 01:57:32 +00:00
|
|
|
let matches = cmd.clone().try_get_matches_from(["test", "--dog"]).unwrap();
|
2022-06-09 16:09:36 +00:00
|
|
|
assert_eq!(*matches.get_one::<u8>("dog").unwrap(), 1);
|
|
|
|
assert_eq!(*matches.get_one::<u8>("mammal").unwrap(), 10);
|
2022-06-01 01:57:32 +00:00
|
|
|
|
|
|
|
let matches = cmd
|
|
|
|
.clone()
|
|
|
|
.try_get_matches_from(["test", "--mammal"])
|
|
|
|
.unwrap();
|
2022-06-09 16:09:36 +00:00
|
|
|
assert_eq!(*matches.get_one::<u8>("dog").unwrap(), 0);
|
|
|
|
assert_eq!(*matches.get_one::<u8>("mammal").unwrap(), 1);
|
2022-06-01 01:57:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn count_with_default_value_if_value() {
|
|
|
|
let cmd = Command::new("test")
|
|
|
|
.arg(
|
|
|
|
Arg::new("mammal")
|
|
|
|
.long("mammal")
|
|
|
|
.action(ArgAction::Count)
|
2022-08-16 16:52:10 +00:00
|
|
|
.default_value_if("dog", "2", Some("10")),
|
2022-06-01 01:57:32 +00:00
|
|
|
)
|
|
|
|
.arg(Arg::new("dog").long("dog").action(ArgAction::Count));
|
|
|
|
|
2022-06-03 15:01:49 +00:00
|
|
|
let matches = cmd.clone().try_get_matches_from(["test"]).unwrap();
|
2022-06-09 16:09:36 +00:00
|
|
|
assert_eq!(*matches.get_one::<u8>("dog").unwrap(), 0);
|
|
|
|
assert_eq!(*matches.get_one::<u8>("mammal").unwrap(), 0);
|
2022-06-03 15:01:49 +00:00
|
|
|
|
2022-06-01 01:57:32 +00:00
|
|
|
let matches = cmd.clone().try_get_matches_from(["test", "--dog"]).unwrap();
|
2022-06-09 16:09:36 +00:00
|
|
|
assert_eq!(*matches.get_one::<u8>("dog").unwrap(), 1);
|
|
|
|
assert_eq!(*matches.get_one::<u8>("mammal").unwrap(), 0);
|
2022-06-01 01:57:32 +00:00
|
|
|
|
|
|
|
let matches = cmd
|
|
|
|
.clone()
|
|
|
|
.try_get_matches_from(["test", "--dog", "--dog"])
|
|
|
|
.unwrap();
|
2022-06-09 16:09:36 +00:00
|
|
|
assert_eq!(*matches.get_one::<u8>("dog").unwrap(), 2);
|
|
|
|
assert_eq!(*matches.get_one::<u8>("mammal").unwrap(), 10);
|
2022-06-01 01:57:32 +00:00
|
|
|
|
|
|
|
let matches = cmd
|
|
|
|
.clone()
|
|
|
|
.try_get_matches_from(["test", "--mammal"])
|
|
|
|
.unwrap();
|
2022-06-09 16:09:36 +00:00
|
|
|
assert_eq!(*matches.get_one::<u8>("dog").unwrap(), 0);
|
|
|
|
assert_eq!(*matches.get_one::<u8>("mammal").unwrap(), 1);
|
2022-06-01 01:57:32 +00:00
|
|
|
}
|