Skip to content

Commit

Permalink
Auto merge of #263 - Vinatorul:dev, r=kbknapp
Browse files Browse the repository at this point in the history
fix(ArgGroup) added asserts to help users to configure clap properly

Also I've added tests for this change

Now you will get 
```
ArgGroup %Your_ArgGroup_Name% doesn't contain any args
```
instead of
```
thread '<main>' panicked at 'arithmetic operation overflowed'
```
And 
```
ArgGroup %Your_ArgGroup_Name% can not have same name as arg inside it
```
instead of 
```
thread '<main>' has overflowed its stack
```
  • Loading branch information
homu committed Sep 21, 2015
2 parents b8e47f1 + 4ed03cd commit 3d0199d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/app/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.iter().map(ToOwned::to_owned).collect()
}
Expand Down Expand Up @@ -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.iter().map(|s| *s).collect()
}
Expand Down
1 change: 1 addition & 0 deletions src/args/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ impl<'n, 'ar> ArgGroup<'n, 'ar> {
pub fn add(mut self,
n: &'ar str)
-> Self {
assert!(self.name != n, "ArgGroup '{}' can not have same name as arg inside it", self.name);
self.args.push(n);
self
}
Expand Down
34 changes: 33 additions & 1 deletion src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashSet;

use super::{App, Arg, SubCommand};
use super::{App, Arg, ArgGroup, SubCommand};
use std::vec::Vec;

arg_enum!{
Expand Down Expand Up @@ -909,3 +909,35 @@ fn create_multiple_subcommands() {
.arg(Arg::with_name("other").long("other"))
.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();
}

0 comments on commit 3d0199d

Please sign in to comment.