diff --git a/src/app/app.rs b/src/app/app.rs index 0697b3dc..5b9dd1d8 100644 --- a/src/app/app.rs +++ b/src/app/app.rs @@ -16,8 +16,8 @@ use super::suggestions::{DidYouMeanMessageStyle, did_you_mean}; use super::errors::{ClapErrorType, ClapError}; -const INTERNAL_ERROR_MSG: &'static str = "Internal Error: Failed to write string. Please \ - consider filing a bug report!"; +const INTERNAL_ERROR_MSG: &'static str = "Internal Error: Failed to write string. Please consider \ + filing a bug report!"; /// Used to create a representation of a command line program and all possible command line /// arguments. @@ -55,7 +55,8 @@ pub struct App<'a, 'v, 'ab, 'u, 'h, 'ar> { author: Option<&'a str>, // The version displayed to the user version: Option<&'v str>, - // A brief explanation of the program that gets displayed to the user when shown help/usage + // A brief explanation of the program that gets displayed to the user when shown + // help/usage // information about: Option<&'ab str>, // Additional help information @@ -145,7 +146,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ global_ver: false, versionless_scs: None, unified_help: false, - overrides: vec![] + overrides: vec![], } } @@ -248,7 +249,9 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ /// .author("Me, me@mymain.com") /// # ; /// ``` - pub fn author(mut self, a: &'a str) -> Self { + pub fn author(mut self, + a: &'a str) + -> Self { self.author = Some(a); self } @@ -267,7 +270,9 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ /// .bin_name("my_binary") /// # ; /// ``` - pub fn bin_name(mut self, a: &str) -> Self { + pub fn bin_name(mut self, + a: &str) + -> Self { self.bin_name = Some(a.to_owned()); self } @@ -283,7 +288,9 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ /// .about("Does really amazing things to great people") /// # ; /// ``` - pub fn about(mut self, a: &'ab str) -> Self { + pub fn about(mut self, + a: &'ab str) + -> Self { self.about = Some(a); self } @@ -301,7 +308,9 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ /// .after_help("Does really amazing things to great people") /// # ; /// ``` - pub fn after_help(mut self, h: &'h str) -> Self { + pub fn after_help(mut self, + h: &'h str) + -> Self { self.more_help = Some(h); self } @@ -310,8 +319,8 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ /// if you had a subcommand or even top level application which had a required arguments that /// are only required as long as there is no subcommand present. /// - /// **Deprecated:** Use `App::setting()` with `AppSettings::SubcommandsNegateReqs` instead. This - /// method will be removed at 2.x + /// **Deprecated:** Use `App::setting()` with `AppSettings::SubcommandsNegateReqs` instead. + /// This method will be removed at 2.x /// /// **NOTE:** This defaults to false (using subcommand does *not* negate requirements) /// @@ -323,7 +332,9 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ /// .subcommands_negate_reqs(true) /// # ; /// ``` - pub fn subcommands_negate_reqs(mut self, n: bool) -> Self { + pub fn subcommands_negate_reqs(mut self, + n: bool) + -> Self { self.subcmds_neg_reqs = n; self } @@ -343,7 +354,9 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ /// .subcommand_required(true) /// # ; /// ``` - pub fn subcommand_required(mut self, n: bool) -> Self { + pub fn subcommand_required(mut self, + n: bool) + -> Self { self.no_sc_error = n; self } @@ -359,7 +372,9 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ /// .version("v0.1.24") /// # ; /// ``` - pub fn version(mut self, v: &'v str) -> Self { + pub fn version(mut self, + v: &'v str) + -> Self { self.version = Some(v); self } @@ -385,7 +400,9 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ /// .usage("myapp [-clDas] ") /// # ; /// ``` - pub fn usage(mut self, u: &'u str) -> Self { + pub fn usage(mut self, + u: &'u str) + -> Self { self.usage_str = Some(u); self } @@ -424,7 +441,9 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ /// work Do some work") /// # ; /// ``` - pub fn help(mut self, h: &'u str) -> Self { + pub fn help(mut self, + h: &'u str) + -> Self { self.help_str = Some(h); self } @@ -445,7 +464,9 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ /// // Using an uppercase `H` instead of the default lowercase `h` /// .help_short("H") /// # ; - pub fn help_short(mut self, s: &str) -> Self { + pub fn help_short(mut self, + s: &str) + -> Self { self.help_short = s.trim_left_matches(|c| c == '-') .chars() .nth(0); @@ -468,7 +489,9 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ /// // Using a lowercase `v` instead of the default capital `V` /// .version_short("v") /// # ; - pub fn version_short(mut self, s: &str) -> Self { + pub fn version_short(mut self, + s: &str) + -> Self { self.version_short = s.trim_left_matches(|c| c == '-') .chars() .nth(0); @@ -491,7 +514,9 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ /// .arg_required_else_help(true) /// # ; /// ``` - pub fn arg_required_else_help(mut self, tf: bool) -> Self { + pub fn arg_required_else_help(mut self, + tf: bool) + -> Self { self.help_on_no_args = tf; self } @@ -517,7 +542,9 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ /// // running `myprog test --version` will display /// // "myprog-test v1.1" /// ``` - pub fn global_version(mut self, gv: bool) -> Self { + pub fn global_version(mut self, + gv: bool) + -> Self { self.global_ver = gv; self } @@ -525,8 +552,8 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ /// Disables `-V` and `--version` for all subcommands (Defaults to false; subcommands have /// version flags) /// - /// **Deprecated:** Use `App::setting()` with `AppSettings::VersionlessSubcommands` instead. This - /// method will be removed at 2.x + /// **Deprecated:** Use `App::setting()` with `AppSettings::VersionlessSubcommands` instead. + /// This method will be removed at 2.x /// /// **NOTE:** This setting must be set **prior** adding any subcommands /// @@ -543,7 +570,9 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ /// .get_matches(); /// // running `myprog test --version` will display unknown argument error /// ``` - pub fn versionless_subcommands(mut self, vers: bool) -> Self { + pub fn versionless_subcommands(mut self, + vers: bool) + -> Self { self.versionless_scs = Some(vers); self } @@ -566,7 +595,9 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ /// .get_matches(); /// // running `myprog --help` will display a unified "docopt" or "getopts" style help message /// ``` - pub fn unified_help_message(mut self, uni_help: bool) -> Self { + pub fn unified_help_message(mut self, + uni_help: bool) + -> Self { self.unified_help = uni_help; self } @@ -594,7 +625,9 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ /// .arg_required_else_help(true) /// # ; /// ``` - pub fn wait_on_error(mut self, w: bool) -> Self { + pub fn wait_on_error(mut self, + w: bool) + -> Self { self.wait_on_error = w; self } @@ -602,8 +635,8 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ /// Specifies that the help text sould be displayed (and then exit gracefully), if no /// subcommands are present at runtime (i.e. an empty run such as, `$ myprog`. /// - /// **Deprecated:** Use `App::setting()` with `AppSettings::SubcommandRequiredElseHelp` instead. This - /// method will be removed at 2.x + /// **Deprecated:** Use `App::setting()` with `AppSettings::SubcommandRequiredElseHelp` + /// instead. This method will be removed at 2.x /// /// **NOTE:** This should *not* be used with `.subcommand_required()` as they do the same /// thing, except one prints the help text, and one prints an error. @@ -620,7 +653,9 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ /// .subcommand_required_else_help(true) /// # ; /// ``` - pub fn subcommand_required_else_help(mut self, tf: bool) -> Self { + pub fn subcommand_required_else_help(mut self, + tf: bool) + -> Self { self.help_on_no_sc = tf; self } @@ -636,13 +671,16 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ /// .setting(AppSettings::WaitOnError) /// # ; /// ``` - pub fn setting(mut self, setting: AppSettings) -> Self { + pub fn setting(mut self, + setting: AppSettings) + -> Self { self.add_setting(&setting); self } // actually adds the settings - fn add_setting(&mut self, s: &AppSettings) { + fn add_setting(&mut self, + s: &AppSettings) { match *s { AppSettings::SubcommandsNegateReqs => self.subcmds_neg_reqs = true, AppSettings::SubcommandRequired => self.no_sc_error = true, @@ -666,7 +704,9 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ /// AppSettings::WaitOnError]) /// # ; /// ``` - pub fn settings(mut self, settings: &[AppSettings]) -> Self { + pub fn settings(mut self, + settings: &[AppSettings]) + -> Self { for s in settings { self.add_setting(s); } @@ -700,15 +740,17 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ /// ) /// # ; /// ``` - pub fn arg(mut self, a: Arg<'ar, 'ar, 'ar, 'ar, 'ar, 'ar>) -> Self { + pub fn arg(mut self, + a: Arg<'ar, 'ar, 'ar, 'ar, 'ar, 'ar>) + -> Self { self.add_arg(a); self } // actually adds the arguments - fn add_arg(&mut self, a: Arg<'ar, 'ar, 'ar, 'ar, 'ar, 'ar>) { - if self.flags.contains_key(a.name) || - self.opts.contains_key(a.name) || + fn add_arg(&mut self, + a: Arg<'ar, 'ar, 'ar, 'ar, 'ar, 'ar>) { + if self.flags.contains_key(a.name) || self.opts.contains_key(a.name) || self.positionals_name.contains_key(a.name) { panic!("Argument name must be unique\n\n\t\"{}\" is already in use", a.name); } @@ -784,7 +826,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ global: a.global, empty_vals: a.empty_vals, validator: None, - overrides: None + overrides: None, }; if pb.min_vals.is_some() && !pb.multiple { panic!("Argument \"{}\" does not allow multiple values, yet it is expecting {} \ @@ -799,14 +841,18 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ if let Some(ref bl) = a.blacklist { let mut bhs = vec![]; // without derefing n = &&str - for n in bl { bhs.push(*n); } + for n in bl { + bhs.push(*n); + } bhs.dedup(); pb.blacklist = Some(bhs); } if let Some(ref or) = a.overrides { let mut bhs = vec![]; // without derefing n = &&str - for n in or { bhs.push(*n); } + for n in or { + bhs.push(*n); + } bhs.dedup(); pb.overrides = Some(bhs); } @@ -827,7 +873,9 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ if let Some(ref p) = a.possible_vals { let mut phs = vec![]; // without derefing n = &&str - for n in p { phs.push(*n); } + for n in p { + phs.push(*n); + } pb.possible_vals = Some(phs); } if let Some(ref p) = a.validator { @@ -857,7 +905,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ required: a.required, empty_vals: a.empty_vals, validator: None, - overrides: None + overrides: None, }; if let Some(ref vec) = ob.val_names { ob.num_vals = Some(vec.len() as u8); @@ -875,7 +923,9 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ if let Some(ref bl) = a.blacklist { let mut bhs = vec![]; // without derefing n = &&str - for n in bl { bhs.push(*n); } + for n in bl { + bhs.push(*n); + } bhs.dedup(); ob.blacklist = Some(bhs); } @@ -898,7 +948,9 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ if let Some(ref or) = a.overrides { let mut bhs = vec![]; // without derefing n = &&str - for n in or { bhs.push(*n); } + for n in or { + bhs.push(*n); + } bhs.dedup(); ob.overrides = Some(bhs); } @@ -906,7 +958,9 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ if let Some(ref p) = a.possible_vals { let mut phs = vec![]; // without derefing n = &&str - for n in p { phs.push(*n); } + for n in p { + phs.push(*n); + } ob.possible_vals = Some(phs); } self.opts.insert(a.name, ob); @@ -939,14 +993,16 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ global: a.global, multiple: a.multiple, requires: None, - overrides: None + overrides: None, }; // Check if there is anything in the blacklist (mutually excludes list) and add any // values if let Some(ref bl) = a.blacklist { let mut bhs = vec![]; // without derefing n = &&str - for n in bl { bhs.push(*n); } + for n in bl { + bhs.push(*n); + } bhs.dedup(); fb.blacklist = Some(bhs); } @@ -954,14 +1010,18 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ if let Some(ref r) = a.requires { let mut rhs = vec![]; // without derefing n = &&str - for n in r { rhs.push(*n); } + for n in r { + rhs.push(*n); + } rhs.dedup(); fb.requires = Some(rhs); } if let Some(ref or) = a.overrides { let mut bhs = vec![]; // without derefing n = &&str - for n in or { bhs.push(*n); } + for n in or { + bhs.push(*n); + } bhs.dedup(); fb.overrides = Some(bhs); } @@ -969,7 +1029,8 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ } if a.global { if a.required { - panic!("Global arguments cannot be required.\n\n\t'{}' is marked as global and required", a.name); + panic!("Global arguments cannot be required.\n\n\t'{}' is marked as global and \ + required", a.name); } self.global_args.push(a); } @@ -989,7 +1050,8 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ /// ) /// # ; /// ``` - pub fn args(mut self, args: Vec>) + pub fn args(mut self, + args: Vec>) -> Self { for arg in args.into_iter() { self = self.arg(arg); @@ -1013,7 +1075,9 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ /// .arg_from_usage("-c --conf= 'Sets a configuration file to use'") /// # ; /// ``` - pub fn arg_from_usage(mut self, usage: &'ar str) -> Self { + pub fn arg_from_usage(mut self, + usage: &'ar str) + -> Self { self = self.arg(Arg::from_usage(usage)); self } @@ -1039,7 +1103,9 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ /// ) /// # ; /// ``` - pub fn args_from_usage(mut self, usage: &'ar str) -> Self { + pub fn args_from_usage(mut self, + usage: &'ar str) + -> Self { for l in usage.lines() { self = self.arg(Arg::from_usage(l.trim())); } @@ -1078,7 +1144,9 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ /// .add_all(&["ver", "major", "minor","patch"]) /// .required(true)) /// # ; - pub fn arg_group(mut self, group: ArgGroup<'ar, 'ar>) -> Self { + pub fn arg_group(mut self, + group: ArgGroup<'ar, 'ar>) + -> Self { if group.required { self.required.push(group.name); if let Some(ref reqs) = group.requires { @@ -1140,7 +1208,9 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ /// .add_all(&["ver", "major", "minor","patch"]) /// .required(true)) /// # ; - pub fn arg_groups(mut self, groups: Vec>) -> Self { + pub fn arg_groups(mut self, + groups: Vec>) + -> Self { for g in groups { self = self.arg_group(g); } @@ -1164,9 +1234,12 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ /// // Additional subcommand configuration goes here, such as other arguments... /// # ; /// ``` - pub fn subcommand(mut self, mut subcmd: App<'a, 'v, 'ab, 'u, 'h, 'ar>) + pub fn subcommand(mut self, + mut subcmd: App<'a, 'v, 'ab, 'u, 'h, 'ar>) -> Self { - if subcmd.name == "help" { self.needs_subcmd_help = false; } + if subcmd.name == "help" { + self.needs_subcmd_help = false; + } if self.versionless_scs.is_some() && self.versionless_scs.unwrap() { subcmd.versionless_scs = Some(false); } @@ -1191,7 +1264,8 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ /// SubCommand::with_name("debug").about("Controls debug functionality")]) /// # ; /// ``` - pub fn subcommands(mut self, subcmds: Vec>) + pub fn subcommands(mut self, + subcmds: Vec>) -> Self { for subcmd in subcmds.into_iter() { self = self.subcommand(subcmd); @@ -1199,7 +1273,9 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ self } - fn get_group_members(&self, group: &str) -> Vec { + fn get_group_members(&self, + group: &str) + -> Vec { let mut g_vec = vec![]; let mut args = vec![]; @@ -1231,7 +1307,9 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ args.iter().map(ToOwned::to_owned).collect() } - fn get_group_members_names(&self, group: &'ar str) -> Vec<&'ar str> { + fn get_group_members_names(&self, + group: &'ar str) + -> Vec<&'ar str> { let mut g_vec = vec![]; let mut args = vec![]; @@ -1261,7 +1339,9 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ args.iter().map(|s| *s).collect() } - fn get_required_from(&self, mut reqs: Vec<&'ar str>) -> VecDeque { + fn get_required_from(&self, + mut reqs: Vec<&'ar str>) + -> VecDeque { reqs.dedup(); let mut c_flags = vec![]; let mut c_pos = vec![]; @@ -1361,10 +1441,10 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ } pmap.into_iter().map(|(_, s)| ret_val.push_back(s)).collect::>(); for f in c_flags.into_iter() { - ret_val.push_back(format!("{}", self.flags.get(*f).unwrap())); + ret_val.push_back(format!("{}", self.flags.get(*f).unwrap())); } for o in c_opt.into_iter() { - ret_val.push_back(format!("{}", self.opts.get(*o).unwrap())); + ret_val.push_back(format!("{}", self.opts.get(*o).unwrap())); } for g in grps.into_iter() { let g_string = self.get_group_members(g).iter() @@ -1380,8 +1460,10 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ // Creates a usage string if one was not provided by the user manually. This happens just // after all arguments were parsed, but before any subcommands have been parsed (so as to // give subcommands their own usage recursively) - fn create_usage(&self, matches: Option>) -> String { - use ::std::fmt::Write; + fn create_usage(&self, + matches: Option>) + -> String { + use std::fmt::Write; let mut usage = String::with_capacity(75); usage.push_str("USAGE:\n\t"); if let Some(u) = self.usage_str { @@ -1430,19 +1512,19 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ } else { usage.push_str(" [OPTIONS]"); } - if !self.unified_help - && !self.opts.is_empty() && self.opts.values().any(|a| !a.required) { + if !self.unified_help && !self.opts.is_empty() && + self.opts.values().any(|a| !a.required) { usage.push_str(" [OPTIONS]"); } // places a '--' in the usage string if there are args and options // supporting multiple values - if !self.positionals_idx.is_empty() - && self.opts.values().any(|a| a.multiple ) - && !self.opts.values().any(|a| a.required) - && self.subcommands.is_empty() { + if !self.positionals_idx.is_empty() && self.opts.values().any(|a| a.multiple ) && + !self.opts.values().any(|a| a.required) && + self.subcommands.is_empty() { usage.push_str(" [--]") } - if !self.positionals_idx.is_empty() && self.positionals_idx.values() + if !self.positionals_idx.is_empty() && + self.positionals_idx.values() .any(|a| !a.required) { usage.push_str(" [ARGS]"); } @@ -1491,9 +1573,11 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ .filter(|f| f.long.is_some()) // 2='--' .map(|a| a.to_string().len() ) { - if fl > longest_flag { longest_flag = fl; } + if fl > longest_flag { + longest_flag = fl; + } } - let mut longest_opt= 0; + let mut longest_opt = 0; for ol in self.opts .values() // .filter(|ref o| o.long.is_some()) @@ -1508,13 +1592,17 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ for pl in self.positionals_idx .values() .map(|f| f.to_string().len() ) { - if pl > longest_pos {longest_pos = pl;} + if pl > longest_pos { + longest_pos = pl; + } } let mut longest_sc = 0; for scl in self.subcommands .values() .map(|f| f.name.len() ) { - if scl > longest_sc {longest_sc = scl;} + if scl > longest_sc { + longest_sc = scl; + } } if let Some(author) = self.author { @@ -1683,26 +1771,31 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ } // Used when spacing arguments and their help message when displaying help information - fn print_spaces(&self, num: usize) { + fn print_spaces(&self, + num: usize) { for _ in (0..num) { print!(" "); } } // Prints the version to the user and exits if quit=true - 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 // dealing with subcommands i.e. git mv is translated to git-mv println!("{} {}", &self.bin_name.clone().unwrap_or( self.name.clone())[..].replace(" ", "-"), self.version.unwrap_or("") ); - if quit { self.exit(0); } + if quit { + self.exit(0); + } } // Exits with a status code passed to the OS // This is legacy from before std::process::exit() and may be removed evenutally - fn exit(&self, status: i32) { + fn exit(&self, + status: i32) { if self.wait_on_error { wlnerr!("\nPress [ENTER] / [RETURN] to continue..."); let mut s = String::new(); @@ -1716,7 +1809,8 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ fn report_error(&self, msg: String, error_type: ClapErrorType, - usage_vec: Option>) -> ClapError { + usage_vec: Option>) + -> ClapError { ClapError { error: format!("{} {}\n\n{}\n\nFor more information try {}", Format::Error(&format!("error:")[..]), @@ -1724,7 +1818,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ self.create_usage(usage_vec), Format::Good("--help") ), - error_type: error_type + error_type: error_type, } } @@ -1752,7 +1846,8 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ /// Starts the parsing process. Called on top level parent app **ONLY** then recursively calls /// the real parsing function for all subcommands /// - /// **NOTE:** This method should only be used when is absolutely necessary to handle errors manually. + /// **NOTE:** This method should only be used when is absolutely necessary to handle errors + /// manually. /// /// /// # Example @@ -1789,10 +1884,12 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ /// // Args and options go here... /// .get_matches_from(arg_vec); /// ``` - pub fn get_matches_from(mut self, itr: I) + pub fn get_matches_from(mut self, + itr: I) -> ArgMatches<'ar, 'ar> - where I: IntoIterator, - T: AsRef { + where I: IntoIterator, + T: AsRef + { // Verify all positional assertions pass self.verify_positionals(); // If there are global arguments, we need to propgate them down to subcommands before @@ -1836,7 +1933,8 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ /// parse arguments from something other than `std::env::args()`. If you are unsure, use /// `App::get_matches_safe()` /// - /// **NOTE:** This method should only be used when is absolutely necessary to handle errors manually. + /// **NOTE:** This method should only be used when is absolutely necessary to handle errors + /// manually. /// /// /// # Example @@ -1850,10 +1948,12 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ /// .get_matches_from_safe(arg_vec) /// .unwrap_or_else( |e| { panic!("An error occurs: {}", e) }); /// ``` - pub fn get_matches_from_safe(mut self, itr: I) - -> Result, ClapError> - where I: IntoIterator, - T: AsRef { + pub fn get_matches_from_safe(mut self, + itr: I) + -> Result, ClapError> + where I: IntoIterator, + T: AsRef + { // Verify all positional assertions pass self.verify_positionals(); // If there are global arguments, we need to propgate them down to subcommands before @@ -1944,8 +2044,10 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ arg: &str, opt: &str, p_vals: &[&str], - matches: &ArgMatches<'ar, 'ar>) -> ClapError { - let suffix = App::did_you_mean_suffix(arg, p_vals.iter(), + matches: &ArgMatches<'ar, 'ar>) + -> ClapError { + let suffix = App::did_you_mean_suffix(arg, + p_vals.iter(), DidYouMeanMessageStyle::EnumValue); let mut sorted = vec![]; @@ -1968,10 +2070,13 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ } // The actual parsing function - fn get_matches_with(&mut self, matches: &mut ArgMatches<'ar, 'ar>, it: &mut I) -> Result<(), ClapError> - where I: Iterator, - T: AsRef { - + fn get_matches_with(&mut self, + matches: &mut ArgMatches<'ar, 'ar>, + it: &mut I) + -> Result<(), ClapError> + where I: Iterator, + T: AsRef + { // First we create the `--help` and `--version` arguments and add them if necessary self.create_help_and_version(); @@ -1995,8 +2100,8 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ false }; - // pos_only is determined later, and set true when a user uses the Unix standard of '--' - // to mean only positionals follow + // pos_only is determined later, and set true when a user uses the Unix standard of + // '--' to mean only positionals follow if !pos_only && !new_arg && !self.subcommands.contains_key(arg_slice) { // Check to see if parsing a value from an option if let Some(nvo) = needs_val_of { @@ -2014,8 +2119,8 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ if let Some(ref ma) = matches.args.get(opt.name) { if let Some(ref vals) = ma.values { if num == vals.len() as u8 && !opt.multiple { - return Err(self.report_error(format!("The argument '{}' was found, \ - but '{}' only expects {} values", + return Err(self.report_error(format!("The argument '{}' \ + was found, but '{}' only expects {} values", Format::Warning(arg.as_ref()), Format::Warning(opt.to_string()), Format::Good(vals.len().to_string())), @@ -2028,18 +2133,20 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ } // if it's an empty value, and we don't allow that, report the error - if !opt.empty_vals && - matches.args.contains_key(opt.name) && - arg_slice.is_empty() { - return Err(self.report_error(format!("The argument '{}' does not allow empty \ - values, but one was found.", Format::Warning(opt.to_string())), + if !opt.empty_vals && matches.args.contains_key(opt.name) && + arg_slice.is_empty() { + return Err(self.report_error( + format!("The argument '{}' does not allow empty values, but one \ + was found.", Format::Warning(opt.to_string())), ClapErrorType::EmptyValue, - App::get_args(matches))); + App::get_args(matches) + )); } // save the value to matched option if let Some(ref mut o) = matches.args.get_mut(opt.name) { - // if it's multiple; the occurrences are increased when originally found + // if it's multiple; the occurrences are increased when originally + // found o.occurrences = if opt.multiple { o.occurrences + 1 } else { @@ -2061,18 +2168,20 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ let len = vals.len() as u8 + 1; vals.insert(len, arg_slice.to_owned()); - // Now that the values have been added, we can ensure we haven't gone - // over any max_limits, or if we've reached the exact number of values - // we can stop parsing values, and go back to arguments. + // Now that the values have been added, we can ensure we haven't + // gone over any max_limits, or if we've reached the exact number + // of values we can stop parsing values, and go back to arguments. // - // For example, if we define an option with exactly 2 values and the - // users passes: + // For example, if we define an option with exactly 2 values and + // the users passes: // $ my_prog --option val1 val2 pos1 - // we stop parsing values of --option after val2, if the user hadn't - // defined an exact or max value, pos1 would be parsed as a value of - // --option + // we stop parsing values of --option after val2, if the user + // hadn't defined an exact or max value, pos1 would be parsed as a + // value of --option if let Some(num) = opt.max_vals { - if len != num { continue } + if len != num { + continue + } } else if let Some(num) = opt.num_vals { if opt.multiple { val_counter += 1; @@ -2083,7 +2192,9 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ } } else { // if we need more values, get the next value - if len != num { continue } + if len != num { + continue + } } } else if !skip { // get the next value from the iterator @@ -2125,13 +2236,13 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ // This arg is either an option or flag using a long (i.e. '--something') match self.parse_long_arg(matches, arg_slice) { Ok(r) => needs_val_of = r, - Err(e) => return Err(e) + Err(e) => return Err(e), } - } else if arg_slice.starts_with("-") && arg_slice.len() != 1 && ! pos_only { + } else if arg_slice.starts_with("-") && arg_slice.len() != 1 && !pos_only { // Multiple or single flag(s), or single option (could be '-SbG' or '-o') match self.parse_short_arg(matches, arg_slice) { Ok(r) => needs_val_of = r, - Err(e) => return Err(e) + Err(e) => return Err(e), } } else { // Positional or Subcommand @@ -2178,7 +2289,8 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ // anyways // matches.args.remove(p.name); - return Err(self.report_error(format!("The argument '{}' cannot be used with {}", + return Err(self.report_error(format!("The argument '{}' cannot be used \ + with {}", Format::Warning(p.to_string()), match self.blacklisted_from(p.name, &matches) { Some(name) => format!("'{}'", Format::Warning(name)), @@ -2204,8 +2316,8 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ if let Some(ref ma) = matches.args.get(p.name) { if let Some(ref vals) = ma.values { if vals.len() as u8 == num { - return Err(self.report_error(format!("The argument '{}' was found, \ - but '{}' wasn't expecting any more values", + return Err(self.report_error(format!("The argument '{}' \ + was found, but '{}' wasn't expecting any more values", Format::Warning(arg.as_ref()), Format::Warning(p.to_string())), ClapErrorType::TooMuchValues, @@ -2214,10 +2326,11 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ } } } - if !p.empty_vals && matches.args.contains_key(p.name) - && arg_slice.is_empty() { - return Err(self.report_error(format!("The argument '{}' does not allow empty \ - values, but one was found.", Format::Warning(p.to_string())), + if !p.empty_vals && matches.args.contains_key(p.name) && + arg_slice.is_empty() { + return Err(self.report_error(format!("The argument '{}' does not \ + allow empty values, but one was found.", + Format::Warning(p.to_string())), ClapErrorType::EmptyValue, App::get_args(matches))); } @@ -2253,8 +2366,9 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ } let mut bm = BTreeMap::new(); if !p.empty_vals && arg_slice.is_empty() { - return Err(self.report_error(format!("The argument '{}' does not allow empty \ - values, but one was found.", Format::Warning(p.to_string())), + return Err(self.report_error(format!("The argument '{}' does not \ + allow empty values, but one was found.", + Format::Warning(p.to_string())), ClapErrorType::EmptyValue, App::get_args(matches))); } @@ -2283,7 +2397,9 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ // Add all required args which aren't already found in matches to the // final required list for n in reqs { - if matches.args.contains_key(n) {continue;} + if matches.args.contains_key(n) { + continue; + } self.required.push(n); } @@ -2293,8 +2409,8 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ } } else { - return Err(self.report_error(format!("The argument '{}' was found, but '{}' wasn't \ - expecting any", Format::Warning(arg.as_ref()), + return Err(self.report_error(format!("The argument '{}' was found, but '{}' \ + wasn't expecting any", Format::Warning(arg.as_ref()), self.bin_name.clone().unwrap_or(self.name.clone())), ClapErrorType::UnexpectedArgument, App::get_args(matches))); @@ -2306,7 +2422,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ if o.multiple && self.required.is_empty() { let should_err = match matches.values_of(o.name) { Some(ref v) => v.is_empty(), - None => true, + None => true, }; if should_err { return Err(self.report_error( @@ -2315,17 +2431,15 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ ClapErrorType::EmptyValue, App::get_args(matches))); } - } - else if !o.multiple { + } else if !o.multiple { return Err(self.report_error( format!("The argument '{}' requires a value but none was supplied", Format::Warning(o.to_string())), ClapErrorType::EmptyValue, App::get_args(matches))); - } - else { - return Err(self.report_error(format!("The following required arguments were not \ - supplied:{}", + } else { + return Err(self.report_error(format!("The following required arguments were \ + not supplied:{}", self.get_required_from(self.required.iter() .map(|s| *s) .collect::>()) @@ -2358,7 +2472,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ matches.usage = Some(self.create_usage(None)); if let Some(sc_name) = subcmd_name { - use ::std::fmt::Write; + use std::fmt::Write; let mut mid_string = String::new(); if !self.subcmds_neg_reqs { let mut hs = self.required.iter().map(|n| *n).collect::>(); @@ -2396,12 +2510,13 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ } matches.subcommand = Some(Box::new(SubCommand { name: sc.name_slice, - matches: new_matches + matches: new_matches, })); } } else if self.no_sc_error { let bn = self.bin_name.clone().unwrap_or(self.name.clone()); - return Err(self.report_error(format!("'{}' requires a subcommand but none was provided", + return Err(self.report_error( + format!("'{}' requires a subcommand but none was provided", Format::Warning(&bn[..])), ClapErrorType::MissingSubcommand, App::get_args(matches))); @@ -2409,7 +2524,8 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ self.print_help(); self.exit(1); } - if ((!self.subcmds_neg_reqs) || matches.subcommand_name().is_none()) && self.validate_required(&matches) { + if ((!self.subcmds_neg_reqs) || matches.subcommand_name().is_none()) && + self.validate_required(&matches) { return Err(self.report_error(format!("The following required arguments were not \ supplied:{}", self.get_required_from(self.required.iter() @@ -2428,7 +2544,10 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ Ok(()) } - fn blacklisted_from(&self, name: &str, matches: &ArgMatches) -> Option { + fn blacklisted_from(&self, + name: &str, + matches: &ArgMatches) + -> Option { for k in matches.args.keys() { if let Some(f) = self.flags.get(k) { if let Some(ref bl) = f.blacklist { @@ -2453,11 +2572,14 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ } } } - } + } None } - fn overriden_from(&self, name: &'ar str, matches: &ArgMatches) -> Option<&'ar str> { + fn overriden_from(&self, + name: &'ar str, + matches: &ArgMatches) + -> Option<&'ar str> { for k in matches.args.keys() { if let Some(f) = self.flags.get(k) { if let Some(ref bl) = f.overrides { @@ -2482,7 +2604,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ } } } - } + } None } @@ -2501,14 +2623,13 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ multiple: false, global: false, requires: None, - overrides: None + overrides: None, }; self.long_list.push("help"); self.flags.insert("hclap_help", arg); } - if self.needs_long_version - && self.versionless_scs.is_none() - || (self.versionless_scs.unwrap()) { + if self.needs_long_version && self.versionless_scs.is_none() || + (self.versionless_scs.unwrap()) { if self.version_short.is_none() && !self.short_list.contains(&'V') { self.version_short = Some('V'); } @@ -2522,7 +2643,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ multiple: false, global: false, requires: None, - overrides: None + overrides: None, }; self.long_list.push("version"); self.flags.insert("vclap_version", arg); @@ -2533,17 +2654,24 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ } } - fn check_for_help_and_version(&self, arg: char) { + fn check_for_help_and_version(&self, + arg: char) { if let Some(h) = self.help_short { - if h == arg { self.print_help(); } + if h == arg { + self.print_help(); + } } if let Some(v) = self.version_short { - if v == arg { self.print_version(true); } + if v == arg { + self.print_version(true); + } } } - fn parse_long_arg<'av>(&mut self, matches: &mut ArgMatches<'ar, 'ar> ,full_arg: &'av str) - -> Result, ClapError> { + fn parse_long_arg<'av>(&mut self, + matches: &mut ArgMatches<'ar, 'ar>, + full_arg: &'av str) + -> Result, ClapError> { let mut arg = full_arg.trim_left_matches(|c| c == '-'); if arg == "help" && self.needs_long_help { @@ -2566,8 +2694,8 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ occurrences: 1, values: None }); - return Err(self.report_error(format!("The argument '{}' requires a value, but none was \ - supplied", Format::Warning(format!("--{}", arg))), + return Err(self.report_error(format!("The argument '{}' requires a value, \ + but none was supplied", Format::Warning(format!("--{}", arg))), ClapErrorType::EmptyValue, App::get_args(matches))); } @@ -2581,8 +2709,9 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ // Ensure this option isn't on the master mutually excludes list if self.blacklist.contains(&v.name) { matches.args.remove(v.name); - return Err(self.report_error(format!("The argument '{}' cannot be used with one or more of \ - the other specified arguments", Format::Warning(format!("--{}", arg))), + return Err(self.report_error(format!("The argument '{}' cannot be used with one \ + or more of the other specified arguments", + Format::Warning(format!("--{}", arg))), ClapErrorType::ArgumentConflict, App::get_args(matches))); } @@ -2607,8 +2736,8 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ if matches.args.contains_key(v.name) { if !v.multiple { - return Err(self.report_error(format!("The argument '{}' was supplied more than once, but \ - does not support multiple values", + return Err(self.report_error(format!("The argument '{}' was supplied more \ + than once, but does not support multiple values", Format::Warning(format!("--{}", arg))), ClapErrorType::UnexpectedMultipleUsage, App::get_args(matches))); @@ -2658,7 +2787,9 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ // Add all required args which aren't already found in matches to the // final required list for n in reqs { - if matches.args.contains_key(n) { continue; } + if matches.args.contains_key(n) { + continue; + } self.required.push(n); } @@ -2667,8 +2798,12 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ parse_group_reqs!(self, v); match arg_val { - None => { return Ok(Some(v.name)); }, - _ => { return Ok(None); } + None => { + return Ok(Some(v.name)); + } + _ => { + return Ok(None); + } } } @@ -2709,8 +2844,9 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ // Make sure this isn't one being added multiple times if it doesn't suppor it if matches.args.contains_key(v.name) && !v.multiple { - return Err(self.report_error(format!("The argument '{}' was supplied more than once, but does \ - not support multiple values", Format::Warning(v.to_string())), + return Err(self.report_error(format!("The argument '{}' was supplied more than \ + once, but does not support multiple values", + Format::Warning(v.to_string())), ClapErrorType::UnexpectedMultipleUsage, App::get_args(matches))); } @@ -2719,7 +2855,11 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ done = false; if let Some(ref mut f) = matches.args.get_mut(v.name) { done = true; - f.occurrences = if v.multiple { f.occurrences + 1 } else { 1 }; + f.occurrences = if v.multiple { + f.occurrences + 1 + } else { + 1 + }; } if !done { matches.args.insert(v.name, MatchedArg{ @@ -2746,7 +2886,9 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ // Add all required args which aren't already found in matches to the master list if let Some(ref reqs) = v.requires { for n in reqs { - if matches.args.contains_key(n) { continue; } + if matches.args.contains_key(n) { + continue; + } self.required.push(n); } @@ -2797,7 +2939,11 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ App::get_args(matches))) } - fn validate_value(&self, v: &OptBuilder, av: &str, matches: &ArgMatches) -> Result<(), ClapError> { + fn validate_value(&self, + v: &OptBuilder, + av: &str, + matches: &ArgMatches) + -> Result<(), ClapError> { if let Some(ref p_vals) = v.possible_vals { if !p_vals.contains(&av) { self.possible_values_error(av, &v.to_string(), p_vals, matches); @@ -2819,7 +2965,9 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ Ok(()) } - fn parse_short_arg(&mut self, matches: &mut ArgMatches<'ar, 'ar> ,full_arg: &str) + fn parse_short_arg(&mut self, + matches: &mut ArgMatches<'ar, 'ar>, + full_arg: &str) -> Result, ClapError> { let arg = &full_arg[..].trim_left_matches(|c| c == '-'); if arg.len() > 1 { @@ -2832,10 +2980,10 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ return Err(self.report_error(format!("The argument '{}' isn't valid", Format::Warning(format!("-{}", c))), ClapErrorType::InvalidArgument, - App::get_args(matches))); + App::get_args(matches))); } - }, - Err(e) => return Err(e) + } + Err(e) => return Err(e), } } return Ok(None); @@ -2852,8 +3000,8 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ if b { return Ok(None); } - }, - Err(e) => return Err(e) + } + Err(e) => return Err(e), } // Check for matching short in options, and return the name @@ -2891,8 +3039,8 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ if matches.args.contains_key(v.name) { if !v.multiple { - return Err(self.report_error(format!("The argument '{}' was supplied more than once, but \ - does not support multiple values", + return Err(self.report_error(format!("The argument '{}' was supplied more \ + than once, but does not support multiple values", Format::Warning(format!("-{}", arg))), ClapErrorType::UnexpectedMultipleUsage, App::get_args(matches))); @@ -2915,7 +3063,9 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ // Add all required args which aren't already found in matches to the // final required list for n in reqs { - if matches.args.contains_key(n) { continue; } + if matches.args.contains_key(n) { + continue; + } self.required.push(n); } @@ -2933,7 +3083,10 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ App::get_args(matches))) } - fn parse_single_short_flag(&mut self, matches: &mut ArgMatches<'ar, 'ar>, arg: char) -> Result { + fn parse_single_short_flag(&mut self, + matches: &mut ArgMatches<'ar, 'ar>, + arg: char) + -> Result { if let Some(v) = self.flags.values() .filter(|&v| v.short.is_some()) .filter(|&v| v.short.unwrap() == arg).nth(0) { @@ -2972,8 +3125,8 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ // Make sure this isn't one being added multiple times if it doesn't suppor it if matches.args.contains_key(v.name) && !v.multiple { - return Err(self.report_error(format!("The argument '{}' was supplied more than once, but does \ - not support multiple values", + return Err(self.report_error(format!("The argument '{}' was supplied more than \ + once, but does not support multiple values", Format::Warning(format!("-{}", arg))), ClapErrorType::UnexpectedMultipleUsage, App::get_args(matches))); @@ -2982,7 +3135,11 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ let mut done = false; if let Some(ref mut f) = matches.args.get_mut(v.name) { done = true; - f.occurrences = if v.multiple { f.occurrences + 1 } else { 1 }; + f.occurrences = if v.multiple { + f.occurrences + 1 + } else { + 1 + }; } if !done { matches.args.insert(v.name, MatchedArg{ @@ -3002,7 +3159,9 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ // Add all required args which aren't already found in matches to the master list if let Some(ref reqs) = v.requires { for n in reqs { - if matches.args.contains_key(n) { continue; } + if matches.args.contains_key(n) { + continue; + } self.required.push(n); } @@ -3015,7 +3174,9 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ Ok(false) } - fn validate_blacklist(&self, matches: &mut ArgMatches<'ar, 'ar>) -> Result<(), ClapError> { + fn validate_blacklist(&self, + matches: &mut ArgMatches<'ar, 'ar>) + -> Result<(), ClapError> { for name in self.blacklist.iter() { if matches.args.contains_key(name) { matches.args.remove(name); @@ -3039,8 +3200,8 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ for n in self.get_group_members_names(name) { if matches.args.contains_key(n) { matches.args.remove(n); - return Err(self.report_error(format!("The argument '{}' cannot be used with one or \ - more of the other specified arguments", + return Err(self.report_error(format!("The argument '{}' cannot be used \ + with one or more of the other specified arguments", if let Some(ref flag) = self.flags.get(n) { format!("{}", Format::Warning(flag.to_string())) } else if let Some(ref opt) = self.opts.get(n) { @@ -3062,7 +3223,9 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ Ok(()) } - fn validate_num_args(&self, matches: &mut ArgMatches<'ar, 'ar>) -> Result<(), ClapError> { + fn validate_num_args(&self, + matches: &mut ArgMatches<'ar, 'ar>) + -> Result<(), ClapError> { for (name, ma) in matches.args.iter() { if let Some(ref vals) = ma.values { if let Some(f) = self.opts.get(name) { @@ -3073,8 +3236,8 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ num != (vals.len() as u8) }; if should_err { - return Err(self.report_error(format!("The argument '{}' requires {} values, \ - but {} w{} provided", + return Err(self.report_error(format!("The argument '{}' requires {} \ + values, but {} w{} provided", Format::Warning(f.to_string()), Format::Good(num.to_string()), Format::Error(if f.multiple { @@ -3091,8 +3254,8 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ } if let Some(num) = f.max_vals { if (vals.len() as u8) > num { - return Err(self.report_error(format!("The argument '{}' requires no more than {} \ - values, but {} w{} provided", + return Err(self.report_error(format!("The argument '{}' requires no \ + more than {} values, but {} w{} provided", Format::Warning(f.to_string()), Format::Good(num.to_string()), Format::Error(vals.len().to_string()), @@ -3103,8 +3266,8 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ } if let Some(num) = f.min_vals { if (vals.len() as u8) < num { - return Err(self.report_error(format!("The argument '{}' requires at least {} \ - values, but {} w{} provided", + return Err(self.report_error(format!("The argument '{}' requires at \ + least {} values, but {} w{} provided", Format::Warning(f.to_string()), Format::Good(num.to_string()), Format::Error(vals.len().to_string()), @@ -3117,8 +3280,8 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ self.positionals_name.get(name).unwrap()) { if let Some(num) = f.num_vals { if num != vals.len() as u8 { - return Err(self.report_error(format!("The argument '{}' requires {} values, \ - but {} w{} provided", + return Err(self.report_error(format!("The argument '{}' requires {} \ + values, but {} w{} provided", Format::Warning(f.to_string()), Format::Good(num.to_string()), Format::Error(vals.len().to_string()), @@ -3133,8 +3296,8 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ } if let Some(num) = f.max_vals { if num > vals.len() as u8 { - return Err(self.report_error(format!("The argument '{}' requires no more than {} \ - values, but {} w{} provided", + return Err(self.report_error(format!("The argument '{}' requires no \ + more than {} values, but {} w{} provided", Format::Warning(f.to_string()), Format::Good(num.to_string()), Format::Error(vals.len().to_string()), @@ -3145,8 +3308,8 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ } if let Some(num) = f.min_vals { if num < vals.len() as u8 { - return Err(self.report_error(format!("The argument '{}' requires at least {} \ - values, but {} w{} provided", + return Err(self.report_error(format!("The argument '{}' requires at \ + least {} values, but {} w{} provided", Format::Warning(f.to_string()), Format::Good(num.to_string()), Format::Error(vals.len().to_string()), @@ -3161,7 +3324,9 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ Ok(()) } - fn validate_required(&self, matches: &ArgMatches<'ar, 'ar>) -> bool { + fn validate_required(&self, + matches: &ArgMatches<'ar, 'ar>) + -> bool { 'outer: for name in self.required.iter() { if matches.args.contains_key(name) { continue 'outer; @@ -3205,7 +3370,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ } // because positions use different keys, we dont use the macro match self.positionals_idx.values().filter(|p| &p.name == name).next() { - Some(p) =>{ + Some(p) => { if let Some(ref bl) = p.blacklist { for n in bl.iter() { if matches.args.contains_key(n) { @@ -3220,8 +3385,8 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ } } } - }, - None =>(), + } + None => (), } return true; } @@ -3230,26 +3395,31 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ /// Returns a suffix that can be empty, or is the standard 'did you mean phrase #[cfg_attr(feature = "lints", allow(needless_lifetimes))] - fn did_you_mean_suffix<'z, T, I>(arg: &str, values: I, style: DidYouMeanMessageStyle) - -> (String, Option<&'z str>) - where T: AsRef + 'z, - I: IntoIterator { + fn did_you_mean_suffix<'z, T, I>(arg: &str, + values: I, + style: DidYouMeanMessageStyle) + -> (String, Option<&'z str>) + where T: AsRef + 'z, + I: IntoIterator + { match did_you_mean(arg, values) { - Some(candidate) => { - let mut suffix = "\n\tDid you mean ".to_owned(); - match style { - DidYouMeanMessageStyle::LongFlag => suffix.push_str(&Format::Good("--").to_string()[..]), - DidYouMeanMessageStyle::EnumValue => suffix.push('\''), - } - suffix.push_str(&Format::Good(candidate).to_string()[..]); - if let DidYouMeanMessageStyle::EnumValue = style { - suffix.push('\''); - } - suffix.push_str(" ?"); - (suffix, Some(candidate)) - }, - None => (String::new(), None), + Some(candidate) => { + let mut suffix = "\n\tDid you mean ".to_owned(); + match style { + DidYouMeanMessageStyle::LongFlag => suffix.push_str( + &*format!("{}", Format::Good("--")) + ), + DidYouMeanMessageStyle::EnumValue => suffix.push('\''), + } + suffix.push_str(&Format::Good(candidate).to_string()[..]); + if let DidYouMeanMessageStyle::EnumValue = style { + suffix.push('\''); + } + suffix.push_str(" ?"); + (suffix, Some(candidate)) + } + None => (String::new(), None), } } -} \ No newline at end of file +} diff --git a/src/app/errors.rs b/src/app/errors.rs index 2444e2b5..c03400da 100644 --- a/src/app/errors.rs +++ b/src/app/errors.rs @@ -158,7 +158,7 @@ pub enum ClapErrorType { /// .multiple(false)) /// .get_matches_from_safe(vec!["", "--debug", "--debug"]); /// ``` - UnexpectedMultipleUsage + UnexpectedMultipleUsage, } /// Command line argument parser error @@ -167,7 +167,7 @@ pub struct ClapError { /// Formated error message pub error: String, /// Command line argument parser error type - pub error_type: ClapErrorType + pub error_type: ClapErrorType, } impl Error for ClapError { @@ -177,7 +177,9 @@ impl Error for ClapError { } impl fmt::Display for ClapError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, + f: &mut fmt::Formatter) + -> fmt::Result { write!(f, "{}", self.error) } -} \ No newline at end of file +} diff --git a/src/app/mod.rs b/src/app/mod.rs index f71f25b7..56303696 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -5,4 +5,4 @@ mod errors; pub use self::settings::AppSettings; pub use self::app::App; -pub use self::errors::{ClapError, ClapErrorType}; \ No newline at end of file +pub use self::errors::{ClapError, ClapErrorType}; diff --git a/src/app/settings.rs b/src/app/settings.rs index a0db996e..68e9d7bc 100644 --- a/src/app/settings.rs +++ b/src/app/settings.rs @@ -157,4 +157,4 @@ impl FromStr for AppSettings { _ => Err("unknown AppSetting, cannot convert from str".to_owned()) } } -} \ No newline at end of file +} diff --git a/src/app/suggestions.rs b/src/app/suggestions.rs index d2c4e622..203e2537 100644 --- a/src/app/suggestions.rs +++ b/src/app/suggestions.rs @@ -7,15 +7,18 @@ use strsim; /// `Some("foo")`, whereas "blark" would yield `None`. #[cfg(feature = "suggestions")] #[cfg_attr(feature = "lints", allow(needless_lifetimes))] -pub fn did_you_mean<'a, T, I>(v: &str, possible_values: I) -> Option<&'a str> - where T: AsRef + 'a, - I: IntoIterator { +pub fn did_you_mean<'a, T, I>(v: &str, + possible_values: I) + -> Option<&'a str> + where T: AsRef + 'a, + I: IntoIterator +{ let mut candidate: Option<(f64, &str)> = None; for pv in possible_values.into_iter() { let confidence = strsim::jaro_winkler(v, pv.as_ref()); - if confidence > 0.8 && (candidate.is_none() || - (candidate.as_ref().unwrap().0 < confidence)) { + if confidence > 0.8 && + (candidate.is_none() || (candidate.as_ref().unwrap().0 < confidence)) { candidate = Some((confidence, pv.as_ref())); } } @@ -26,9 +29,12 @@ pub fn did_you_mean<'a, T, I>(v: &str, possible_values: I) -> Option<&'a str> } #[cfg(not(feature = "suggestions"))] -pub fn did_you_mean<'a, T, I>(_: &str, _: I) -> Option<&'a str> - where T: AsRef + 'a, - I: IntoIterator { +pub fn did_you_mean<'a, T, I>(_: &str, + _: I) + -> Option<&'a str> + where T: AsRef + 'a, + I: IntoIterator +{ None } @@ -51,4 +57,4 @@ mod test { assert!(did_you_mean("hahaahahah", p_vals.iter()).is_none()); } -} \ No newline at end of file +} diff --git a/src/args/arg.rs b/src/args/arg.rs index 178d29e7..7240d1f6 100644 --- a/src/args/arg.rs +++ b/src/args/arg.rs @@ -100,8 +100,8 @@ pub struct Arg<'n, 'l, 'h, 'g, 'p, 'r> { #[doc(hidden)] pub validator: Option Result<(), String>>>, /// A list of names for other arguments that *mutually override* this flag - #[doc(hidden)] - pub overrides: Option> + #[doc(hidden)] + pub overrides: Option>, } impl<'n, 'l, 'h, 'g, 'p, 'r> Arg<'n, 'l, 'h, 'g, 'p, 'r> { @@ -144,7 +144,7 @@ impl<'n, 'l, 'h, 'g, 'p, 'r> Arg<'n, 'l, 'h, 'g, 'p, 'r> { global: false, empty_vals: true, validator: None, - overrides: None + overrides: None, } } @@ -284,17 +284,17 @@ impl<'n, 'l, 'h, 'g, 'p, 'r> Arg<'n, 'l, 'h, 'g, 'p, 'r> { assert!(u.len() > 0, "Arg::from_usage() requires a non-zero-length usage string but none \ was provided"); - let mut name = None; - let mut short = None; - let mut long = None; - let mut help = None; - let mut required = false; - let mut takes_value = false; - let mut multiple = false; - let mut num_names = 1; - let mut name_first = false; - let mut consec_names = false; - let mut val_names = BTreeSet::new(); + let mut name = None; + let mut short = None; + let mut long = None; + let mut help = None; + let mut required = false; + let mut takes_value = false; + let mut multiple = false; + let mut num_names = 1; + let mut name_first = false; + let mut consec_names = false; + let mut val_names = BTreeSet::new(); let parser = UsageParser::with_usage(u); for_match!{ parser, @@ -365,7 +365,9 @@ impl<'n, 'l, 'h, 'g, 'p, 'r> Arg<'n, 'l, 'h, 'g, 'p, 'r> { } Arg { - name: name.unwrap_or_else(|| panic!("Missing flag name in \"{}\", check from_usage call", u)), + name: name.unwrap_or_else(|| { + panic!("Missing flag name in \"{}\", check from_usage call", u) + }), short: short, long: long, help: help, @@ -376,8 +378,16 @@ impl<'n, 'l, 'h, 'g, 'p, 'r> Arg<'n, 'l, 'h, 'g, 'p, 'r> { possible_vals: None, blacklist: None, requires: None, - num_vals: if num_names > 1 { Some(num_names) } else { None }, - val_names: if val_names.len() > 1 {Some(val_names)}else{None}, + num_vals: if num_names > 1 { + Some(num_names) + } else { + None + }, + val_names: if val_names.len() > 1 { + Some(val_names) + } else { + None + }, max_vals: None, min_vals: None, group: None, @@ -408,7 +418,9 @@ impl<'n, 'l, 'h, 'g, 'p, 'r> Arg<'n, 'l, 'h, 'g, 'p, 'r> { /// # Arg::with_name("conifg") /// .short("c") /// # ).get_matches(); - pub fn short(mut self, s: &str) -> Self { + pub fn short(mut self, + s: &str) + -> Self { self.short = s.trim_left_matches(|c| c == '-').chars().nth(0); self } @@ -432,7 +444,9 @@ impl<'n, 'l, 'h, 'g, 'p, 'r> Arg<'n, 'l, 'h, 'g, 'p, 'r> { /// # Arg::with_name("conifg") /// .long("config") /// # ).get_matches(); - pub fn long(mut self, l: &'l str) -> Self { + pub fn long(mut self, + l: &'l str) + -> Self { self.long = Some(l.trim_left_matches(|c| c == '-')); self } @@ -450,7 +464,9 @@ impl<'n, 'l, 'h, 'g, 'p, 'r> Arg<'n, 'l, 'h, 'g, 'p, 'r> { /// # Arg::with_name("conifg") /// .help("The config file used by the myprog") /// # ).get_matches(); - pub fn help(mut self, h: &'h str) -> Self { + pub fn help(mut self, + h: &'h str) + -> Self { self.help = Some(h); self } @@ -474,7 +490,9 @@ impl<'n, 'l, 'h, 'g, 'p, 'r> Arg<'n, 'l, 'h, 'g, 'p, 'r> { /// # Arg::with_name("conifg") /// .required(true) /// # ).get_matches(); - pub fn required(mut self, r: bool) -> Self { + pub fn required(mut self, + r: bool) + -> Self { self.required = r; self } @@ -494,7 +512,9 @@ impl<'n, 'l, 'h, 'g, 'p, 'r> Arg<'n, 'l, 'h, 'g, 'p, 'r> { /// # let myprog = App::new("myprog").arg(Arg::with_name("conifg") /// .conflicts_with("debug") /// # ).get_matches(); - pub fn conflicts_with(mut self, name: &'r str) -> Self { + pub fn conflicts_with(mut self, + name: &'r str) + -> Self { if let Some(ref mut vec) = self.blacklist { vec.push(name); } else { @@ -519,10 +539,12 @@ impl<'n, 'l, 'h, 'g, 'p, 'r> Arg<'n, 'l, 'h, 'g, 'p, 'r> { /// # let myprog = App::new("myprog").arg(Arg::with_name("conifg") /// .conflicts_with_all(&config_conflicts) /// # ).get_matches(); - pub fn conflicts_with_all(mut self, names: I) + pub fn conflicts_with_all(mut self, + names: I) -> Self - where T: AsRef + 'r, - I: IntoIterator { + where T: AsRef + 'r, + I: IntoIterator + { if let Some(ref mut vec) = self.blacklist { names.into_iter().map(|s| vec.push(s.as_ref())).collect::>(); } else { @@ -541,7 +563,9 @@ impl<'n, 'l, 'h, 'g, 'p, 'r> Arg<'n, 'l, 'h, 'g, 'p, 'r> { /// # let myprog = App::new("myprog").arg(Arg::with_name("conifg") /// .mutually_overrides_with("debug") /// # ).get_matches(); - pub fn mutually_overrides_with(mut self, name: &'r str) -> Self { + pub fn mutually_overrides_with(mut self, + name: &'r str) + -> Self { if let Some(ref mut vec) = self.overrides { vec.push(name); } else { @@ -561,10 +585,12 @@ impl<'n, 'l, 'h, 'g, 'p, 'r> Arg<'n, 'l, 'h, 'g, 'p, 'r> { /// # let myprog = App::new("myprog").arg(Arg::with_name("conifg") /// .mutually_overrides_with_all(&config_overrides) /// # ).get_matches(); - pub fn mutually_overrides_with_all(mut self, names: I) - -> Self - where T: AsRef + 'r, - I: IntoIterator { + pub fn mutually_overrides_with_all(mut self, + names: I) + -> Self + where T: AsRef + 'r, + I: IntoIterator + { if let Some(ref mut vec) = self.overrides { names.into_iter().map(|s| vec.push(s.as_ref())).collect::>(); } else { @@ -586,7 +612,9 @@ impl<'n, 'l, 'h, 'g, 'p, 'r> Arg<'n, 'l, 'h, 'g, 'p, 'r> { /// # let myprog = App::new("myprog").arg(Arg::with_name("conifg") /// .requires("debug") /// # ).get_matches(); - pub fn requires(mut self, name: &'r str) -> Self { + pub fn requires(mut self, + name: &'r str) + -> Self { if let Some(ref mut vec) = self.requires { vec.push(name); } else { @@ -610,10 +638,12 @@ impl<'n, 'l, 'h, 'g, 'p, 'r> Arg<'n, 'l, 'h, 'g, 'p, 'r> { /// # let myprog = App::new("myprog").arg(Arg::with_name("conifg") /// .requires_all(&config_reqs) /// # ).get_matches(); - pub fn requires_all(mut self, names: I) + pub fn requires_all(mut self, + names: I) -> Self - where T: AsRef + 'r, - I: IntoIterator { + where T: AsRef + 'r, + I: IntoIterator + { if let Some(ref mut vec) = self.requires { names.into_iter().map(|s| vec.push(s.as_ref())).collect::>(); } else { @@ -637,7 +667,9 @@ impl<'n, 'l, 'h, 'g, 'p, 'r> Arg<'n, 'l, 'h, 'g, 'p, 'r> { /// # Arg::with_name("conifg") /// .takes_value(true) /// # ).get_matches(); - pub fn takes_value(mut self, tv: bool) -> Self { + pub fn takes_value(mut self, + tv: bool) + -> Self { self.takes_value = tv; self } @@ -659,7 +691,9 @@ impl<'n, 'l, 'h, 'g, 'p, 'r> Arg<'n, 'l, 'h, 'g, 'p, 'r> { /// # Arg::with_name("conifg") /// .index(1) /// # ).get_matches(); - pub fn index(mut self, idx: u8) -> Self { + pub fn index(mut self, + idx: u8) + -> Self { self.index = Some(idx); self } @@ -682,7 +716,9 @@ impl<'n, 'l, 'h, 'g, 'p, 'r> Arg<'n, 'l, 'h, 'g, 'p, 'r> { /// # Arg::with_name("debug") /// .multiple(true) /// # ).get_matches(); - pub fn multiple(mut self, multi: bool) -> Self { + pub fn multiple(mut self, + multi: bool) + -> Self { self.multiple = multi; self } @@ -707,7 +743,9 @@ impl<'n, 'l, 'h, 'g, 'p, 'r> Arg<'n, 'l, 'h, 'g, 'p, 'r> { /// # Arg::with_name("debug") /// .global(true) /// # ).get_matches(); - pub fn global(mut self, g: bool) -> Self { + pub fn global(mut self, + g: bool) + -> Self { self.global = g; self } @@ -727,7 +765,9 @@ impl<'n, 'l, 'h, 'g, 'p, 'r> Arg<'n, 'l, 'h, 'g, 'p, 'r> { /// # Arg::with_name("debug") /// .empty_values(true) /// # ).get_matches(); - pub fn empty_values(mut self, ev: bool) -> Self { + pub fn empty_values(mut self, + ev: bool) + -> Self { self.empty_vals = ev; self } @@ -748,10 +788,12 @@ impl<'n, 'l, 'h, 'g, 'p, 'r> Arg<'n, 'l, 'h, 'g, 'p, 'r> { /// # Arg::with_name("debug").index(1) /// .possible_values(&mode_vals) /// # ).get_matches(); - pub fn possible_values(mut self, names: I) + pub fn possible_values(mut self, + names: I) -> Self - where T: AsRef + 'p, - I: IntoIterator { + where T: AsRef + 'p, + I: IntoIterator + { if let Some(ref mut vec) = self.possible_vals { names.into_iter().map(|s| vec.push(s.as_ref())).collect::>(); } else { @@ -776,7 +818,9 @@ impl<'n, 'l, 'h, 'g, 'p, 'r> Arg<'n, 'l, 'h, 'g, 'p, 'r> { /// .possible_value("fast") /// .possible_value("slow") /// # ).get_matches(); - pub fn possible_value(mut self, name: &'p str) -> Self { + pub fn possible_value(mut self, + name: &'p str) + -> Self { if let Some(ref mut vec) = self.possible_vals { vec.push(name); } else { @@ -797,7 +841,9 @@ impl<'n, 'l, 'h, 'g, 'p, 'r> Arg<'n, 'l, 'h, 'g, 'p, 'r> { /// # Arg::with_name("debug").index(1) /// .group("mode") /// # ).get_matches(); - pub fn group(mut self, name: &'g str) -> Self { + pub fn group(mut self, + name: &'g str) + -> Self { self.group = Some(name); self } @@ -820,7 +866,9 @@ impl<'n, 'l, 'h, 'g, 'p, 'r> Arg<'n, 'l, 'h, 'g, 'p, 'r> { /// # Arg::with_name("debug").index(1) /// .number_of_values(3) /// # ).get_matches(); - pub fn number_of_values(mut self, qty: u8) -> Self { + pub fn number_of_values(mut self, + qty: u8) + -> Self { self.num_vals = Some(qty); self } @@ -851,7 +899,11 @@ impl<'n, 'l, 'h, 'g, 'p, 'r> Arg<'n, 'l, 'h, 'g, 'p, 'r> { /// } /// }) /// # ).get_matches(); - pub fn validator(mut self, f: F) -> Self where F: Fn(String) -> Result<(), String> + 'static { + pub fn validator(mut self, + f: F) + -> Self + where F: Fn(String) -> Result<(), String> + 'static + { self.validator = Some(Rc::new(f)); self } @@ -874,7 +926,9 @@ impl<'n, 'l, 'h, 'g, 'p, 'r> Arg<'n, 'l, 'h, 'g, 'p, 'r> { /// # Arg::with_name("debug").index(1) /// .max_values(3) /// # ).get_matches(); - pub fn max_values(mut self, qty: u8) -> Self { + pub fn max_values(mut self, + qty: u8) + -> Self { if qty < 2 { panic!("Arguments with max_values(qty) qty must be > 1. Prefer \ takes_value(true) for arguments with only one value, or flags for arguments \ @@ -906,7 +960,9 @@ impl<'n, 'l, 'h, 'g, 'p, 'r> Arg<'n, 'l, 'h, 'g, 'p, 'r> { /// # Arg::with_name("debug").index(1) /// .min_values(2) /// # ).get_matches(); - pub fn min_values(mut self, qty: u8) -> Self { + pub fn min_values(mut self, + qty: u8) + -> Self { if qty < 1 { panic!("Arguments with min_values(qty) qty must be > 0. Prefer flags for arguments \ with 0 values."); @@ -940,10 +996,12 @@ impl<'n, 'l, 'h, 'g, 'p, 'r> Arg<'n, 'l, 'h, 'g, 'p, 'r> { /// // ... /// .value_names(&val_names) /// # ).get_matches(); - pub fn value_names(mut self, names: I) - -> Self - where T: AsRef + 'n, - I: IntoIterator { + pub fn value_names(mut self, + names: I) + -> Self + where T: AsRef + 'n, + I: IntoIterator + { if let Some(ref mut vec) = self.val_names { names.into_iter().map(|s| vec.insert(s.as_ref())).collect::>(); } else { @@ -965,7 +1023,8 @@ impl<'n, 'l, 'h, 'g, 'p, 'r> Arg<'n, 'l, 'h, 'g, 'p, 'r> { /// .index(1) /// .value_name("file") /// # ).get_matches(); - pub fn value_name(mut self, name: &'n str) + pub fn value_name(mut self, + name: &'n str) -> Self { if let Some(ref mut vec) = self.val_names { vec.insert(name); @@ -978,7 +1037,8 @@ impl<'n, 'l, 'h, 'g, 'p, 'r> Arg<'n, 'l, 'h, 'g, 'p, 'r> { } } -impl<'n, 'l, 'h, 'g, 'p, 'r, 'z> From<&'z Arg<'n, 'l, 'h, 'g, 'p, 'r>> for Arg<'n, 'l, 'h, 'g, 'p, 'r> { +impl<'n, 'l, 'h, 'g, 'p, 'r, 'z> From<&'z Arg<'n, 'l, 'h, 'g, 'p, 'r>> + for Arg<'n, 'l, 'h, 'g, 'p, 'r> { fn from(a: &'z Arg<'n, 'l, 'h, 'g, 'p, 'r>) -> Self { Arg { name: a.name, @@ -1000,7 +1060,7 @@ impl<'n, 'l, 'h, 'g, 'p, 'r, 'z> From<&'z Arg<'n, 'l, 'h, 'g, 'p, 'r>> for Arg<' global: a.global, empty_vals: a.empty_vals, validator: a.validator.clone(), - overrides: a.overrides.clone() + overrides: a.overrides.clone(), } } -} \ No newline at end of file +} diff --git a/src/args/argbuilder/flag.rs b/src/args/argbuilder/flag.rs index b77577e0..230dd25e 100644 --- a/src/args/argbuilder/flag.rs +++ b/src/args/argbuilder/flag.rs @@ -1,5 +1,5 @@ // use std::collections::HashSet; -use std::fmt::{ Display, Formatter, Result }; +use std::fmt::{Display, Formatter, Result}; pub struct FlagBuilder<'n> { pub name: &'n str, @@ -26,11 +26,13 @@ pub struct FlagBuilder<'n> { pub short: Option, pub global: bool, /// A list of names for other arguments that *mutually override* this flag - pub overrides: Option> + pub overrides: Option>, } impl<'n> Display for FlagBuilder<'n> { - fn fmt(&self, f: &mut Formatter) -> Result { + fn fmt(&self, + f: &mut Formatter) + -> Result { if let Some(l) = self.long { write!(f, "--{}", l) } else { @@ -53,7 +55,7 @@ mod test { blacklist: None, requires: None, global: false, - overrides: None + overrides: None, }; assert_eq!(&*format!("{}", f), "--flag"); @@ -67,9 +69,9 @@ mod test { blacklist: None, requires: None, global: false, - overrides: None + overrides: None, }; assert_eq!(&*format!("{}", f2), "-f"); } -} \ No newline at end of file +} diff --git a/src/args/argbuilder/option.rs b/src/args/argbuilder/option.rs index 01caf3da..916382c2 100644 --- a/src/args/argbuilder/option.rs +++ b/src/args/argbuilder/option.rs @@ -1,6 +1,6 @@ use std::rc::Rc; use std::collections::BTreeSet; -use std::fmt::{ Display, Formatter, Result }; +use std::fmt::{Display, Formatter, Result}; use std::result::Result as StdResult; pub struct OptBuilder<'n> { @@ -34,11 +34,13 @@ pub struct OptBuilder<'n> { pub global: bool, pub validator: Option StdResult<(), String>>>, /// A list of names for other arguments that *mutually override* this flag - pub overrides: Option> + pub overrides: Option>, } impl<'n> Display for OptBuilder<'n> { - fn fmt(&self, f: &mut Formatter) -> Result { + fn fmt(&self, + f: &mut Formatter) + -> Result { // Write the name such --long or -l if let Some(l) = self.long { try!(write!(f, "--{}", l)); @@ -89,7 +91,7 @@ mod test { empty_vals: true, global: false, validator: None, - overrides: None + overrides: None, }; assert_eq!(&*format!("{}", o), "--option ..."); @@ -115,9 +117,9 @@ mod test { empty_vals: true, global: false, validator: None, - overrides: None + overrides: None, }; assert_eq!(&*format!("{}", o2), "-o "); } -} \ No newline at end of file +} diff --git a/src/args/argbuilder/positional.rs b/src/args/argbuilder/positional.rs index 634258f8..049cef69 100644 --- a/src/args/argbuilder/positional.rs +++ b/src/args/argbuilder/positional.rs @@ -1,4 +1,4 @@ -use std::fmt::{ Display, Formatter, Result }; +use std::fmt::{Display, Formatter, Result}; use std::result::Result as StdResult; use std::rc::Rc; @@ -34,7 +34,9 @@ pub struct PosBuilder<'n> { } impl<'n> Display for PosBuilder<'n> { - fn fmt(&self, f: &mut Formatter) -> Result { + fn fmt(&self, + f: &mut Formatter) + -> Result { if self.required { try!(write!(f, "<{}>", self.name)); } else { @@ -68,7 +70,7 @@ mod test { empty_vals: true, global: false, validator: None, - overrides: None + overrides: None, }; assert_eq!(&*format!("{}", p), "[pos]..."); @@ -88,9 +90,9 @@ mod test { empty_vals: true, global: false, validator: None, - overrides: None + overrides: None, }; assert_eq!(&*format!("{}", p2), ""); } -} \ No newline at end of file +} diff --git a/src/args/argmatches.rs b/src/args/argmatches.rs index 911d85fb..9a62733a 100644 --- a/src/args/argmatches.rs +++ b/src/args/argmatches.rs @@ -58,7 +58,7 @@ pub struct ArgMatches<'n, 'a> { #[doc(hidden)] pub subcommand: Option>>, #[doc(hidden)] - pub usage: Option + pub usage: Option, } impl<'n, 'a> ArgMatches<'n, 'a> { @@ -91,12 +91,17 @@ impl<'n, 'a> ArgMatches<'n, 'a> { /// /// ```no_run /// # use clap::{App, Arg}; - /// # let matches = App::new("myapp").arg(Arg::with_name("output").takes_value(true)).get_matches(); + /// # let matches = App::new("myapp") + /// # .arg(Arg::with_name("output") + /// # .takes_value(true)) + /// # .get_matches(); /// if let Some(o) = matches.value_of("output") { /// println!("Value for output: {}", o); /// } /// ``` - pub fn value_of(&self, name: &str) -> Option<&str> { + pub fn value_of(&self, + name: &str) + -> Option<&str> { if let Some(ref arg) = self.args.get(name) { if let Some(ref vals) = arg.values { if let Some(ref val) = vals.values().nth(0) { @@ -115,7 +120,8 @@ impl<'n, 'a> ArgMatches<'n, 'a> { /// /// ```no_run /// # use clap::{App, Arg}; - /// # let matches = App::new("myapp").arg(Arg::with_name("output").takes_value(true)).get_matches(); + /// # let matches = App::new("myapp") + /// # .arg(Arg::with_name("output").takes_value(true)).get_matches(); /// // If the program had option "-c" that took a value and was run /// // via "myapp -o some -o other -o file" /// // values_of() would return a [&str; 3] ("some", "other", "file") @@ -125,7 +131,9 @@ impl<'n, 'a> ArgMatches<'n, 'a> { /// } /// } /// ``` - pub fn values_of(&'a self, name: &str) -> Option> { + pub fn values_of(&'a self, + name: &str) + -> Option> { if let Some(ref arg) = self.args.get(name) { if let Some(ref vals) = arg.values { return Some(vals.values().map(|s| &s[..]).collect::>()); @@ -141,16 +149,23 @@ impl<'n, 'a> ArgMatches<'n, 'a> { /// /// ```no_run /// # use clap::{App, Arg}; - /// # let matches = App::new("myapp").arg(Arg::with_name("output").takes_value(true)).get_matches(); + /// # let matches = App::new("myapp") + /// # .arg(Arg::with_name("output").takes_value(true)).get_matches(); /// if matches.is_present("output") { /// println!("The output argument was used!"); /// } /// ``` - pub fn is_present(&self, name: &str) -> bool { + pub fn is_present(&self, + name: &str) + -> bool { if let Some(ref sc) = self.subcommand { - if sc.name == name { return true; } + if sc.name == name { + return true; + } + } + if self.args.contains_key(name) { + return true; } - if self.args.contains_key(name) {return true;} false } @@ -163,14 +178,17 @@ impl<'n, 'a> ArgMatches<'n, 'a> { /// /// ```no_run /// # use clap::{App, Arg}; - /// # let matches = App::new("myapp").arg(Arg::with_name("output").takes_value(true)).get_matches(); + /// # let matches = App::new("myapp") + /// # .arg(Arg::with_name("output").takes_value(true)).get_matches(); /// if matches.occurrences_of("debug") > 1 { /// println!("Debug mode is REALLY on"); /// } else { /// println!("Debug mode kind of on"); /// } /// ``` - pub fn occurrences_of(&self, name: &str) -> u8 { + pub fn occurrences_of(&self, + name: &str) + -> u8 { if let Some(ref arg) = self.args.get(name) { return arg.occurrences; } @@ -185,14 +203,19 @@ impl<'n, 'a> ArgMatches<'n, 'a> { /// /// ```no_run /// # use clap::{App, Arg, SubCommand}; - /// # let app_matches = App::new("myapp").subcommand(SubCommand::with_name("test")).get_matches(); + /// # let app_matches = App::new("myapp") + /// # .subcommand(SubCommand::with_name("test")).get_matches(); /// if let Some(matches) = app_matches.subcommand_matches("test") { /// // Use matches as normal /// } /// ``` - pub fn subcommand_matches<'na>(&self, name: &'na str) -> Option<&ArgMatches> { + pub fn subcommand_matches<'na>(&self, + name: &'na str) + -> Option<&ArgMatches> { if let Some( ref sc) = self.subcommand { - if sc.name != name { return None; } + if sc.name != name { + return None; + } return Some(&sc.matches); } None @@ -208,7 +231,8 @@ impl<'n, 'a> ArgMatches<'n, 'a> { /// /// ```no_run /// # use clap::{App, Arg, SubCommand}; - /// # let app_matches = App::new("myapp").subcommand(SubCommand::with_name("test")).get_matches(); + /// # let app_matches = App::new("myapp") + /// # .subcommand(SubCommand::with_name("test")).get_matches(); /// match app_matches.subcommand_name() { /// Some("test") => {}, // test was used /// Some("config") => {}, // config was used @@ -230,7 +254,8 @@ impl<'n, 'a> ArgMatches<'n, 'a> { /// /// ```no_run /// # use clap::{App, Arg, SubCommand}; - /// # let app_matches = App::new("myapp").subcommand(SubCommand::with_name("test")).get_matches(); + /// # let app_matches = App::new("myapp") + /// # .subcommand(SubCommand::with_name("test")).get_matches(); /// match app_matches.subcommand() { /// ("test", Some(matches)) => {}, // test was used /// ("config", Some(matches)) => {}, // config was used @@ -251,7 +276,8 @@ impl<'n, 'a> ArgMatches<'n, 'a> { /// /// ```no_run /// # use clap::{App, Arg, SubCommand}; - /// # let app_matches = App::new("myapp").subcommand(SubCommand::with_name("test")).get_matches(); + /// # let app_matches = App::new("myapp") + /// # .subcommand(SubCommand::with_name("test")).get_matches(); /// println!("{}",app_matches.usage()); /// ``` pub fn usage(&self) -> &str { diff --git a/src/args/group.rs b/src/args/group.rs index cf98916f..ecc00ec4 100644 --- a/src/args/group.rs +++ b/src/args/group.rs @@ -51,7 +51,7 @@ pub struct ArgGroup<'n, 'ar> { #[doc(hidden)] pub requires: Option>, #[doc(hidden)] - pub conflicts: Option> + pub conflicts: Option>, } impl<'n, 'ar> ArgGroup<'n, 'ar> { @@ -96,14 +96,14 @@ impl<'n, 'ar> ArgGroup<'n, 'ar> { for (k, v) in group_settings.iter() { a = match k.as_str().unwrap() { "required" => a.required(v.as_bool().unwrap()), - "args" => { + "args" => { for ys in v.as_vec().unwrap() { if let Some(s) = ys.as_str() { a = a.add(s); } } a - }, + } "requires" => { for ys in v.as_vec().unwrap() { if let Some(s) = ys.as_str() { @@ -111,7 +111,7 @@ impl<'n, 'ar> ArgGroup<'n, 'ar> { } } a - }, + } "conflicts_with" => { for ys in v.as_vec().unwrap() { if let Some(s) = ys.as_str() { @@ -119,8 +119,9 @@ impl<'n, 'ar> ArgGroup<'n, 'ar> { } } a - }, - s => panic!("Unknown ArgGroup setting '{}' in YAML file for ArgGroup '{}'", s, name_str) + } + s => panic!("Unknown ArgGroup setting '{}' in YAML file for \ + ArgGroup '{}'", s, name_str), } } @@ -139,7 +140,9 @@ impl<'n, 'ar> ArgGroup<'n, 'ar> { /// # ArgGroup::with_name("conifg") /// .add("config") /// # ).get_matches(); - pub fn add(mut self, n: &'ar str) -> Self { + pub fn add(mut self, + n: &'ar str) + -> Self { self.args.push(n); self } @@ -156,7 +159,9 @@ impl<'n, 'ar> ArgGroup<'n, 'ar> { /// # ArgGroup::with_name("conifg") /// .add_all(&["config", "input", "output"]) /// # ).get_matches(); - pub fn add_all(mut self, ns: &[&'ar str]) -> Self { + pub fn add_all(mut self, + ns: &[&'ar str]) + -> Self { for n in ns { self = self.add(n); } @@ -178,7 +183,9 @@ impl<'n, 'ar> ArgGroup<'n, 'ar> { /// # ArgGroup::with_name("conifg") /// .required(true) /// # ).get_matches(); - pub fn required(mut self, r: bool) -> Self { + pub fn required(mut self, + r: bool) + -> Self { self.required = r; self } @@ -199,7 +206,9 @@ impl<'n, 'ar> ArgGroup<'n, 'ar> { /// # ArgGroup::with_name("conifg") /// .requires("config") /// # ).get_matches(); - pub fn requires(mut self, n: &'ar str) -> Self { + pub fn requires(mut self, + n: &'ar str) + -> Self { if let Some(ref mut reqs) = self.requires { reqs.push(n); } else { @@ -224,7 +233,9 @@ impl<'n, 'ar> ArgGroup<'n, 'ar> { /// # ArgGroup::with_name("conifg") /// .requires_all(&["config", "input"]) /// # ).get_matches(); - pub fn requires_all(mut self, ns: &[&'ar str]) -> Self { + pub fn requires_all(mut self, + ns: &[&'ar str]) + -> Self { for n in ns { self = self.requires(n); } @@ -247,7 +258,9 @@ impl<'n, 'ar> ArgGroup<'n, 'ar> { /// # ArgGroup::with_name("conifg") /// .conflicts_with("config") /// # ).get_matches(); - pub fn conflicts_with(mut self, n: &'ar str) -> Self { + pub fn conflicts_with(mut self, + n: &'ar str) + -> Self { if let Some(ref mut confs) = self.conflicts { confs.push(n); } else { @@ -272,7 +285,9 @@ impl<'n, 'ar> ArgGroup<'n, 'ar> { /// # ArgGroup::with_name("conifg") /// .conflicts_with_all(&["config", "input"]) /// # ).get_matches(); - pub fn conflicts_with_all(mut self, ns: &[&'ar str]) -> Self { + pub fn conflicts_with_all(mut self, + ns: &[&'ar str]) + -> Self { for n in ns { self = self.conflicts_with(n); } @@ -281,7 +296,9 @@ impl<'n, 'ar> ArgGroup<'n, 'ar> { } impl<'n, 'ar> Debug for ArgGroup<'n, 'ar> { - fn fmt(&self, f: &mut Formatter) -> Result { + fn fmt(&self, + f: &mut Formatter) + -> Result { write!(f, "{{ name:{:?}, args: {:?}, @@ -310,8 +327,8 @@ mod test { .requires_all(&["r2", "r3"]) .requires("r4"); - let args = vec!["a1", "a2", "a3", "a4"]; - let reqs = vec!["r1", "r2", "r3", "r4"]; + let args = vec!["a1", "a2", "a3", "a4"]; + let reqs = vec!["r1", "r2", "r3", "r4"]; let confs = vec!["c1", "c2", "c3", "c4"]; assert_eq!(g.args, args); @@ -319,4 +336,4 @@ mod test { assert_eq!(g.conflicts.unwrap(), confs); } -} \ No newline at end of file +} diff --git a/src/args/matchedarg.rs b/src/args/matchedarg.rs index d829a765..337fa459 100644 --- a/src/args/matchedarg.rs +++ b/src/args/matchedarg.rs @@ -2,10 +2,11 @@ use std::collections::BTreeMap; #[doc(hidden)] pub struct MatchedArg { - // #[doc(hidden)] + // #[doc(hidden)] // pub name: String, - #[doc(hidden)] + #[doc(hidden)] pub occurrences: u8, - #[doc(hidden)] - pub values: Option> + #[doc(hidden)] + // Consider VecMap once stablized + pub values: Option>, } diff --git a/src/args/subcommand.rs b/src/args/subcommand.rs index 36def9ed..472dd33c 100644 --- a/src/args/subcommand.rs +++ b/src/args/subcommand.rs @@ -27,7 +27,7 @@ pub struct SubCommand<'n, 'a> { #[doc(hidden)] pub name: &'n str, #[doc(hidden)] - pub matches: ArgMatches<'n, 'a> + pub matches: ArgMatches<'n, 'a>, } impl<'n, 'a> SubCommand<'n, 'a> { diff --git a/src/fmt.rs b/src/fmt.rs index d2b361fd..8befabb5 100644 --- a/src/fmt.rs +++ b/src/fmt.rs @@ -7,9 +7,9 @@ use ansi_term::ANSIString; pub enum Format { - Error(T), - Warning(T), - Good(T), + Error(T), + Warning(T), + Good(T), } #[cfg(all(feature = "color", not(target_os = "windows")))] @@ -26,7 +26,8 @@ impl> Format { #[cfg(all(feature = "color", not(target_os = "windows")))] impl> fmt::Display for Format { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, + f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", &self.format()) } } @@ -44,7 +45,8 @@ impl Format { #[cfg(any(not(feature = "color"), target_os = "windows"))] impl fmt::Display for Format { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, + f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", &self.format()) } } @@ -63,4 +65,4 @@ mod test { let warn = Format::Warning("warn"); assert_eq!(&*format!("{}", warn), &*format!("{}", Yellow.paint("warn"))); } -} \ No newline at end of file +} diff --git a/src/lib.rs b/src/lib.rs index 97dd24a8..6c24c889 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -30,4 +30,4 @@ mod usageparser; mod fmt; #[cfg(test)] -mod tests; \ No newline at end of file +mod tests; diff --git a/src/tests.rs b/src/tests.rs index 8dee6051..cbfda4a3 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -80,7 +80,8 @@ fn test_enums() { #[test] fn create_app() { - let _ = App::new("test").version("1.0").author("kevin").about("does awesome things").get_matches(); + let _ = + App::new("test").version("1.0").author("kevin").about("does awesome things").get_matches(); } #[test] @@ -908,5 +909,3 @@ fn create_multiple_subcommands() { .arg(Arg::with_name("other").long("other")) .get_matches(); } - - diff --git a/src/usageparser.rs b/src/usageparser.rs index eca5d819..7c743e4d 100644 --- a/src/usageparser.rs +++ b/src/usageparser.rs @@ -34,7 +34,7 @@ impl<'u> Iterator for UsageParser<'u> { fn next(&mut self) -> Option> { loop { match self.chars.next() { - Some(c) if c == '[' || c == '<' => { + Some(c) if c == '[' || c == '<' => { // self.s = self.e + 1; if self.e != 0 { self.e += 1; @@ -43,27 +43,33 @@ impl<'u> Iterator for UsageParser<'u> { let closing = match c { '[' => ']', '<' => '>', - _ => unreachable!() + _ => unreachable!(), }; - while let Some(c) = self.chars.next() { + while let Some(c) = self.chars.next() { self.e += 1; - if c == closing { break } + if c == closing { + break + } + } + if self.e > self.usage.len() { + return None } - if self.e > self.usage.len() { return None } let name = &self.usage[self.s..self.e]; return Some(UsageToken::Name(name, if c == '<' { Some(true) } else { None })); - }, + } Some('\'') => { self.s = self.e + 2; self.e = self.usage.len() - 1; - while let Some(_) = self.chars.next() { continue } + while let Some(_) = self.chars.next() { + continue + } return Some(UsageToken::Help(&self.usage[self.s..self.e])); - }, - Some('-') => { + } + Some('-') => { self.e += 1; match self.chars.next() { Some('-') => { @@ -75,16 +81,20 @@ impl<'u> Iterator for UsageParser<'u> { while let Some(c) = self.chars.next() { self.e += 1; - if c == ' ' || c == '=' || c == '.' { break } + if c == ' ' || c == '=' || c == '.' { + break + } + } + if self.e > self.usage.len() { + return None } - if self.e > self.usage.len() { return None } if self.e == self.usage.len() - 1 { return Some(UsageToken::Long(&self.usage[self.s..])) } return Some(UsageToken::Long(&self.usage[self.s..self.e])) - }, - Some(c) => { + } + Some(c) => { // When short is first don't increment e if self.e != 1 { self.e += 1; @@ -94,12 +104,12 @@ impl<'u> Iterator for UsageParser<'u> { return None } return Some(UsageToken::Short(c)) - }, - _ => { + } + _ => { return None } } - }, + } Some('.') => { self.e += 1; let mut mult = false; @@ -108,8 +118,10 @@ impl<'u> Iterator for UsageParser<'u> { match self.chars.next() { // longs consume one '.' so they match '.. ' whereas shorts can // match '...' - Some('.') | Some(' ') => { mult = true; }, - _ => { + Some('.') | Some(' ') => { + mult = true; + } + _ => { // if there is no help or following space all we can match is '..' if self.e == self.usage.len() - 1 { mult = true; @@ -118,18 +130,19 @@ impl<'u> Iterator for UsageParser<'u> { } } } - if mult { return Some(UsageToken::Multiple) } - }, + if mult { + return Some(UsageToken::Multiple) + } + } Some(' ') | Some('=') | Some(']') | Some('>') | Some('\t') | Some(',') => { self.e += 1; continue - }, + } None => { return None - }, - Some(c) => { - panic!("Usage parser error, unexpected \"{}\" at \"{}\", check from_usage call", c, self.usage); } + Some(c) => panic!("Usage parser error, unexpected \ + \"{}\" at \"{}\", check from_usage call", c, self.usage), } } }