mirror of
https://github.com/clap-rs/clap
synced 2024-11-10 23:04:23 +00:00
fix(flags): fix bug not allowing users to specify -v or -h
This commit is contained in:
parent
572bc31f08
commit
90e72cffde
1 changed files with 29 additions and 25 deletions
54
src/app.rs
54
src/app.rs
|
@ -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"));
|
||||
|
|
Loading…
Reference in a new issue