Skip to content

Commit

Permalink
feat(positionals): add assertions for positional args with multiple vals
Browse files Browse the repository at this point in the history
  • Loading branch information
kbknapp committed Apr 1, 2015
1 parent 8078400 commit b7fa72d
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{
if a.short.is_some() || a.long.is_some() {
panic!("Argument \"{}\" has conflicting requirements, both index() and short(), or long(), were supplied", a.name);
}
if self.positionals_idx.contains_key(&i) {
panic!("Argument \"{}\" has the same index as another positional argument", a.name);
}
// if a.multiple {
// panic!("Argument \"{}\" has conflicting requirements, both index() and multiple(true) were supplied",a.name);
// }
Expand Down Expand Up @@ -555,6 +558,11 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{
}

pub fn get_matches(mut self) -> ArgMatches<'ar> {
self.verify_positionals();
for sc in self.subcommands.values() {
sc.verify_positionals();
}

let mut matches = ArgMatches::new();

let args = env::args().collect::<Vec<_>>();
Expand All @@ -572,6 +580,29 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{
matches
}

fn verify_positionals(&self) {
// Because you must wait until all arguments have been supplied, this is the first chance
// to make assertions on positional argument indexes
//
// Firt we verify that the index highest supplied index, is equal to the number of
// positional arguments to verify there are no gaps (i.e. supplying an index of 1 and 3
// but no 2)
//
// Next we verify that only the highest index has a .multiple(true) (if any)
if let Some((idx, ref p)) = self.positionals_idx.iter().rev().next() {
if *idx as usize != self.positionals_idx.len() {
panic!("Found positional argument \"{}\" who's index is {} but there are only {} positional arguments defined", p.name, idx, self.positionals_idx.len());
}
}
if let Some(ref p) = self.positionals_idx.values()
.filter(|ref a| a.multiple)
.filter(|ref a| a.index as usize != self.positionals_idx.len())
.next() {
panic!("Found positional argument \"{}\" which accepts multiple values but it's not the last positional argument (i.e. others have a higher index)",
p.name);
}
}

fn get_matches_from(&mut self, matches: &mut ArgMatches<'ar>, it: &mut IntoIter<String>) {
self.create_help_and_version();

Expand Down

0 comments on commit b7fa72d

Please sign in to comment.