Skip to content

Commit

Permalink
fix(flags): fix bug not allowing users to specify -v or -h
Browse files Browse the repository at this point in the history
  • Loading branch information
kbknapp committed Apr 4, 2015
1 parent 572bc31 commit 90e72cf
Showing 1 changed file with 29 additions and 25 deletions.
54 changes: 29 additions & 25 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,19 +187,29 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{
} else {
self.arg_list.insert(a.name);
}
if let Some(ref s) = a.short {
if self.short_list.contains(s) {
if let Some(s) = a.short {
if self.short_list.contains(&s) {
panic!("Argument short must be unique, -{} is already in use", s);
} else {
self.short_list.insert(*s);
self.short_list.insert(s);
}
if s == 'h' {
self.needs_short_help = false;
} else if s == 'v' {
self.needs_short_version = false;
}
}
if let Some(ref l) = a.long {
if let Some(l) = a.long {
if self.long_list.contains(l) {
panic!("Argument long must be unique, --{} is already in use", l);
} else {
self.long_list.insert(l);
}
if l == "help" {
self.needs_long_help = false;
} else if l == "version" {
self.needs_long_version = false;
}
}
if a.required {
self.required.insert(a.name);
Expand Down Expand Up @@ -288,20 +298,6 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{
}
self.opts.insert(a.name, ob);
} else {
if let Some(ref l) = a.long {
if *l == "help" {
self.needs_long_help = false;
} else if *l == "version" {
self.needs_long_version = false;
}
}
if let Some(ref s) = a.short {
if *s == 'h' {
self.needs_short_help = false;
} else if *s == 'v' {
self.needs_short_version = false;
}
}
if a.short.is_none() && a.long.is_none() {
panic!("Argument \"{}\" must have either a short() and/or long() supplied since no index() or takes_value() were found", a.name);
}
Expand Down Expand Up @@ -901,26 +897,34 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{

fn create_help_and_version(&mut self) {
if self.needs_long_help {
self.flags.insert("clap_help", FlagBuilder {
let mut arg = FlagBuilder {
name: "clap_help",
short: if self.needs_short_help { Some('h') } else { None },
short: None,
long: Some("help"),
help: Some("Prints this message"),
help: Some("Prints help information"),
blacklist: None,
multiple: false,
requires: None,
});
};
if self.needs_short_help {
arg.short = Some('h');
}
self.flags.insert("clap_help", arg);
}
if self.needs_long_version {
self.flags.insert("clap_version", FlagBuilder {
let mut arg = FlagBuilder {
name: "clap_version",
short: if self.needs_short_help { Some('v') } else { None },
short: None,
long: Some("version"),
help: Some("Prints version information"),
blacklist: None,
multiple: false,
requires: None,
});
};
if self.needs_short_version {
arg.short = Some('v');
}
self.flags.insert("clap_version", arg);
}
if self.needs_subcmd_help && !self.subcommands.is_empty() {
self.subcommands.insert("help".to_owned(), App::new("help").about("Prints this message"));
Expand Down

0 comments on commit 90e72cf

Please sign in to comment.