mirror of
https://github.com/clap-rs/clap
synced 2025-01-20 16:43:54 +00:00
fix(ArgGroup) added asserts to help users to configure clap properly
Also I've added tests for this change
This commit is contained in:
parent
5b7a4a03bb
commit
a87c5cd660
3 changed files with 36 additions and 1 deletions
|
@ -1330,6 +1330,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
assert!(args.len() > 0, "ArgGroup '{}' doesn't contain any args", group);
|
||||||
args.dedup();
|
args.dedup();
|
||||||
args.iter().map(ToOwned::to_owned).collect()
|
args.iter().map(ToOwned::to_owned).collect()
|
||||||
}
|
}
|
||||||
|
@ -1362,6 +1363,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
assert!(args.len() > 0, "ArgGroup '{}' doesn't contain any args", group);
|
||||||
args.dedup();
|
args.dedup();
|
||||||
args.iter().map(|s| *s).collect()
|
args.iter().map(|s| *s).collect()
|
||||||
}
|
}
|
||||||
|
|
|
@ -143,6 +143,7 @@ impl<'n, 'ar> ArgGroup<'n, 'ar> {
|
||||||
pub fn add(mut self,
|
pub fn add(mut self,
|
||||||
n: &'ar str)
|
n: &'ar str)
|
||||||
-> Self {
|
-> Self {
|
||||||
|
assert!(self.name != n, "ArgGroup '{}' can not have same name as arg inside it", self.name);
|
||||||
self.args.push(n);
|
self.args.push(n);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
34
src/tests.rs
34
src/tests.rs
|
@ -1,6 +1,6 @@
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
|
|
||||||
use super::{App, Arg, SubCommand};
|
use super::{App, Arg, ArgGroup, SubCommand};
|
||||||
use std::vec::Vec;
|
use std::vec::Vec;
|
||||||
|
|
||||||
arg_enum!{
|
arg_enum!{
|
||||||
|
@ -909,3 +909,35 @@ fn create_multiple_subcommands() {
|
||||||
.arg(Arg::with_name("other").long("other"))
|
.arg(Arg::with_name("other").long("other"))
|
||||||
.get_matches();
|
.get_matches();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[should_panic]
|
||||||
|
fn empty_group() {
|
||||||
|
let _ = App::new("empty_group")
|
||||||
|
.arg(Arg::from_usage("-f, --flag 'some flag'"))
|
||||||
|
.arg_group(ArgGroup::with_name("vers")
|
||||||
|
.required(true))
|
||||||
|
.get_matches();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[should_panic]
|
||||||
|
fn empty_group_2() {
|
||||||
|
let _ = App::new("empty_group")
|
||||||
|
.arg(Arg::from_usage("-f, --flag 'some flag'"))
|
||||||
|
.arg_group(ArgGroup::with_name("vers")
|
||||||
|
.required(true)
|
||||||
|
.add_all(&["ver", "major"]))
|
||||||
|
.get_matches();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[should_panic]
|
||||||
|
fn errous_group() {
|
||||||
|
let _ = App::new("errous_group")
|
||||||
|
.arg(Arg::from_usage("-f, --flag 'some flag'"))
|
||||||
|
.arg_group(ArgGroup::with_name("vers")
|
||||||
|
.add("vers")
|
||||||
|
.required(true))
|
||||||
|
.get_matches();
|
||||||
|
}
|
Loading…
Reference in a new issue