Skip to content

Commit da549dc

Browse files
committed
fix(apps): allow use of hyphens in application and subcommand names
1 parent c214f72 commit da549dc

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/app.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{
431431
format!("-{}=",o.long.unwrap())
432432
},o.name));
433433

434-
print!("\t{} {} {} {} {}", if let Some(ref name) = self.bin_name { name.replace("-", " ") } else { self.name.clone() },
434+
print!("\t{} {} {} {} {}", self.bin_name.clone().unwrap_or(self.name.clone()),
435435
if flags {"[FLAGS]"} else {""},
436436
if opts {
437437
if num_req_opts != self.opts.len() && !req_opts.is_empty() {
@@ -537,7 +537,9 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{
537537
}
538538

539539
fn print_version(&self, quit: bool) {
540-
println!("{} {}", self.bin_name.clone().unwrap_or(self.name.clone()), self.version.unwrap_or("") );
540+
// Print the binary name if existing, but replace all spaces with hyphens in case we're
541+
// dealing with subcommands i.e. git mv is translated to git-mv
542+
println!("{} {}", &self.bin_name.clone().unwrap_or(self.name.clone())[..].replace(" ", "-"), self.version.unwrap_or("") );
541543
if quit { self.exit(); }
542544
}
543545

@@ -698,7 +700,8 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{
698700
if let Some(sc_name) = subcmd_name {
699701
if let Some(ref mut sc) = self.subcommands.get_mut(&sc_name) {
700702
let mut new_matches = ArgMatches::new();
701-
sc.bin_name = Some(format!("{}{}{}", self.bin_name.clone().unwrap_or("".to_owned()),if self.bin_name.is_some() {"-"} else {""}, sc.name.clone()));
703+
// bin_name should be parent's bin_name + the sc's name seperated by a space
704+
sc.bin_name = Some(format!("{}{}{}", self.bin_name.clone().unwrap_or("".to_owned()),if self.bin_name.is_some() {" "} else {""}, sc.name.clone()));
702705
sc.get_matches_from(&mut new_matches, it);
703706
matches.subcommand = Some(Box::new(SubCommand{
704707
name: sc.name.clone(),

0 commit comments

Comments
 (0)