Merge pull request #61 from kbknapp/cleanup

Cleanup and fix
This commit is contained in:
Kevin K. 2015-04-04 16:17:12 -04:00
commit 046546b3e3

View file

@ -187,19 +187,29 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{
} else { } else {
self.arg_list.insert(a.name); self.arg_list.insert(a.name);
} }
if let Some(ref s) = a.short { if let Some(s) = a.short {
if self.short_list.contains(s) { if self.short_list.contains(&s) {
panic!("Argument short must be unique, -{} is already in use", s); panic!("Argument short must be unique, -{} is already in use", s);
} else { } 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) { if self.long_list.contains(l) {
panic!("Argument long must be unique, --{} is already in use", l); panic!("Argument long must be unique, --{} is already in use", l);
} else { } else {
self.long_list.insert(l); 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 { if a.required {
self.required.insert(a.name); self.required.insert(a.name);
@ -211,9 +221,6 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{
if self.positionals_idx.contains_key(&i) { if self.positionals_idx.contains_key(&i) {
panic!("Argument \"{}\" has the same index as another positional argument", a.name); panic!("Argument \"{}\" has the same index as another positional argument", a.name);
} }
// if a.multiple {
// panic!("Argument \"{}\" has conflicting requirements, both index() and multiple(true) were supplied",a.name);
// }
if a.takes_value { if a.takes_value {
panic!("Argument \"{}\" has conflicting requirements, both index() and takes_value(true) were supplied", a.name); panic!("Argument \"{}\" has conflicting requirements, both index() and takes_value(true) were supplied", a.name);
} }
@ -291,20 +298,6 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{
} }
self.opts.insert(a.name, ob); self.opts.insert(a.name, ob);
} else { } 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() { 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); panic!("Argument \"{}\" must have either a short() and/or long() supplied since no index() or takes_value() were found", a.name);
} }
@ -316,11 +309,6 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{
} }
// No need to check for index() or takes_value() as that is handled above // No need to check for index() or takes_value() as that is handled above
// Flags can't be required
// This should be unreachable...
// if self.required.contains(a.name) {
// self.required.remove(a.name);
// }
let mut fb = FlagBuilder { let mut fb = FlagBuilder {
name: a.name, name: a.name,
short: a.short, short: a.short,
@ -665,6 +653,7 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{
30|_=> " " 30|_=> " "
} }
} }
fn print_version(&self, quit: bool) { fn print_version(&self, quit: bool) {
// Print the binary name if existing, but replace all spaces with hyphens in case we're // Print the binary name if existing, but replace all spaces with hyphens in case we're
// dealing with subcommands i.e. git mv is translated to git-mv // dealing with subcommands i.e. git mv is translated to git-mv
@ -674,7 +663,6 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{
fn exit(&self, status: i32) { fn exit(&self, status: i32) {
process::exit(status); process::exit(status);
// unsafe { libc::exit(0); }
} }
fn report_error(&self, msg: String, usage: bool, quit: bool) { fn report_error(&self, msg: String, usage: bool, quit: bool) {
@ -909,26 +897,34 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{
fn create_help_and_version(&mut self) { fn create_help_and_version(&mut self) {
if self.needs_long_help { if self.needs_long_help {
self.flags.insert("clap_help", FlagBuilder { let mut arg = FlagBuilder {
name: "clap_help", name: "clap_help",
short: if self.needs_short_help { Some('h') } else { None }, short: None,
long: Some("help"), long: Some("help"),
help: Some("Prints this message"), help: Some("Prints help information"),
blacklist: None, blacklist: None,
multiple: false, multiple: false,
requires: None, requires: None,
}); };
if self.needs_short_help {
arg.short = Some('h');
}
self.flags.insert("clap_help", arg);
} }
if self.needs_long_version { if self.needs_long_version {
self.flags.insert("clap_version", FlagBuilder { let mut arg = FlagBuilder {
name: "clap_version", name: "clap_version",
short: if self.needs_short_help { Some('v') } else { None }, short: None,
long: Some("version"), long: Some("version"),
help: Some("Prints version information"), help: Some("Prints version information"),
blacklist: None, blacklist: None,
multiple: false, multiple: false,
requires: None, 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() { if self.needs_subcmd_help && !self.subcommands.is_empty() {
self.subcommands.insert("help".to_owned(), App::new("help").about("Prints this message")); self.subcommands.insert("help".to_owned(), App::new("help").about("Prints this message"));
@ -1009,10 +1005,9 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{
self.required.remove(name); self.required.remove(name);
} }
} }
// No need to check for existance, returns None if not found
// if self.required.contains(v.name) {
self.required.remove(v.name); self.required.remove(v.name);
// }
if let Some(ref reqs) = v.requires { if let Some(ref reqs) = v.requires {
// Add all required args which aren't already found in matches to the // Add all required args which aren't already found in matches to the
// final required list // final required list
@ -1058,10 +1053,7 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{
// If this flag was requierd, remove it // If this flag was requierd, remove it
// .. even though Flags shouldn't be required // .. even though Flags shouldn't be required
// No need to check for existance, returns None if not found
// if self.required.contains(v.name) {
self.required.remove(v.name); self.required.remove(v.name);
// }
// Add all of this flags "mutually excludes" list to the master list // Add all of this flags "mutually excludes" list to the master list
if let Some(ref bl) = v.blacklist { if let Some(ref bl) = v.blacklist {
@ -1087,6 +1079,7 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{
// Shouldn't reach here // Shouldn't reach here
self.report_error(format!("Argument --{} isn't valid", arg), true, true); self.report_error(format!("Argument --{} isn't valid", arg), true, true);
// Can't reach here...
unreachable!(); unreachable!();
} }
@ -1138,10 +1131,9 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{
self.required.remove(name); self.required.remove(name);
} }
} }
// No need to check for existance, returns None if not found
// if self.required.contains(v.name) {
self.required.remove(v.name); self.required.remove(v.name);
// }
if let Some(ref reqs) = v.requires { if let Some(ref reqs) = v.requires {
// Add all required args which aren't already found in matches to the // Add all required args which aren't already found in matches to the
// final required list // final required list
@ -1190,11 +1182,7 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{
// If this flag was requierd, remove it // If this flag was requierd, remove it
// .. even though Flags shouldn't be required // .. even though Flags shouldn't be required
// No need to check for existance, returns None if it didn't exist
// if self.required.contains(v.name) {
self.required.remove(v.name); self.required.remove(v.name);
// }
// Add all of this flags "mutually excludes" list to the master list // Add all of this flags "mutually excludes" list to the master list
if let Some(ref bl) = v.blacklist { if let Some(ref bl) = v.blacklist {