2022-07-24 23:10:31 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2022-02-14 22:09:38 +00:00
|
|
|
use clap::{CommandFactory, Parser};
|
2019-12-29 15:53:12 +00:00
|
|
|
|
2021-11-30 15:36:53 +00:00
|
|
|
use crate::utils;
|
2019-12-29 15:53:12 +00:00
|
|
|
|
|
|
|
#[test]
|
2021-07-28 13:53:32 +00:00
|
|
|
fn default_value() {
|
2021-07-13 17:50:55 +00:00
|
|
|
#[derive(Parser, PartialEq, Debug)]
|
2019-12-29 15:53:12 +00:00
|
|
|
struct Opt {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(default_value = "3")]
|
2021-07-28 13:53:32 +00:00
|
|
|
arg: i32,
|
|
|
|
}
|
2022-11-24 13:54:25 +00:00
|
|
|
assert_eq!(Opt { arg: 3 }, Opt::try_parse_from(["test"]).unwrap());
|
|
|
|
assert_eq!(Opt { arg: 1 }, Opt::try_parse_from(["test", "1"]).unwrap());
|
2021-07-28 13:53:32 +00:00
|
|
|
|
2021-11-30 15:36:53 +00:00
|
|
|
let help = utils::get_long_help::<Opt>();
|
2021-07-28 13:53:32 +00:00
|
|
|
assert!(help.contains("[default: 3]"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_value_t() {
|
2021-07-13 17:50:55 +00:00
|
|
|
#[derive(Parser, PartialEq, Debug)]
|
2021-07-28 13:53:32 +00:00
|
|
|
struct Opt {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(default_value_t = 3)]
|
2021-07-28 13:53:32 +00:00
|
|
|
arg: i32,
|
|
|
|
}
|
2022-11-24 13:54:25 +00:00
|
|
|
assert_eq!(Opt { arg: 3 }, Opt::try_parse_from(["test"]).unwrap());
|
|
|
|
assert_eq!(Opt { arg: 1 }, Opt::try_parse_from(["test", "1"]).unwrap());
|
2021-07-28 13:53:32 +00:00
|
|
|
|
2021-11-30 15:36:53 +00:00
|
|
|
let help = utils::get_long_help::<Opt>();
|
2021-07-28 13:53:32 +00:00
|
|
|
assert!(help.contains("[default: 3]"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn auto_default_value_t() {
|
2021-07-13 17:50:55 +00:00
|
|
|
#[derive(Parser, PartialEq, Debug)]
|
2021-07-28 13:53:32 +00:00
|
|
|
struct Opt {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(default_value_t)]
|
2019-12-29 15:53:12 +00:00
|
|
|
arg: i32,
|
|
|
|
}
|
2022-11-24 13:54:25 +00:00
|
|
|
assert_eq!(Opt { arg: 0 }, Opt::try_parse_from(["test"]).unwrap());
|
|
|
|
assert_eq!(Opt { arg: 1 }, Opt::try_parse_from(["test", "1"]).unwrap());
|
2019-12-29 15:53:12 +00:00
|
|
|
|
2021-11-30 15:36:53 +00:00
|
|
|
let help = utils::get_long_help::<Opt>();
|
2019-12-29 15:53:12 +00:00
|
|
|
assert!(help.contains("[default: 0]"));
|
|
|
|
}
|
2021-12-13 22:19:12 +00:00
|
|
|
|
2022-07-24 23:10:31 +00:00
|
|
|
#[test]
|
|
|
|
fn default_values_t() {
|
|
|
|
#[derive(Parser, PartialEq, Debug)]
|
|
|
|
struct Opt {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(default_values_t = vec![1, 2, 3])]
|
2022-07-24 23:10:31 +00:00
|
|
|
arg1: Vec<i32>,
|
|
|
|
|
2022-11-24 13:54:25 +00:00
|
|
|
#[arg(long, default_values_t = [4, 5, 6])]
|
2022-07-24 23:10:31 +00:00
|
|
|
arg2: Vec<i32>,
|
|
|
|
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(long, default_values_t = [7, 8, 9])]
|
2022-07-24 23:10:31 +00:00
|
|
|
arg3: Vec<i32>,
|
|
|
|
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(long, default_values_t = 10..=12)]
|
2022-07-24 23:10:31 +00:00
|
|
|
arg4: Vec<i32>,
|
|
|
|
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(long, default_values_t = vec!["hello".to_string(), "world".to_string()])]
|
2022-07-24 23:10:31 +00:00
|
|
|
arg5: Vec<String>,
|
|
|
|
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(long, default_values_t = &vec!["foo".to_string(), "bar".to_string()])]
|
2022-07-24 23:10:31 +00:00
|
|
|
arg6: Vec<String>,
|
|
|
|
}
|
|
|
|
assert_eq!(
|
|
|
|
Opt {
|
|
|
|
arg1: vec![1, 2, 3],
|
|
|
|
arg2: vec![4, 5, 6],
|
|
|
|
arg3: vec![7, 8, 9],
|
|
|
|
arg4: vec![10, 11, 12],
|
|
|
|
arg5: vec!["hello".to_string(), "world".to_string()],
|
|
|
|
arg6: vec!["foo".to_string(), "bar".to_string()],
|
|
|
|
},
|
2022-11-24 13:54:25 +00:00
|
|
|
Opt::try_parse_from(["test"]).unwrap()
|
2022-07-24 23:10:31 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
Opt {
|
|
|
|
arg1: vec![1],
|
|
|
|
arg2: vec![4, 5, 6],
|
|
|
|
arg3: vec![7, 8, 9],
|
|
|
|
arg4: vec![10, 11, 12],
|
|
|
|
arg5: vec!["hello".to_string(), "world".to_string()],
|
|
|
|
arg6: vec!["foo".to_string(), "bar".to_string()],
|
|
|
|
},
|
2022-11-24 13:54:25 +00:00
|
|
|
Opt::try_parse_from(["test", "1"]).unwrap()
|
2022-07-24 23:10:31 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
Opt {
|
|
|
|
arg1: vec![1, 2, 3],
|
|
|
|
arg2: vec![4, 5, 6],
|
|
|
|
arg3: vec![7, 8, 9],
|
|
|
|
arg4: vec![42, 15],
|
|
|
|
arg5: vec!["baz".to_string()],
|
|
|
|
arg6: vec!["foo".to_string(), "bar".to_string()],
|
|
|
|
},
|
2022-11-24 13:54:25 +00:00
|
|
|
Opt::try_parse_from(["test", "--arg4", "42", "--arg4", "15", "--arg5", "baz"]).unwrap()
|
2022-07-24 23:10:31 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
let help = utils::get_long_help::<Opt>();
|
|
|
|
assert!(help.contains("[default: 1 2 3]"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_value_os_t() {
|
|
|
|
#[derive(Parser, PartialEq, Debug)]
|
|
|
|
struct Opt {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(default_value_os_t = PathBuf::from("abc.def"))]
|
2022-07-24 23:10:31 +00:00
|
|
|
arg: PathBuf,
|
|
|
|
}
|
|
|
|
assert_eq!(
|
|
|
|
Opt {
|
|
|
|
arg: PathBuf::from("abc.def")
|
|
|
|
},
|
2022-11-24 13:54:25 +00:00
|
|
|
Opt::try_parse_from(["test"]).unwrap()
|
2022-07-24 23:10:31 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
Opt {
|
|
|
|
arg: PathBuf::from("ghi")
|
|
|
|
},
|
2022-11-24 13:54:25 +00:00
|
|
|
Opt::try_parse_from(["test", "ghi"]).unwrap()
|
2022-07-24 23:10:31 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
let help = utils::get_long_help::<Opt>();
|
|
|
|
assert!(help.contains("[default: abc.def]"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_values_os_t() {
|
|
|
|
#[derive(Parser, PartialEq, Debug)]
|
|
|
|
struct Opt {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(
|
2022-07-24 23:10:31 +00:00
|
|
|
default_values_os_t = vec![PathBuf::from("abc.def"), PathBuf::from("123.foo")]
|
|
|
|
)]
|
|
|
|
arg1: Vec<PathBuf>,
|
|
|
|
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(
|
2022-07-24 23:10:31 +00:00
|
|
|
long,
|
2022-11-24 13:54:25 +00:00
|
|
|
default_values_os_t = [PathBuf::from("bar.baz")]
|
2022-07-24 23:10:31 +00:00
|
|
|
)]
|
|
|
|
arg2: Vec<PathBuf>,
|
|
|
|
}
|
|
|
|
assert_eq!(
|
|
|
|
Opt {
|
|
|
|
arg1: vec![PathBuf::from("abc.def"), PathBuf::from("123.foo")],
|
|
|
|
arg2: vec![PathBuf::from("bar.baz")]
|
|
|
|
},
|
2022-11-24 13:54:25 +00:00
|
|
|
Opt::try_parse_from(["test"]).unwrap()
|
2022-07-24 23:10:31 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
Opt {
|
|
|
|
arg1: vec![PathBuf::from("ghi")],
|
|
|
|
arg2: vec![PathBuf::from("baz.bar"), PathBuf::from("foo.bar")]
|
|
|
|
},
|
2022-11-24 13:54:25 +00:00
|
|
|
Opt::try_parse_from(["test", "ghi", "--arg2", "baz.bar", "--arg2", "foo.bar"]).unwrap()
|
2022-07-24 23:10:31 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
let help = utils::get_long_help::<Opt>();
|
|
|
|
assert!(help.contains("[default: abc.def 123.foo]"));
|
|
|
|
}
|
|
|
|
|
2021-12-13 22:19:12 +00:00
|
|
|
#[test]
|
|
|
|
fn detect_os_variant() {
|
2022-08-29 17:42:30 +00:00
|
|
|
#![allow(deprecated)]
|
|
|
|
|
2021-12-13 22:19:12 +00:00
|
|
|
#[derive(clap::Parser)]
|
|
|
|
pub struct Options {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(default_value_os = "123")]
|
2021-12-13 22:19:12 +00:00
|
|
|
x: String,
|
|
|
|
}
|
2022-02-14 22:04:07 +00:00
|
|
|
Options::command().debug_assert();
|
2021-12-13 22:19:12 +00:00
|
|
|
}
|