mirror of
https://github.com/clap-rs/clap
synced 2024-12-13 06:12:40 +00:00
Closes #17
This commit is contained in:
parent
01e3fc6ddd
commit
19c205b72d
3 changed files with 42 additions and 19 deletions
23
src/app.rs
23
src/app.rs
|
@ -222,6 +222,7 @@ impl App {
|
||||||
blacklist: a.blacklist,
|
blacklist: a.blacklist,
|
||||||
help: a.help,
|
help: a.help,
|
||||||
requires: a.requires,
|
requires: a.requires,
|
||||||
|
occurrences: 1,
|
||||||
required: a.required,
|
required: a.required,
|
||||||
values: vec![]
|
values: vec![]
|
||||||
});
|
});
|
||||||
|
@ -456,6 +457,10 @@ impl App {
|
||||||
if arg.contains("=") {
|
if arg.contains("=") {
|
||||||
let arg_vec: Vec<&str> = arg.split("=").collect();
|
let arg_vec: Vec<&str> = arg.split("=").collect();
|
||||||
arg = arg_vec[0];
|
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());
|
arg_val = Some(arg_vec[1].to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -466,7 +471,18 @@ impl App {
|
||||||
self.report_error(format!("The argument --{} is mutually exclusive with one or more other arguments", arg),
|
self.report_error(format!("The argument --{} is mutually exclusive with one or more other arguments", arg),
|
||||||
true, true);
|
true, true);
|
||||||
}
|
}
|
||||||
|
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{
|
matches.opts.insert(k, OptArg{
|
||||||
name: v.name,
|
name: v.name,
|
||||||
short: v.short,
|
short: v.short,
|
||||||
|
@ -474,11 +490,12 @@ impl App {
|
||||||
help: v.help,
|
help: v.help,
|
||||||
required: v.required,
|
required: v.required,
|
||||||
blacklist: None,
|
blacklist: None,
|
||||||
|
occurrences: 1,
|
||||||
multiple: v.multiple,
|
multiple: v.multiple,
|
||||||
requires: None,
|
requires: None,
|
||||||
values: if arg_val.is_some() { vec![arg_val.clone().unwrap()]} else {vec![]}
|
values: if arg_val.is_some() { vec![arg_val.clone().unwrap()]} else {vec![]}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
match arg_val {
|
match arg_val {
|
||||||
None => { return Some(v.name); },
|
None => { return Some(v.name); },
|
||||||
_ => { return None; }
|
_ => { return None; }
|
||||||
|
@ -730,6 +747,7 @@ impl App {
|
||||||
if let Some(ref mut o) = matches.opts.get_mut(opt.name) {
|
if let Some(ref mut o) = matches.opts.get_mut(opt.name) {
|
||||||
done = true;
|
done = true;
|
||||||
o.values.push(arg.clone());
|
o.values.push(arg.clone());
|
||||||
|
o.occurrences += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ! done {
|
if ! done {
|
||||||
|
@ -741,6 +759,7 @@ impl App {
|
||||||
requires: None,
|
requires: None,
|
||||||
blacklist: None,
|
blacklist: None,
|
||||||
multiple: opt.multiple,
|
multiple: opt.multiple,
|
||||||
|
occurrences: 1,
|
||||||
required: opt.required,
|
required: opt.required,
|
||||||
values: vec![arg.clone()]
|
values: vec![arg.clone()]
|
||||||
});
|
});
|
||||||
|
|
|
@ -168,13 +168,12 @@ impl ArgMatches {
|
||||||
false
|
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
|
/// *NOTE:* This _*DOES NOT*_ work for positional arguments (use `.value_of()` instead).
|
||||||
/// (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.
|
|
||||||
///
|
///
|
||||||
///
|
///
|
||||||
/// # Example
|
/// # Example
|
||||||
|
@ -192,6 +191,9 @@ impl ArgMatches {
|
||||||
if let Some(ref f) = self.flags.get(name) {
|
if let Some(ref f) = self.flags.get(name) {
|
||||||
return f.occurrences;
|
return f.occurrences;
|
||||||
}
|
}
|
||||||
|
if let Some(ref o) = self.opts.get(name) {
|
||||||
|
return o.occurrences;
|
||||||
|
}
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -33,6 +33,8 @@ pub struct OptArg {
|
||||||
pub blacklist: Option<Vec<&'static str>>,
|
pub blacklist: Option<Vec<&'static str>>,
|
||||||
/// Allow multiple occurrences of an option argument such as "-c some -c other"
|
/// Allow multiple occurrences of an option argument such as "-c some -c other"
|
||||||
pub multiple: bool,
|
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
|
/// The value provided to the argument by the user
|
||||||
pub values: Vec<String>
|
pub values: Vec<String>
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue