Skip to content

Commit

Permalink
fix(usage): display required args in usage, even if only required by …
Browse files Browse the repository at this point in the history
…others
  • Loading branch information
kbknapp committed Apr 1, 2015
1 parent 20f3e77 commit 1b7316d
Showing 1 changed file with 31 additions and 19 deletions.
50 changes: 31 additions & 19 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,27 +345,39 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{
if let Some(u) = self.usage_str {
println!("\t{}",u);
} else {
let flags = ! self.flags.is_empty();
let pos = ! self.positionals_idx.is_empty();
let req_pos = self.positionals_idx.values().filter_map(|ref x| if x.required { Some(x.name) } else {None})
let flags = !self.flags.is_empty();
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})
.fold(String::new(), |acc, ref name| acc + &format!("<{}> ", name)[..]);
let req_opts = self.opts.values().filter(|ref x| x.required)
.fold(String::new(), |acc, ref o| acc + &format!("{}{} ",if let Some(s) = o.short {
format!("-{} ", s)
let req_opts = self.opts.values().filter(|ref x| x.required || self.required.contains(x.name))
.fold(String::new(), |acc, ref o| acc + &format!("-{}{} ",if let Some(s) = o.short {
format!("{} ", s)
} else {
format!("--{}=",o.long.unwrap())
format!("-{}=",o.long.unwrap())
},o.name));
let opts = ! self.opts.is_empty();
let subcmds = ! self.subcommands.is_empty();

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.is_empty() { "[OPTIONS]" } else { &req_opts[..] }
} else { "" },
if req_opts.len() != self.opts.len() && !req_opts.is_empty() {
format!("[OPTIONS] {}", &req_opts[..])
} else if req_opts.is_empty() {
"[OPTIONS]".to_owned()
} else {
req_opts
}
} else { "".to_owned() },
if pos {
if req_pos.is_empty() { "[POSITIONAL]"} else { &req_pos[..] }
} else {""},
if req_pos.len() != self.positionals_idx.len() && !req_pos.is_empty() {
format!("[POSITIONAL] {}", &req_pos[..])
} else if req_pos.is_empty() {
"[POSITIONAL]".to_owned()
} else {
req_pos
}
} else {"".to_owned()},
if subcmds {"[SUBCOMMANDS]"} else {""});
}

Expand Down Expand Up @@ -397,8 +409,8 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{
println!("FLAGS:");
for v in self.flags.values() {
println!("\t{}{}\t{}",
if let Some(s) = v.short{format!("-{}",s)}else{format!(" ")},
if let Some(l) = v.long {format!(",--{}",l)}else {format!(" \t")},
if let Some(s) = v.short{format!("-{}",s)}else{" ".to_owned()},
if let Some(l) = v.long {format!(",--{}",l)}else {" \t".to_owned()},
if let Some(h) = v.help {h} else {" "} );
}
}
Expand All @@ -408,10 +420,10 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{
for v in self.opts.values() {
let mut needs_tab = false;
println!("\t{}{}{}\t{}",
if let Some(s) = v.short{format!("-{} ",s)}else{format!(" ")},
if let Some(l) = v.long {format!(",--{}=",l)}else {needs_tab = true; format!(" ")},
if let Some(s) = v.short{format!("-{} ",s)}else{" ".to_owned()},
if let Some(l) = v.long {format!(",--{}=",l)}else {needs_tab = true; " ".to_owned()},
format!("{}", v.name),
if let Some(h) = v.help {if needs_tab {format!("\t{}", h)} else { format!("{}", h) } } else {format!(" ")} );
if let Some(h) = v.help {if needs_tab {format!("\t{}", h)} else { format!("{}", h) } } else {" ".to_owned()} );
}
}
if pos {
Expand Down Expand Up @@ -572,7 +584,7 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{
if let Some(sc_name) = subcmd_name {
if let Some(ref mut sc) = self.subcommands.get_mut(&sc_name) {
let mut new_matches = ArgMatches::new();
sc.bin_name = Some(format!("{}{}{}", self.bin_name.clone().unwrap_or(format!("")),if self.bin_name.is_some() {"-"} else {""}, sc.name.clone()));
sc.bin_name = Some(format!("{}{}{}", self.bin_name.clone().unwrap_or("".to_owned()),if self.bin_name.is_some() {"-"} else {""}, sc.name.clone()));
sc.get_matches_from(&mut new_matches, it);
matches.subcommand = Some(Box::new(SubCommand{
name: sc.name.clone(),
Expand Down

0 comments on commit 1b7316d

Please sign in to comment.