mirror of
https://github.com/clap-rs/clap
synced 2024-11-10 14:54:15 +00:00
fix: Revert as much yaml policy to v2 as possible
This commit is contained in:
parent
0294fd0170
commit
3f9da04744
22 changed files with 61 additions and 569 deletions
|
@ -422,34 +422,26 @@ impl<'help> App<'help> {
|
|||
)
|
||||
};
|
||||
|
||||
let mut has_metadata = false;
|
||||
|
||||
for (k, v) in yaml {
|
||||
a = match k.as_str().expect("App fields must be strings") {
|
||||
"_has_metadata" => {
|
||||
has_metadata = true;
|
||||
a
|
||||
}
|
||||
"bin_name" => yaml_to_str!(a, v, bin_name),
|
||||
"version" => yaml_to_str!(a, v, version),
|
||||
"long_version" => yaml_to_str!(a, v, long_version),
|
||||
"author" => yaml_to_str!(a, v, author),
|
||||
"bin_name" => yaml_to_str!(a, v, bin_name),
|
||||
"about" => yaml_to_str!(a, v, about),
|
||||
"long_about" => yaml_to_str!(a, v, long_about),
|
||||
"before_help" => yaml_to_str!(a, v, before_help),
|
||||
"before_long_help" => yaml_to_str!(a, v, before_long_help),
|
||||
"after_help" => yaml_to_str!(a, v, after_help),
|
||||
"after_long_help" => yaml_to_str!(a, v, after_long_help),
|
||||
"help_heading" => yaml_to_str!(a, v, help_heading),
|
||||
"help_template" => yaml_to_str!(a, v, help_template),
|
||||
"override_help" => yaml_to_str!(a, v, override_help),
|
||||
"override_usage" => yaml_to_str!(a, v, override_usage),
|
||||
"template" => yaml_to_str!(a, v, help_template),
|
||||
"usage" => yaml_to_str!(a, v, override_usage),
|
||||
"help" => yaml_to_str!(a, v, override_help),
|
||||
"help_message" => yaml_to_str!(a, v, help_message),
|
||||
"version_message" => yaml_to_str!(a, v, version_message),
|
||||
"alias" => yaml_to_str!(a, v, alias),
|
||||
"aliases" => yaml_vec_or_str!(a, v, alias),
|
||||
"visible_alias" => yaml_to_str!(a, v, visible_alias),
|
||||
"visible_aliases" => yaml_vec_or_str!(a, v, visible_alias),
|
||||
"display_order" => yaml_to_usize!(a, v, display_order),
|
||||
"term_width" => yaml_to_usize!(a, v, term_width),
|
||||
"max_term_width" => yaml_to_usize!(a, v, max_term_width),
|
||||
"args" => {
|
||||
if let Some(vec) = v.as_vec() {
|
||||
for arg_yaml in vec {
|
||||
|
@ -486,13 +478,7 @@ impl<'help> App<'help> {
|
|||
"global_setting" | "global_settings" => {
|
||||
yaml_to_setting!(a, v, global_setting, AppSettings, "AppSetting", err)
|
||||
}
|
||||
"name" => continue,
|
||||
s => {
|
||||
if !has_metadata {
|
||||
panic!("Unknown setting '{}' in YAML file for {}", s, err)
|
||||
}
|
||||
continue;
|
||||
}
|
||||
_ => a,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -103,11 +103,11 @@ impl_settings! { AppSettings, AppFlags,
|
|||
=> Flags::DISABLE_HELP_SC,
|
||||
DisableHelpFlag("disablehelpflag")
|
||||
=> Flags::DISABLE_HELP_FLAG,
|
||||
DisableHelpFlags("disablehelpflag")
|
||||
DisableHelpFlags("disablehelpflags")
|
||||
=> Flags::DISABLE_HELP_FLAG,
|
||||
DisableVersionFlag("disableversionflag")
|
||||
=> Flags::DISABLE_VERSION_FLAG,
|
||||
DisableVersion("disableversionflag")
|
||||
DisableVersion("disableversion")
|
||||
=> Flags::DISABLE_VERSION_FLAG,
|
||||
PropagateVersion("propagateversion")
|
||||
=> Flags::PROPAGATE_VERSION,
|
||||
|
|
|
@ -23,10 +23,6 @@ use std::{
|
|||
#[cfg(feature = "env")]
|
||||
use std::{env, ffi::OsString};
|
||||
|
||||
// Third Party
|
||||
#[cfg(feature = "regex")]
|
||||
use ::regex::Regex;
|
||||
|
||||
#[cfg(feature = "yaml")]
|
||||
use yaml_rust::Yaml;
|
||||
|
||||
|
@ -355,6 +351,7 @@ impl<'help> Arg<'help> {
|
|||
note = "Maybe clap::Parser would fit your use case? (Issue #9)"
|
||||
)]
|
||||
pub fn from_yaml(y: &'help Yaml) -> Self {
|
||||
#![allow(deprecated)]
|
||||
let yaml_file_hash = y.as_hash().expect("YAML file must be a hash");
|
||||
// We WANT this to panic on error...so expect() is good.
|
||||
let (name_yaml, yaml) = yaml_file_hash
|
||||
|
@ -364,33 +361,21 @@ impl<'help> Arg<'help> {
|
|||
let name_str = name_yaml.as_str().expect("Arg name must be a string");
|
||||
let mut a = Arg::new(name_str);
|
||||
|
||||
let mut has_metadata = false;
|
||||
|
||||
for (k, v) in yaml.as_hash().expect("Arg must be a hash") {
|
||||
a = match k.as_str().expect("Arg fields must be strings") {
|
||||
"_has_metadata" => {
|
||||
has_metadata = true;
|
||||
a
|
||||
}
|
||||
"short" => yaml_to_char!(a, v, short),
|
||||
"long" => yaml_to_str!(a, v, long),
|
||||
"alias" => yaml_to_str!(a, v, alias),
|
||||
"aliases" => yaml_vec_or_str!(a, v, alias),
|
||||
"short_alias" => yaml_to_str!(a, v, alias),
|
||||
"short_aliases" => yaml_to_chars!(a, v, short_aliases),
|
||||
"help" => yaml_to_str!(a, v, help),
|
||||
"long_help" => yaml_to_str!(a, v, long_help),
|
||||
"required" => yaml_to_bool!(a, v, required),
|
||||
"required_if_eq" => yaml_tuple2!(a, v, required_if_eq),
|
||||
"required_if_eq_any" => yaml_array_tuple2!(a, v, required_if_eq_any),
|
||||
"required_if_eq_all" => yaml_array_tuple2!(a, v, required_if_eq_all),
|
||||
"required_if" => yaml_tuple2!(a, v, required_if_eq),
|
||||
"required_ifs" => yaml_tuple2!(a, v, required_if_eq),
|
||||
"takes_value" => yaml_to_bool!(a, v, takes_value),
|
||||
"index" => yaml_to_usize!(a, v, index),
|
||||
"multiple_occurrences" => yaml_to_bool!(a, v, multiple_occurrences),
|
||||
"multiple_values" => yaml_to_bool!(a, v, multiple_values),
|
||||
"hide" => yaml_to_bool!(a, v, hide),
|
||||
"hide_long_help" => yaml_to_bool!(a, v, hide_long_help),
|
||||
"hide_short_help" => yaml_to_bool!(a, v, hide_short_help),
|
||||
"global" => yaml_to_bool!(a, v, global),
|
||||
"multiple" => yaml_to_bool!(a, v, multiple),
|
||||
"hidden" => yaml_to_bool!(a, v, hide),
|
||||
"next_line_help" => yaml_to_bool!(a, v, next_line_help),
|
||||
"group" => yaml_to_str!(a, v, group),
|
||||
"number_of_values" => yaml_to_usize!(a, v, number_of_values),
|
||||
|
@ -399,17 +384,14 @@ impl<'help> Arg<'help> {
|
|||
"value_name" => yaml_to_str!(a, v, value_name),
|
||||
"use_delimiter" => yaml_to_bool!(a, v, use_delimiter),
|
||||
"allow_hyphen_values" => yaml_to_bool!(a, v, allow_hyphen_values),
|
||||
"raw" => yaml_to_bool!(a, v, raw),
|
||||
"require_equals" => yaml_to_bool!(a, v, require_equals),
|
||||
"last" => yaml_to_bool!(a, v, last),
|
||||
"require_delimiter" => yaml_to_bool!(a, v, require_delimiter),
|
||||
"value_terminator" => yaml_to_str!(a, v, value_terminator),
|
||||
"value_delimiter" => yaml_to_char!(a, v, value_delimiter),
|
||||
"required_unless_present" => yaml_to_str!(a, v, required_unless_present),
|
||||
"required_unless" => yaml_to_str!(a, v, required_unless_present),
|
||||
"display_order" => yaml_to_usize!(a, v, display_order),
|
||||
"default_value" => yaml_to_str!(a, v, default_value),
|
||||
"default_value_if" => yaml_tuple3!(a, v, default_value_if),
|
||||
"default_value_ifs" => yaml_tuple3!(a, v, default_value_if),
|
||||
"default_missing_value" => yaml_to_str!(a, v, default_missing_value),
|
||||
#[cfg(feature = "env")]
|
||||
"env" => yaml_to_str!(a, v, env),
|
||||
"value_names" => yaml_vec_or_str!(a, v, value_name),
|
||||
|
@ -418,62 +400,16 @@ impl<'help> Arg<'help> {
|
|||
"requires_if" => yaml_tuple2!(a, v, requires_if),
|
||||
"requires_ifs" => yaml_tuple2!(a, v, requires_if),
|
||||
"conflicts_with" => yaml_vec_or_str!(a, v, conflicts_with),
|
||||
"exclusive" => yaml_to_bool!(a, v, exclusive),
|
||||
"last" => yaml_to_bool!(a, v, last),
|
||||
"help_heading" => yaml_to_str!(a, v, help_heading),
|
||||
"value_hint" => yaml_str_parse!(a, v, value_hint),
|
||||
"hide_default_value" => yaml_to_bool!(a, v, hide_default_value),
|
||||
#[cfg(feature = "env")]
|
||||
"hide_env" => yaml_to_bool!(a, v, hide_env),
|
||||
#[cfg(feature = "env")]
|
||||
"hide_env_values" => yaml_to_bool!(a, v, hide_env_values),
|
||||
"hide_possible_values" => yaml_to_bool!(a, v, hide_possible_values),
|
||||
"overrides_with" => yaml_to_str!(a, v, overrides_with),
|
||||
"overrides_with_all" => yaml_vec_or_str!(a, v, overrides_with),
|
||||
"possible_value" => yaml_to_str!(a, v, possible_value),
|
||||
"possible_values" => yaml_vec_or_str!(a, v, possible_value),
|
||||
"ignore_case" => yaml_to_bool!(a, v, ignore_case),
|
||||
"required_unless_present_any" => yaml_vec!(a, v, required_unless_present_any),
|
||||
"required_unless_present_all" => yaml_vec!(a, v, required_unless_present_all),
|
||||
"visible_alias" => yaml_to_str!(a, v, visible_alias),
|
||||
"visible_aliases" => yaml_vec_or_str!(a, v, visible_alias),
|
||||
"visible_short_alias" => yaml_to_char!(a, v, visible_short_alias),
|
||||
"visible_short_aliases" => yaml_to_chars!(a, v, visible_short_aliases),
|
||||
#[cfg(feature = "regex")]
|
||||
"validator_regex" => {
|
||||
if let Some(vec) = v.as_vec() {
|
||||
debug_assert_eq!(2, vec.len());
|
||||
let regex = yaml_str!(vec[0]);
|
||||
|
||||
match Regex::new(regex) {
|
||||
Err(e) => panic!(
|
||||
"Failed to convert \"{}\" into regular expression: {}",
|
||||
regex, e
|
||||
),
|
||||
Ok(regex) => a.validator_regex(regex, yaml_str!(vec[1])),
|
||||
}
|
||||
} else {
|
||||
panic!("Failed to convert YAML value to vector")
|
||||
}
|
||||
}
|
||||
"setting" | "settings" => {
|
||||
yaml_to_setting!(
|
||||
a,
|
||||
v,
|
||||
setting,
|
||||
ArgSettings,
|
||||
"ArgSetting",
|
||||
format!("arg '{}'", name_str)
|
||||
)
|
||||
}
|
||||
"case_insensitive" => yaml_to_bool!(a, v, ignore_case),
|
||||
"required_unless_one" => yaml_vec!(a, v, required_unless_present_any),
|
||||
"required_unless_all" => yaml_vec!(a, v, required_unless_present_all),
|
||||
s => {
|
||||
if !has_metadata {
|
||||
panic!(
|
||||
"Unknown setting '{}' in YAML file for arg '{}'",
|
||||
s, name_str
|
||||
)
|
||||
}
|
||||
continue;
|
||||
panic!(
|
||||
"Unknown setting '{}' in YAML file for arg '{}'",
|
||||
s, name_str
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -64,7 +64,7 @@ impl_settings! { ArgSettings, ArgFlags,
|
|||
RequireEquals("requireequals") => Flags::REQUIRE_EQUALS,
|
||||
Last("last") => Flags::LAST,
|
||||
IgnoreCase("ignorecase") => Flags::CASE_INSENSITIVE,
|
||||
CaseInsensitive("ignorecase") => Flags::CASE_INSENSITIVE,
|
||||
CaseInsensitive("caseinsensitive") => Flags::CASE_INSENSITIVE,
|
||||
#[cfg(feature = "env")]
|
||||
HideEnv("hideenv") => Flags::HIDE_ENV,
|
||||
#[cfg(feature = "env")]
|
||||
|
|
|
@ -17,25 +17,6 @@ macro_rules! yaml_tuple2 {
|
|||
}};
|
||||
}
|
||||
|
||||
#[cfg(feature = "yaml")]
|
||||
macro_rules! yaml_array_tuple2 {
|
||||
($a:ident, $v:ident, $c:ident) => {{
|
||||
if let Some(vec) = $v.as_vec() {
|
||||
for ys in vec {
|
||||
if let Some(tup) = ys.as_vec() {
|
||||
debug_assert_eq!(2, tup.len());
|
||||
$a = $a.$c(&[(yaml_str!(tup[0]), yaml_str!(tup[1]))]);
|
||||
} else {
|
||||
panic!("Failed to convert YAML value to vec");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
panic!("Failed to convert YAML value to vec");
|
||||
}
|
||||
$a
|
||||
}};
|
||||
}
|
||||
|
||||
#[cfg(feature = "yaml")]
|
||||
macro_rules! yaml_tuple3 {
|
||||
($a:ident, $v:ident, $c:ident) => {{
|
||||
|
@ -130,28 +111,6 @@ macro_rules! yaml_char {
|
|||
}};
|
||||
}
|
||||
|
||||
#[cfg(feature = "yaml")]
|
||||
macro_rules! yaml_chars {
|
||||
($v:expr) => {{
|
||||
&$v.as_vec()
|
||||
.unwrap_or_else(|| panic!("failed to convert YAML {:?} value to a list", $v))
|
||||
.into_iter()
|
||||
.map(|s| {
|
||||
s.as_str()
|
||||
.unwrap_or_else(|| panic!("failed to convert YAML {:?} value to a string", s))
|
||||
})
|
||||
.map(|s| {
|
||||
let mut chars = s.chars();
|
||||
let c = chars.next().expect("short aliases must be a single char");
|
||||
if chars.next().is_some() {
|
||||
panic!("short aliases must be a single char");
|
||||
}
|
||||
c
|
||||
})
|
||||
.collect::<Vec<char>>()
|
||||
}};
|
||||
}
|
||||
|
||||
#[cfg(feature = "yaml")]
|
||||
macro_rules! yaml_str {
|
||||
($v:expr) => {{
|
||||
|
@ -160,17 +119,6 @@ macro_rules! yaml_str {
|
|||
}};
|
||||
}
|
||||
|
||||
#[cfg(feature = "yaml")]
|
||||
macro_rules! yaml_str_parse {
|
||||
($a:ident, $v:ident, $c:ident) => {{
|
||||
$a.$c($v
|
||||
.as_str()
|
||||
.unwrap_or_else(|| panic!("failed to convert YAML {:?} value to a string", $v))
|
||||
.parse()
|
||||
.unwrap_or_else(|err| panic!("{}", err)))
|
||||
}};
|
||||
}
|
||||
|
||||
#[cfg(feature = "yaml")]
|
||||
macro_rules! yaml_to_char {
|
||||
($a:ident, $v:ident, $c:ident) => {{
|
||||
|
@ -178,13 +126,6 @@ macro_rules! yaml_to_char {
|
|||
}};
|
||||
}
|
||||
|
||||
#[cfg(feature = "yaml")]
|
||||
macro_rules! yaml_to_chars {
|
||||
($a:ident, $v:ident, $c:ident) => {{
|
||||
$a.$c(yaml_chars!($v))
|
||||
}};
|
||||
}
|
||||
|
||||
#[cfg(feature = "yaml")]
|
||||
macro_rules! yaml_to_str {
|
||||
($a:ident, $v:ident, $c:ident) => {{
|
||||
|
|
|
@ -4,31 +4,26 @@ about: tests clap library
|
|||
author: Kevin K. <kbknapp@gmail.com>
|
||||
settings:
|
||||
- ArgRequiredElseHelp
|
||||
help_message: prints help with a nonstandard description
|
||||
args:
|
||||
- help:
|
||||
short: h
|
||||
long: help
|
||||
help: prints help with a nonstandard description
|
||||
- option:
|
||||
- opt:
|
||||
short: o
|
||||
long: option
|
||||
multiple: true
|
||||
takes_value: true
|
||||
multiple_values: true
|
||||
help: tests options
|
||||
- positional:
|
||||
help: tests positionals
|
||||
index: 1
|
||||
- positional2:
|
||||
help: tests positionals with exclusions
|
||||
index: 2
|
||||
default_value_if:
|
||||
- [flag, null, some]
|
||||
- [positional, other, something]
|
||||
- [flag, Null, some]
|
||||
- [postional, other, something]
|
||||
- flag:
|
||||
short: f
|
||||
long: flag
|
||||
multiple: true
|
||||
takes_value: true
|
||||
multiple_values: true
|
||||
help: tests flags
|
||||
global: true
|
||||
- flag2:
|
||||
|
@ -41,13 +36,11 @@ args:
|
|||
- option2:
|
||||
long: long-option-2
|
||||
help: tests long options with exclusions
|
||||
conflicts_with:
|
||||
- option
|
||||
requires:
|
||||
- positional2
|
||||
- option3:
|
||||
short: O
|
||||
long: option3
|
||||
long: Option
|
||||
help: tests options with specific value sets
|
||||
takes_value: true
|
||||
possible_values:
|
||||
|
@ -56,7 +49,6 @@ args:
|
|||
requires_if:
|
||||
- [fast, flag]
|
||||
- positional3:
|
||||
index: 3
|
||||
help: tests positionals with specific values
|
||||
possible_values: [ vi, emacs ]
|
||||
- multvals:
|
||||
|
@ -67,83 +59,49 @@ args:
|
|||
- two
|
||||
- multvalsmo:
|
||||
long: multvalsmo
|
||||
multiple_values: true
|
||||
multiple: true
|
||||
takes_value: true
|
||||
help: Tests multiple values, not mult occs
|
||||
value_names: [one, two]
|
||||
- multvalsdelim:
|
||||
long: multvalsdelim
|
||||
help: Tests multiple values with required delimiter
|
||||
multiple: true
|
||||
takes_value: true
|
||||
multiple_occurrences: true
|
||||
use_delimiter: true
|
||||
require_delimiter: true
|
||||
- settings:
|
||||
short: s
|
||||
takes_value: true
|
||||
multiple_values: true
|
||||
value_delimiter: ","
|
||||
- singlealias:
|
||||
long: singlealias
|
||||
help: Tests single alias
|
||||
aliases: [alias]
|
||||
required_if_eq:
|
||||
required_if:
|
||||
- [multvalsmo, two]
|
||||
- multaliases:
|
||||
long: multaliases
|
||||
help: Tests multiple aliases
|
||||
aliases: [als1, als2, als3]
|
||||
- singleshortalias:
|
||||
long: singleshortalias
|
||||
help: Tests single short alias
|
||||
short_aliases: [a]
|
||||
required_if_eq:
|
||||
- [multvalsmo, two]
|
||||
- multshortaliases:
|
||||
long: multshortaliases
|
||||
help: Tests multiple short aliases
|
||||
short_aliases: [b, c]
|
||||
- minvals2:
|
||||
long: minvals2
|
||||
multiple_values: true
|
||||
multiple: true
|
||||
takes_value: true
|
||||
help: Tests 2 min vals
|
||||
min_values: 2
|
||||
- maxvals3:
|
||||
long: maxvals3
|
||||
multiple_values: true
|
||||
multiple: true
|
||||
takes_value: true
|
||||
help: Tests 3 max vals
|
||||
max_values: 3
|
||||
- exclusive:
|
||||
long: exclusive
|
||||
help: Tests 3 exclusive
|
||||
exclusive: true
|
||||
- ignore_case:
|
||||
index: 4
|
||||
help: Test ignore case
|
||||
- case_insensitive:
|
||||
help: Test case_insensitive
|
||||
possible_values: [test123, test321]
|
||||
ignore_case: true
|
||||
- value_hint:
|
||||
long: value-hint
|
||||
help: Test value_hint
|
||||
value_hint: FilePath
|
||||
- verbose:
|
||||
short: v
|
||||
multiple_occurrences: true
|
||||
takes_value: false
|
||||
help: Sets the level of verbosity
|
||||
- visiblealiases:
|
||||
long: visiblealiases
|
||||
help: Tests visible aliases
|
||||
visible_alias: visals1
|
||||
visible_aliases: [visals2, visals2, visals3]
|
||||
- visibleshortaliases:
|
||||
long: visibleshortaliases
|
||||
help: Tests visible short aliases
|
||||
visible_short_alias: e
|
||||
visible_short_aliases: [l, m]
|
||||
groups:
|
||||
case_insensitive: true
|
||||
|
||||
arg_groups:
|
||||
- test:
|
||||
args:
|
||||
- maxvals3
|
||||
- minvals2
|
||||
- minmals2
|
||||
conflicts_with:
|
||||
- option3
|
||||
requires:
|
||||
|
@ -157,9 +115,8 @@ subcommands:
|
|||
- scoption:
|
||||
short: o
|
||||
long: option
|
||||
multiple_values: true
|
||||
multiple: true
|
||||
help: tests options
|
||||
takes_value: true
|
||||
- scpositional:
|
||||
help: tests positionals
|
||||
index: 1
|
11
tests/fixtures/app_2space.yaml
vendored
11
tests/fixtures/app_2space.yaml
vendored
|
@ -1,11 +0,0 @@
|
|||
name: claptests
|
||||
version: "1.0"
|
||||
about: tests clap library
|
||||
author: Kevin K. <kbknapp@gmail.com>
|
||||
args:
|
||||
- opt:
|
||||
short: o
|
||||
long: option
|
||||
takes_value: true
|
||||
multiple_values: true
|
||||
help: tests options
|
2
tests/fixtures/app_field_not_string.yaml
vendored
2
tests/fixtures/app_field_not_string.yaml
vendored
|
@ -1,2 +0,0 @@
|
|||
name: claptests
|
||||
1: "invalid"
|
9
tests/fixtures/app_regex.yaml
vendored
9
tests/fixtures/app_regex.yaml
vendored
|
@ -1,9 +0,0 @@
|
|||
name: clapregextest
|
||||
version: "1.0"
|
||||
about: tests clap regex functionality
|
||||
author: Benjamin Kästner <benjamin.kaestner@gmail.com>
|
||||
args:
|
||||
- filter:
|
||||
index: 1
|
||||
validator_regex: ["^*\\.[a-z]+$", expected extension pattern]
|
||||
help: file extension pattern
|
8
tests/fixtures/app_regex_invalid.yaml
vendored
8
tests/fixtures/app_regex_invalid.yaml
vendored
|
@ -1,8 +0,0 @@
|
|||
name: clapregextest
|
||||
version: "1.0"
|
||||
about: tests clap regex functionality
|
||||
author: Benjamin Kästner <benjamin.kaestner@gmail.com>
|
||||
args:
|
||||
- filter:
|
||||
index: 1
|
||||
validator_regex: [")", invalid regular expression]
|
5
tests/fixtures/app_setting_invalid.yaml
vendored
5
tests/fixtures/app_setting_invalid.yaml
vendored
|
@ -1,5 +0,0 @@
|
|||
name: claptests
|
||||
version: "1.0"
|
||||
about: tests clap extra fields
|
||||
settings:
|
||||
- random
|
2
tests/fixtures/arg_field_not_string.yaml
vendored
2
tests/fixtures/arg_field_not_string.yaml
vendored
|
@ -1,2 +0,0 @@
|
|||
claptest-arg:
|
||||
6: false
|
4
tests/fixtures/arg_not_hash.yaml
vendored
4
tests/fixtures/arg_not_hash.yaml
vendored
|
@ -1,4 +0,0 @@
|
|||
name: claptests
|
||||
args:
|
||||
- arg:
|
||||
short: a
|
8
tests/fixtures/arg_setting_invalid.yaml
vendored
8
tests/fixtures/arg_setting_invalid.yaml
vendored
|
@ -1,8 +0,0 @@
|
|||
name: claptests
|
||||
version: "1.0"
|
||||
about: tests clap extra fields
|
||||
args:
|
||||
- option:
|
||||
long: option
|
||||
settings:
|
||||
- random
|
10
tests/fixtures/extra_fields.yaml
vendored
10
tests/fixtures/extra_fields.yaml
vendored
|
@ -1,10 +0,0 @@
|
|||
_has_metadata: true
|
||||
name: claptests
|
||||
version: "1.0"
|
||||
about: tests clap extra fields
|
||||
random: This field is extra
|
||||
args:
|
||||
- option:
|
||||
_has_metadata: true
|
||||
long: option
|
||||
random: This field is extra
|
6
tests/fixtures/extra_fields_invalid_app.yaml
vendored
6
tests/fixtures/extra_fields_invalid_app.yaml
vendored
|
@ -1,6 +0,0 @@
|
|||
name: claptests
|
||||
version: "1.0"
|
||||
about: tests clap extra fields
|
||||
subcommands:
|
||||
- info:
|
||||
random: This field is extra
|
7
tests/fixtures/extra_fields_invalid_arg.yaml
vendored
7
tests/fixtures/extra_fields_invalid_arg.yaml
vendored
|
@ -1,7 +0,0 @@
|
|||
name: claptests
|
||||
version: "1.0"
|
||||
about: tests clap extra fields
|
||||
args:
|
||||
- option:
|
||||
long: option
|
||||
random: This field is extra
|
3
tests/fixtures/field_not_hash.yaml
vendored
3
tests/fixtures/field_not_hash.yaml
vendored
|
@ -1,3 +0,0 @@
|
|||
claptests:
|
||||
- 5
|
||||
- 6
|
21
tests/fixtures/multiple_groups.yaml
vendored
21
tests/fixtures/multiple_groups.yaml
vendored
|
@ -1,21 +0,0 @@
|
|||
name: app
|
||||
|
||||
args:
|
||||
- arg_1:
|
||||
short: a
|
||||
- arg_2:
|
||||
short: b
|
||||
- arg_3:
|
||||
short: c
|
||||
- arg_4:
|
||||
short: d
|
||||
|
||||
groups:
|
||||
- group_1:
|
||||
args:
|
||||
- arg_1
|
||||
- arg_2
|
||||
- group_2:
|
||||
args:
|
||||
- arg_3
|
||||
- arg_4
|
1
tests/fixtures/name_not_string.yaml
vendored
1
tests/fixtures/name_not_string.yaml
vendored
|
@ -1 +0,0 @@
|
|||
100: "claptests"
|
1
tests/fixtures/not_hash.yaml
vendored
1
tests/fixtures/not_hash.yaml
vendored
|
@ -1 +0,0 @@
|
|||
["clap", "tests"]
|
254
tests/yaml.rs
254
tests/yaml.rs
|
@ -1,268 +1,38 @@
|
|||
#![cfg(feature = "yaml")]
|
||||
#![allow(deprecated)]
|
||||
|
||||
use clap::{load_yaml, App, Arg, ErrorKind, ValueHint};
|
||||
use clap::{load_yaml, App};
|
||||
|
||||
#[test]
|
||||
fn create_app_from_yaml() {
|
||||
let yaml = load_yaml!("fixtures/app.yaml");
|
||||
let _ = App::from_yaml(yaml);
|
||||
let yml = load_yaml!("app.yml");
|
||||
App::from_yaml(yml);
|
||||
}
|
||||
|
||||
// TODO: Uncomment to test yaml with 2 spaces https://github.com/chyh1990/yaml-rust/issues/101
|
||||
// #[test]
|
||||
// fn create_app_from_yaml_2spaces() {
|
||||
// let yaml = load_yaml!("fixtures/app_2space.yaml");
|
||||
// App::from_yaml(yaml);
|
||||
// }
|
||||
|
||||
#[test]
|
||||
fn help_message() {
|
||||
let yaml = load_yaml!("fixtures/app.yaml");
|
||||
let mut app = App::from_yaml(yaml);
|
||||
let yml = load_yaml!("app.yml");
|
||||
let mut app = App::from_yaml(yml);
|
||||
// Generate the full help message!
|
||||
let _ = app.try_get_matches_from_mut(Vec::<String>::new());
|
||||
let _ = app.get_matches_from_safe_borrow(Vec::<String>::new());
|
||||
|
||||
let mut help_buffer = Vec::new();
|
||||
app.write_help(&mut help_buffer).unwrap();
|
||||
let help_string = String::from_utf8(help_buffer).unwrap();
|
||||
println!("{}", help_string);
|
||||
assert!(help_string
|
||||
.contains("-h, --help\n prints help with a nonstandard description\n"));
|
||||
println!("{}", &help_string);
|
||||
assert!(help_string.contains("tests positionals with exclusions\n"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn author() {
|
||||
let yaml = load_yaml!("fixtures/app.yaml");
|
||||
let mut app = App::from_yaml(yaml);
|
||||
let yml = load_yaml!("app.yml");
|
||||
let mut app = App::from_yaml(yml);
|
||||
// Generate the full help message!
|
||||
let _ = app.try_get_matches_from_mut(Vec::<String>::new());
|
||||
let _ = app.get_matches_from_safe_borrow(Vec::<String>::new());
|
||||
|
||||
let mut help_buffer = Vec::new();
|
||||
app.write_help(&mut help_buffer).unwrap();
|
||||
let help_string = String::from_utf8(help_buffer).unwrap();
|
||||
println!("{}", &help_string);
|
||||
assert!(help_string.contains("Kevin K. <kbknapp@gmail.com>"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn app_settings() {
|
||||
let yaml = load_yaml!("fixtures/app.yaml");
|
||||
let app = App::from_yaml(yaml);
|
||||
|
||||
let m = app.try_get_matches_from(vec!["prog"]);
|
||||
|
||||
assert!(m.is_err());
|
||||
assert_eq!(
|
||||
m.unwrap_err().kind,
|
||||
ErrorKind::DisplayHelpOnMissingArgumentOrSubcommand
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic = "Unknown AppSetting 'random' found in YAML file for app"]
|
||||
fn app_setting_invalid() {
|
||||
let yaml = load_yaml!("fixtures/app_setting_invalid.yaml");
|
||||
let _ = App::from_yaml(yaml);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic = "Unknown ArgSetting 'random' found in YAML file for arg 'option'"]
|
||||
fn arg_setting_invalid() {
|
||||
let yaml = load_yaml!("fixtures/arg_setting_invalid.yaml");
|
||||
let _ = App::from_yaml(yaml);
|
||||
}
|
||||
|
||||
// ValueHint must be parsed correctly from Yaml
|
||||
#[test]
|
||||
fn value_hint() {
|
||||
let yml = load_yaml!("fixtures/app.yaml");
|
||||
let app = App::from_yaml(yml);
|
||||
|
||||
let arg = app
|
||||
.get_arguments()
|
||||
.find(|a| a.get_name() == "value_hint")
|
||||
.unwrap();
|
||||
assert_eq!(arg.get_value_hint(), ValueHint::FilePath);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn default_value_if_not_triggered_by_argument() {
|
||||
let yml = load_yaml!("fixtures/app.yaml");
|
||||
let app = App::from_yaml(yml);
|
||||
|
||||
// Fixtures use "other" as value
|
||||
let matches = app.try_get_matches_from(vec!["prog", "wrong"]).unwrap();
|
||||
|
||||
assert!(matches.value_of("positional2").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn default_value_if_triggered_by_matching_argument() {
|
||||
let yml = load_yaml!("fixtures/app.yaml");
|
||||
let app = App::from_yaml(yml);
|
||||
|
||||
let matches = app.try_get_matches_from(vec!["prog", "other"]).unwrap();
|
||||
assert_eq!(matches.value_of("positional2").unwrap(), "something");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn default_value_if_triggered_by_flag() {
|
||||
let yml = load_yaml!("fixtures/app.yaml");
|
||||
let app = App::from_yaml(yml);
|
||||
|
||||
let matches = app
|
||||
.try_get_matches_from(vec!["prog", "--flag", "flagvalue"])
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(matches.value_of("positional2").unwrap(), "some");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn default_value_if_triggered_by_flag_and_argument() {
|
||||
let yml = load_yaml!("fixtures/app.yaml");
|
||||
let app = App::from_yaml(yml);
|
||||
|
||||
let matches = app
|
||||
.try_get_matches_from(vec!["prog", "--flag", "flagvalue", "other"])
|
||||
.unwrap();
|
||||
|
||||
// First condition triggers, therefore "some"
|
||||
assert_eq!(matches.value_of("positional2").unwrap(), "some");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn yaml_multiple_occurrences() {
|
||||
let yaml = load_yaml!("fixtures/app.yaml");
|
||||
let matches = App::from_yaml(yaml)
|
||||
.try_get_matches_from(vec!["prog", "-vvv"])
|
||||
.unwrap();
|
||||
assert_eq!(matches.occurrences_of("verbose"), 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn yaml_multiple_values() {
|
||||
let yaml = load_yaml!("fixtures/app.yaml");
|
||||
let matches = App::from_yaml(yaml)
|
||||
.try_get_matches_from(vec!["prog", "-s", "aaa", "bbb"])
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
matches
|
||||
.values_of("settings")
|
||||
.unwrap()
|
||||
.collect::<Vec<&str>>(),
|
||||
vec!["aaa", "bbb"]
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg(feature = "regex")]
|
||||
#[test]
|
||||
fn regex_with_invalid_string() {
|
||||
let yml = load_yaml!("fixtures/app_regex.yaml");
|
||||
let app = App::from_yaml(yml);
|
||||
let res = app.try_get_matches_from(vec!["prog", "not a proper filter"]);
|
||||
|
||||
assert!(res.is_err());
|
||||
}
|
||||
|
||||
#[cfg(feature = "regex")]
|
||||
#[test]
|
||||
fn regex_with_valid_string() {
|
||||
let yml = load_yaml!("fixtures/app_regex.yaml");
|
||||
let app = App::from_yaml(yml);
|
||||
|
||||
let matches = app.try_get_matches_from(vec!["prog", "*.txt"]).unwrap();
|
||||
|
||||
assert_eq!(matches.value_of("filter").unwrap(), "*.txt");
|
||||
}
|
||||
|
||||
#[cfg(feature = "regex")]
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn regex_with_invalid_yaml() {
|
||||
let yml = load_yaml!("fixtures/app_regex_invalid.yaml");
|
||||
let _ = App::from_yaml(yml);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn extra_fields() {
|
||||
let yml = load_yaml!("fixtures/extra_fields.yaml");
|
||||
let _ = App::from_yaml(yml);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic = "Unknown setting 'random' in YAML file for arg 'option'"]
|
||||
fn extra_fields_invalid_arg() {
|
||||
let yml = load_yaml!("fixtures/extra_fields_invalid_arg.yaml");
|
||||
let _ = App::from_yaml(yml);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic = "Unknown setting 'random' in YAML file for subcommand 'info'"]
|
||||
fn extra_fields_invalid_app() {
|
||||
let yml = load_yaml!("fixtures/extra_fields_invalid_app.yaml");
|
||||
let _ = App::from_yaml(yml);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic = "YAML file must be a hash"]
|
||||
fn app_not_hash() {
|
||||
let yml = load_yaml!("fixtures/not_hash.yaml");
|
||||
let _ = App::from_yaml(yml);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic = "YAML file must be a hash"]
|
||||
fn arg_file_not_hash() {
|
||||
let yml = load_yaml!("fixtures/not_hash.yaml");
|
||||
let _ = Arg::from_yaml(yml);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic = "Subcommand must be a hash"]
|
||||
fn subcommand_not_hash() {
|
||||
let yml = load_yaml!("fixtures/field_not_hash.yaml");
|
||||
let _ = App::from_yaml(yml);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic = "Arg must be a hash"]
|
||||
fn arg_not_hash() {
|
||||
let yml = load_yaml!("fixtures/arg_not_hash.yaml");
|
||||
let _ = App::from_yaml(yml);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic = "Subcommand name must be a string"]
|
||||
fn subcommand_name_not_string() {
|
||||
let yml = load_yaml!("fixtures/name_not_string.yaml");
|
||||
let _ = App::from_yaml(yml);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic = "Arg name must be a string"]
|
||||
fn arg_name_not_string() {
|
||||
let yml = load_yaml!("fixtures/name_not_string.yaml");
|
||||
let _ = Arg::from_yaml(yml);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic = "App fields must be strings"]
|
||||
fn app_field_not_string() {
|
||||
let yml = load_yaml!("fixtures/app_field_not_string.yaml");
|
||||
let _ = App::from_yaml(yml);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic = "Arg fields must be strings"]
|
||||
fn arg_field_not_string() {
|
||||
let yml = load_yaml!("fixtures/arg_field_not_string.yaml");
|
||||
let _ = Arg::from_yaml(yml);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn multiple_groups() {
|
||||
let yml = load_yaml!("fixtures/multiple_groups.yaml");
|
||||
let matches = App::from_yaml(yml).try_get_matches_from(&["app", "-a", "-c"]);
|
||||
eprintln!("{:?}", matches);
|
||||
assert!(matches.is_ok());
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue