mirror of
https://github.com/clap-rs/clap
synced 2024-12-12 22:02:35 +00:00
Closes #17
This commit is contained in:
parent
01e3fc6ddd
commit
19c205b72d
3 changed files with 42 additions and 19 deletions
45
src/app.rs
45
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,19 +471,31 @@ 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;
|
||||||
matches.opts.insert(k, OptArg{
|
if v.multiple {
|
||||||
name: v.name,
|
if let Some(ref mut o) = matches.opts.get_mut(v.name) {
|
||||||
short: v.short,
|
if arg_val.is_some() {
|
||||||
long: v.long,
|
o.occurrences += 1;
|
||||||
help: v.help,
|
o.values.push(arg_val.clone().unwrap());
|
||||||
required: v.required,
|
return None;
|
||||||
blacklist: None,
|
}
|
||||||
multiple: v.multiple,
|
done = true;
|
||||||
requires: None,
|
}
|
||||||
values: if arg_val.is_some() { vec![arg_val.clone().unwrap()]} else {vec![]}
|
}
|
||||||
});
|
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 {
|
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