2017-10-03 23:23:03 +00:00
|
|
|
extern crate clap;
|
|
|
|
|
|
|
|
use std::env;
|
|
|
|
use std::ffi::OsStr;
|
|
|
|
|
2017-10-05 17:41:36 +00:00
|
|
|
use clap::{App, Arg};
|
2017-10-03 23:23:03 +00:00
|
|
|
|
|
|
|
#[test]
|
2017-10-05 17:41:36 +00:00
|
|
|
fn env() {
|
2017-10-03 23:23:03 +00:00
|
|
|
env::set_var("CLP_TEST_ENV", "env");
|
|
|
|
|
|
|
|
let r = App::new("df")
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("[arg] 'some opt'").env("CLP_TEST_ENV"))
|
2017-10-03 23:23:03 +00:00
|
|
|
.get_matches_from_safe(vec![""]);
|
2017-10-04 18:24:54 +00:00
|
|
|
|
2017-10-03 23:23:03 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
2017-10-05 17:41:36 +00:00
|
|
|
assert!(m.is_present("arg"));
|
|
|
|
assert_eq!(m.occurrences_of("arg"), 0);
|
2017-10-03 23:23:03 +00:00
|
|
|
assert_eq!(m.value_of("arg").unwrap(), "env");
|
|
|
|
}
|
2017-10-05 17:41:36 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn env_os() {
|
|
|
|
env::set_var("CLP_TEST_ENV", "env");
|
|
|
|
|
|
|
|
let r = App::new("df")
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("[arg] 'some opt'").env_os(OsStr::new("CLP_TEST_ENV")))
|
2017-10-05 17:41:36 +00:00
|
|
|
.get_matches_from_safe(vec![""]);
|
|
|
|
|
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("arg"));
|
|
|
|
assert_eq!(m.occurrences_of("arg"), 0);
|
|
|
|
assert_eq!(m.value_of("arg").unwrap(), "env");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_env() {
|
|
|
|
// All the other tests use the presence of the Environment variable...
|
|
|
|
// we need another variable just in case one of the others is running at the same time...
|
|
|
|
env::remove_var("CLP_TEST_ENV_NONE");
|
|
|
|
|
|
|
|
let r = App::new("df")
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("[arg] 'some opt'").env("CLP_TEST_ENV_NONE"))
|
2017-10-05 17:41:36 +00:00
|
|
|
.get_matches_from_safe(vec![""]);
|
|
|
|
|
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(!m.is_present("arg"));
|
|
|
|
assert_eq!(m.occurrences_of("arg"), 0);
|
|
|
|
assert_eq!(m.value_of("arg"), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn with_default() {
|
|
|
|
env::set_var("CLP_TEST_ENV", "env");
|
|
|
|
|
|
|
|
let r = App::new("df")
|
|
|
|
.arg(
|
2018-04-21 18:59:19 +00:00
|
|
|
Arg::from("[arg] 'some opt'")
|
2017-10-05 17:41:36 +00:00
|
|
|
.env("CLP_TEST_ENV")
|
|
|
|
.default_value("default"),
|
|
|
|
)
|
|
|
|
.get_matches_from_safe(vec![""]);
|
|
|
|
|
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("arg"));
|
|
|
|
assert_eq!(m.occurrences_of("arg"), 0);
|
|
|
|
assert_eq!(m.value_of("arg").unwrap(), "env");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn opt_user_override() {
|
|
|
|
env::set_var("CLP_TEST_ENV", "env");
|
|
|
|
|
|
|
|
let r = App::new("df")
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("--arg [FILE] 'some arg'").env("CLP_TEST_ENV"))
|
2017-10-05 17:41:36 +00:00
|
|
|
.get_matches_from_safe(vec!["", "--arg", "opt"]);
|
|
|
|
|
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("arg"));
|
|
|
|
assert_eq!(m.occurrences_of("arg"), 1);
|
|
|
|
assert_eq!(m.value_of("arg").unwrap(), "opt");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn positionals() {
|
|
|
|
env::set_var("CLP_TEST_ENV", "env");
|
|
|
|
|
|
|
|
let r = App::new("df")
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("[arg] 'some opt'").env("CLP_TEST_ENV"))
|
2017-10-05 17:41:36 +00:00
|
|
|
.get_matches_from_safe(vec![""]);
|
|
|
|
|
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("arg"));
|
|
|
|
assert_eq!(m.occurrences_of("arg"), 0);
|
|
|
|
assert_eq!(m.value_of("arg").unwrap(), "env");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn positionals_user_override() {
|
|
|
|
env::set_var("CLP_TEST_ENV", "env");
|
|
|
|
|
|
|
|
let r = App::new("df")
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("[arg] 'some opt'").env("CLP_TEST_ENV"))
|
2017-10-05 17:41:36 +00:00
|
|
|
.get_matches_from_safe(vec!["", "opt"]);
|
|
|
|
|
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("arg"));
|
|
|
|
assert_eq!(m.occurrences_of("arg"), 1);
|
|
|
|
assert_eq!(m.value_of("arg").unwrap(), "opt");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiple_one() {
|
|
|
|
env::set_var("CLP_TEST_ENV", "env");
|
|
|
|
|
|
|
|
let r = App::new("df")
|
|
|
|
.arg(
|
2018-04-21 18:59:19 +00:00
|
|
|
Arg::from("[arg] 'some opt'")
|
2017-10-05 17:41:36 +00:00
|
|
|
.env("CLP_TEST_ENV")
|
|
|
|
.use_delimiter(true)
|
|
|
|
.multiple(true),
|
|
|
|
)
|
|
|
|
.get_matches_from_safe(vec![""]);
|
|
|
|
|
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("arg"));
|
|
|
|
assert_eq!(m.occurrences_of("arg"), 0);
|
|
|
|
assert_eq!(m.values_of("arg").unwrap().collect::<Vec<_>>(), vec!["env"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiple_three() {
|
|
|
|
env::set_var("CLP_TEST_ENV_MULTI1", "env1,env2,env3");
|
|
|
|
|
|
|
|
let r = App::new("df")
|
|
|
|
.arg(
|
2018-04-21 18:59:19 +00:00
|
|
|
Arg::from("[arg] 'some opt'")
|
2017-10-05 17:41:36 +00:00
|
|
|
.env("CLP_TEST_ENV_MULTI1")
|
|
|
|
.use_delimiter(true)
|
|
|
|
.multiple(true),
|
|
|
|
)
|
|
|
|
.get_matches_from_safe(vec![""]);
|
|
|
|
|
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("arg"));
|
|
|
|
assert_eq!(m.occurrences_of("arg"), 0);
|
|
|
|
assert_eq!(
|
|
|
|
m.values_of("arg").unwrap().collect::<Vec<_>>(),
|
|
|
|
vec!["env1", "env2", "env3"]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiple_no_delimiter() {
|
|
|
|
env::set_var("CLP_TEST_ENV_MULTI2", "env1 env2 env3");
|
|
|
|
|
|
|
|
let r = App::new("df")
|
|
|
|
.arg(
|
2018-04-21 18:59:19 +00:00
|
|
|
Arg::from("[arg] 'some opt'")
|
2017-10-05 17:41:36 +00:00
|
|
|
.env("CLP_TEST_ENV_MULTI2")
|
|
|
|
.multiple(true),
|
|
|
|
)
|
|
|
|
.get_matches_from_safe(vec![""]);
|
|
|
|
|
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("arg"));
|
|
|
|
assert_eq!(m.occurrences_of("arg"), 0);
|
|
|
|
assert_eq!(
|
|
|
|
m.values_of("arg").unwrap().collect::<Vec<_>>(),
|
|
|
|
vec!["env1 env2 env3"]
|
|
|
|
);
|
|
|
|
}
|
2017-10-05 18:36:50 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn possible_value() {
|
|
|
|
env::set_var("CLP_TEST_ENV", "env");
|
|
|
|
|
|
|
|
let r = App::new("df")
|
|
|
|
.arg(
|
2018-04-21 18:59:19 +00:00
|
|
|
Arg::from("[arg] 'some opt'")
|
2017-10-05 18:36:50 +00:00
|
|
|
.env("CLP_TEST_ENV")
|
|
|
|
.possible_value("env"),
|
|
|
|
)
|
|
|
|
.get_matches_from_safe(vec![""]);
|
|
|
|
|
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("arg"));
|
|
|
|
assert_eq!(m.occurrences_of("arg"), 0);
|
|
|
|
assert_eq!(m.value_of("arg").unwrap(), "env");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn not_possible_value() {
|
|
|
|
env::set_var("CLP_TEST_ENV", "env");
|
|
|
|
|
|
|
|
let r = App::new("df")
|
|
|
|
.arg(
|
2018-04-21 18:59:19 +00:00
|
|
|
Arg::from("[arg] 'some opt'")
|
2017-10-05 18:36:50 +00:00
|
|
|
.env("CLP_TEST_ENV")
|
|
|
|
.possible_value("never"),
|
|
|
|
)
|
|
|
|
.get_matches_from_safe(vec![""]);
|
|
|
|
|
|
|
|
assert!(r.is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn validator() {
|
|
|
|
env::set_var("CLP_TEST_ENV", "env");
|
|
|
|
|
|
|
|
let r = App::new("df")
|
|
|
|
.arg(
|
2018-04-21 18:59:19 +00:00
|
|
|
Arg::from("[arg] 'some opt'")
|
2017-10-05 18:36:50 +00:00
|
|
|
.env("CLP_TEST_ENV")
|
2018-01-25 04:05:05 +00:00
|
|
|
.validator(|s| {
|
|
|
|
if s == "env" {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err("not equal".to_string())
|
|
|
|
}
|
2017-10-05 18:36:50 +00:00
|
|
|
}),
|
|
|
|
)
|
|
|
|
.get_matches_from_safe(vec![""]);
|
|
|
|
|
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("arg"));
|
|
|
|
assert_eq!(m.occurrences_of("arg"), 0);
|
|
|
|
assert_eq!(m.value_of("arg").unwrap(), "env");
|
|
|
|
}
|
|
|
|
|
2018-04-05 00:45:22 +00:00
|
|
|
#[test]
|
|
|
|
fn validator_output() {
|
|
|
|
env::set_var("CLP_TEST_ENV", "42");
|
|
|
|
|
2018-07-23 19:09:42 +00:00
|
|
|
let m = App::new("df")
|
2018-04-05 00:45:22 +00:00
|
|
|
.arg(
|
2018-04-21 18:59:19 +00:00
|
|
|
Arg::from("[arg] 'some opt'")
|
2018-04-05 00:45:22 +00:00
|
|
|
.env("CLP_TEST_ENV")
|
2018-07-23 19:09:42 +00:00
|
|
|
.validator(|s| s.parse::<i32>()),
|
2018-04-05 00:45:22 +00:00
|
|
|
)
|
2018-07-23 19:09:42 +00:00
|
|
|
.get_matches_from(vec![""]);
|
2018-04-05 00:45:22 +00:00
|
|
|
|
2018-07-23 19:09:42 +00:00
|
|
|
assert_eq!(m.value_of("arg").unwrap().parse(), Ok(42));
|
2018-04-05 00:45:22 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 18:36:50 +00:00
|
|
|
#[test]
|
|
|
|
fn validator_invalid() {
|
|
|
|
env::set_var("CLP_TEST_ENV", "env");
|
|
|
|
|
|
|
|
let r = App::new("df")
|
|
|
|
.arg(
|
2018-04-21 18:59:19 +00:00
|
|
|
Arg::from("[arg] 'some opt'")
|
2017-10-05 18:36:50 +00:00
|
|
|
.env("CLP_TEST_ENV")
|
2018-01-25 04:05:05 +00:00
|
|
|
.validator(|s| {
|
|
|
|
if s != "env" {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err("is equal".to_string())
|
|
|
|
}
|
2017-10-05 18:36:50 +00:00
|
|
|
}),
|
|
|
|
)
|
|
|
|
.get_matches_from_safe(vec![""]);
|
|
|
|
|
|
|
|
assert!(r.is_err());
|
|
|
|
}
|