fix(flags): fix bug not allowing users to specify -v or -h

This commit is contained in:
Kevin K 2015-04-04 15:35:14 -04:00
parent 572bc31f08
commit 90e72cffde

View file

@ -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);
@ -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);
}
@ -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"));