Skip to content

Commit

Permalink
fix(args): determine if the only arguments allowed are also required
Browse files Browse the repository at this point in the history
  • Loading branch information
kbknapp committed Apr 1, 2015
1 parent 62ec95a commit 0a09eb3
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/app.rs
Expand Up @@ -410,9 +410,21 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{
let pos = !self.positionals_idx.is_empty();
let opts = !self.opts.is_empty();
let subcmds = !self.subcommands.is_empty();
let req_pos = self.positionals_idx.values().filter_map(|ref x| if x.required || self.required.contains(x.name) { Some(x.name) } else {None})
let mut num_req_pos = 0;
let req_pos = self.positionals_idx.values().filter_map(|ref x| if x.required || self.required.contains(x.name) {
num_req_pos += 1;
Some(x.name)
} else {
None
})
.fold(String::new(), |acc, ref name| acc + &format!("<{}> ", name)[..]);
let req_opts = self.opts.values().filter(|ref x| x.required || self.required.contains(x.name))
let mut num_req_opts = 0;
let req_opts = self.opts.values().filter_map(|x| if x.required || self.required.contains(x.name) {
num_req_opts += 1;
Some(x)
}else {
None
})
.fold(String::new(), |acc, ref o| acc + &format!("-{}{} ",if let Some(s) = o.short {
format!("{} ", s)
} else {
Expand All @@ -422,7 +434,7 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{
print!("\t{} {} {} {} {}", if let Some(ref name) = self.bin_name { name.replace("-", " ") } else { self.name.clone() },
if flags {"[FLAGS]"} else {""},
if opts {
if req_opts.len() != self.opts.len() && !req_opts.is_empty() {
if num_req_opts != self.opts.len() && !req_opts.is_empty() {
format!("[OPTIONS] {}", &req_opts[..])
} else if req_opts.is_empty() {
"[OPTIONS]".to_owned()
Expand All @@ -431,7 +443,7 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{
}
} else { "".to_owned() },
if pos {
if req_pos.len() != self.positionals_idx.len() && !req_pos.is_empty() {
if num_req_pos != self.positionals_idx.len() && !req_pos.is_empty() {
format!("[POSITIONAL] {}", &req_pos[..])
} else if req_pos.is_empty() {
"[POSITIONAL]".to_owned()
Expand Down

0 comments on commit 0a09eb3

Please sign in to comment.