mirror of
https://github.com/clap-rs/clap
synced 2025-03-04 23:37:32 +00:00
feat(ArgGroups): adds support for building ArgGroups from yaml
This commit is contained in:
parent
e415cf78ba
commit
ecf88665cb
2 changed files with 61 additions and 0 deletions
|
@ -218,6 +218,11 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
|
|||
a = a.subcommand(SubCommand::from_yaml(&sc_yaml));
|
||||
}
|
||||
}
|
||||
if let Some(v) = yaml["arg_groups"].as_vec() {
|
||||
for ag_yaml in v {
|
||||
a = a.arg_group(ArgGroup::from_yaml(&ag_yaml.as_hash().unwrap()));
|
||||
}
|
||||
}
|
||||
|
||||
a
|
||||
}
|
||||
|
|
|
@ -1,6 +1,11 @@
|
|||
#[cfg(feature = "yaml")]
|
||||
use std::collections::BTreeMap;
|
||||
use std::collections::HashSet;
|
||||
use std::fmt::{Debug, Formatter, Result};
|
||||
|
||||
#[cfg(feature = "yaml")]
|
||||
use yaml_rust::Yaml;
|
||||
|
||||
/// `ArgGroup`s are a family of related arguments and way for you to say, "Any of these arguments".
|
||||
/// By placing arguments in a logical group, you can make easier requirement and exclusion rules
|
||||
/// intead of having to list each individually, or when you want a rule to apply "any but not all"
|
||||
|
@ -72,6 +77,57 @@ impl<'n, 'ar> ArgGroup<'n, 'ar> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Creates a new instace of `ArgGroup` from a .yml (YAML) file.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```ignore
|
||||
/// # use clap::ArgGroup;
|
||||
/// let yml = load_yaml!("group.yml");
|
||||
/// let ag = ArgGroup::from_yaml(yml);
|
||||
/// ```
|
||||
#[cfg(feature = "yaml")]
|
||||
pub fn from_yaml<'y>(y: &'y BTreeMap<Yaml, Yaml>) -> ArgGroup<'y, 'y> {
|
||||
// We WANT this to panic on error...so expect() is good.
|
||||
let name_yml = y.keys().nth(0).unwrap();
|
||||
let name_str = name_yml.as_str().unwrap();
|
||||
let mut a = ArgGroup::with_name(name_str);
|
||||
let group_settings = y.get(name_yml).unwrap().as_hash().unwrap();
|
||||
|
||||
for (k, v) in group_settings.iter() {
|
||||
a = match k.as_str().unwrap() {
|
||||
"required" => a.required(v.as_bool().unwrap()),
|
||||
"args" => {
|
||||
for ys in v.as_vec().unwrap() {
|
||||
if let Some(s) = ys.as_str() {
|
||||
a = a.add(s);
|
||||
}
|
||||
}
|
||||
a
|
||||
},
|
||||
"requires" => {
|
||||
for ys in v.as_vec().unwrap() {
|
||||
if let Some(s) = ys.as_str() {
|
||||
a = a.requires(s);
|
||||
}
|
||||
}
|
||||
a
|
||||
},
|
||||
"conflicts_with" => {
|
||||
for ys in v.as_vec().unwrap() {
|
||||
if let Some(s) = ys.as_str() {
|
||||
a = a.conflicts_with(s);
|
||||
}
|
||||
}
|
||||
a
|
||||
},
|
||||
s => panic!("Unknown ArgGroup setting '{}' in YAML file for ArgGroup '{}'", s, name_str)
|
||||
}
|
||||
}
|
||||
|
||||
a
|
||||
}
|
||||
|
||||
/// Adds an argument to this group by name
|
||||
///
|
||||
///
|
||||
|
|
Loading…
Add table
Reference in a new issue