diff --git a/src/app.rs b/src/app.rs index 0073925a..7ab4a3fe 100644 --- a/src/app.rs +++ b/src/app.rs @@ -222,6 +222,7 @@ impl App { blacklist: a.blacklist, help: a.help, requires: a.requires, + occurrences: 1, required: a.required, values: vec![] }); @@ -456,6 +457,10 @@ impl App { if arg.contains("=") { let arg_vec: Vec<&str> = arg.split("=").collect(); arg = arg_vec[0]; + // prevents "--config= value" typo + if arg_vec[1].len() == 0 { + self.report_error(format!("Argument --{} requires a value, but none was supplied", arg), true, true); + } arg_val = Some(arg_vec[1].to_string()); } @@ -466,19 +471,31 @@ impl App { self.report_error(format!("The argument --{} is mutually exclusive with one or more other arguments", arg), true, true); } - - matches.opts.insert(k, OptArg{ - name: v.name, - short: v.short, - long: v.long, - help: v.help, - required: v.required, - blacklist: None, - multiple: v.multiple, - requires: None, - values: if arg_val.is_some() { vec![arg_val.clone().unwrap()]} else {vec![]} - }); - + let mut done = false; + if v.multiple { + if let Some(ref mut o) = matches.opts.get_mut(v.name) { + if arg_val.is_some() { + o.occurrences += 1; + o.values.push(arg_val.clone().unwrap()); + return None; + } + done = true; + } + } + if ! done { + matches.opts.insert(k, OptArg{ + name: v.name, + short: v.short, + long: v.long, + help: v.help, + required: v.required, + blacklist: None, + occurrences: 1, + multiple: v.multiple, + requires: None, + values: if arg_val.is_some() { vec![arg_val.clone().unwrap()]} else {vec![]} + }); + } match arg_val { None => { return Some(v.name); }, _ => { return None; } @@ -730,6 +747,7 @@ impl App { if let Some(ref mut o) = matches.opts.get_mut(opt.name) { done = true; o.values.push(arg.clone()); + o.occurrences += 1; } } if ! done { @@ -741,6 +759,7 @@ impl App { requires: None, blacklist: None, multiple: opt.multiple, + occurrences: 1, required: opt.required, values: vec![arg.clone()] }); diff --git a/src/args/argmatches.rs b/src/args/argmatches.rs index d7b2591b..27549279 100644 --- a/src/args/argmatches.rs +++ b/src/args/argmatches.rs @@ -168,13 +168,12 @@ impl ArgMatches { false } - /// Checks the number of occurrences of a flag at runtime. + /// Checks the number of occurrences of an option or flag at runtime. + /// If an option or flag isn't present it will return `0`, if the option or flag doesn't + /// allow multiple occurrences, it will return `1` no matter how many times it occurred + /// (unless it wasn't prsent) at all. /// - /// This **DOES NOT** work for option or positional arguments - /// (use `.value_of()` instead). If a flag isn't present it will - /// return `0`, if a flag doesn't allow multiple occurrences, it will - /// return `1` no matter how many times it occurred (unless it wasn't prsent) - /// at all. + /// *NOTE:* This _*DOES NOT*_ work for positional arguments (use `.value_of()` instead). /// /// /// # Example @@ -192,6 +191,9 @@ impl ArgMatches { if let Some(ref f) = self.flags.get(name) { return f.occurrences; } + if let Some(ref o) = self.opts.get(name) { + return o.occurrences; + } 0 } diff --git a/src/args/optarg.rs b/src/args/optarg.rs index 50a6eea3..65bbab64 100644 --- a/src/args/optarg.rs +++ b/src/args/optarg.rs @@ -33,6 +33,8 @@ pub struct OptArg { pub blacklist: Option>, /// Allow multiple occurrences of an option argument such as "-c some -c other" pub multiple: bool, + /// How many occurences of this option have been found when parsing + pub occurrences: u8, /// The value provided to the argument by the user pub values: Vec }