mirror of
https://github.com/clap-rs/clap
synced 2025-03-04 23:37:32 +00:00
feat: supports -aValue style options
This commit is contained in:
parent
d03e3e0d54
commit
0e3733e4fe
1 changed files with 106 additions and 126 deletions
|
@ -3167,8 +3167,6 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
|
|||
full_arg: &str)
|
||||
-> Result<Option<&'ar str>, ClapError> {
|
||||
let arg = &full_arg[..].trim_left_matches(|c| c == '-');
|
||||
if arg.len() > 1 {
|
||||
// Multiple flags using short i.e. -bgHlS
|
||||
for c in arg.chars() {
|
||||
if let Err(e) = self.check_for_help_and_version(c) {
|
||||
return Err(ClapError{
|
||||
|
@ -3178,48 +3176,13 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
|
|||
error_type: ClapErrorType::MissingSubcommand
|
||||
});
|
||||
}
|
||||
match self.parse_single_short_flag(matches, c) {
|
||||
Ok(b) => {
|
||||
if !b {
|
||||
return Err(self.report_error(format!("The argument '{}' isn't valid",
|
||||
Format::Warning(format!("-{}", c))),
|
||||
ClapErrorType::InvalidArgument,
|
||||
Some(matches.args.keys().map(|k| *k).collect())));
|
||||
}
|
||||
}
|
||||
Err(e) => return Err(e),
|
||||
}
|
||||
}
|
||||
return Ok(None);
|
||||
}
|
||||
// Short flag or opt
|
||||
let arg_c = arg.chars().nth(0).unwrap();
|
||||
|
||||
// Ensure the arg in question isn't a help or version flag
|
||||
if let Err(e) = self.check_for_help_and_version(arg_c) {
|
||||
return Err(ClapError{
|
||||
error: format!("{} {}\n\terror message: {}\n",
|
||||
Format::Error("error:"),
|
||||
INTERNAL_ERROR_MSG, e.description()),
|
||||
error_type: ClapErrorType::MissingSubcommand
|
||||
});
|
||||
}
|
||||
|
||||
// Check for a matching flag, and return none if found
|
||||
match self.parse_single_short_flag(matches, arg_c) {
|
||||
Ok(b) => {
|
||||
if b {
|
||||
return Ok(None);
|
||||
}
|
||||
}
|
||||
Err(e) => return Err(e),
|
||||
}
|
||||
|
||||
// Check for matching short in options, and return the name
|
||||
// (only ones with shorts, of course)
|
||||
if let Some(v) = self.opts.values()
|
||||
.filter(|&v| v.short.is_some())
|
||||
.filter(|&v| v.short.unwrap() == arg_c).nth(0) {
|
||||
.filter(|&v| v.short == Some(c)).nth(0) {
|
||||
let mut ret = Some(v.name);
|
||||
// Ensure this option isn't on the master mutually excludes list
|
||||
if self.blacklist.contains(&v.name) {
|
||||
matches.args.remove(v.name);
|
||||
|
@ -3255,18 +3218,27 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
|
|||
Some(matches.args.keys().map(|k| *k).collect())));
|
||||
}
|
||||
} else {
|
||||
let val: Vec<&str> = arg.splitn(2, c).collect();
|
||||
let bm = if val[1].is_empty() {
|
||||
BTreeMap::new()
|
||||
} else {
|
||||
ret = None;
|
||||
let mut bm = BTreeMap::new();
|
||||
bm.insert(0, val[1].to_owned());
|
||||
bm
|
||||
};
|
||||
if let Some(ref vec) = self.groups_for(v.name) {
|
||||
for grp in vec {
|
||||
matches.args.insert(grp, MatchedArg{
|
||||
occurrences: 1,
|
||||
values: Some(BTreeMap::new()),
|
||||
values: Some(bm.clone()),
|
||||
});
|
||||
}
|
||||
}
|
||||
matches.args.insert(v.name, MatchedArg{
|
||||
// occurrences will be incremented on getting a value
|
||||
occurrences: 0,
|
||||
values: Some(BTreeMap::new())
|
||||
values: Some(bm)
|
||||
});
|
||||
}
|
||||
if let Some(ref bl) = v.blacklist {
|
||||
|
@ -3291,14 +3263,22 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
|
|||
|
||||
parse_group_reqs!(self, v);
|
||||
|
||||
return Ok(Some(v.name))
|
||||
return Ok(ret);
|
||||
}
|
||||
|
||||
// Didn't match a flag or option, must be invalid
|
||||
Err(self.report_error(format!("The argument '{}' isn't valid",
|
||||
Format::Warning(format!("-{}", arg_c))),
|
||||
match self.parse_single_short_flag(matches, c) {
|
||||
Ok(b) => {
|
||||
if !b {
|
||||
return Err(self.report_error(format!("The argument '{}' isn't valid",
|
||||
Format::Warning(format!("-{}", c))),
|
||||
ClapErrorType::InvalidArgument,
|
||||
Some(matches.args.keys().map(|k| *k).collect())))
|
||||
Some(matches.args.keys().map(|k| *k).collect())));
|
||||
}
|
||||
}
|
||||
Err(e) => return Err(e),
|
||||
}
|
||||
}
|
||||
unreachable!();
|
||||
}
|
||||
|
||||
fn parse_single_short_flag(&mut self,
|
||||
|
|
Loading…
Add table
Reference in a new issue