mirror of
https://github.com/clap-rs/clap
synced 2024-12-14 14:52:33 +00:00
refactor(parser): encode all parsing state of FlagSubCommand
in Parser
This commit is contained in:
parent
65b3892ef6
commit
a91ca7fc1b
1 changed files with 15 additions and 19 deletions
|
@ -24,8 +24,7 @@ use crate::{
|
||||||
pub(crate) enum ParseResult {
|
pub(crate) enum ParseResult {
|
||||||
Flag,
|
Flag,
|
||||||
FlagSubCommand(String),
|
FlagSubCommand(String),
|
||||||
// subcommand name, whether there are more shorts args remaining
|
FlagSubCommandShort(String),
|
||||||
FlagSubCommandShort(String, bool),
|
|
||||||
Opt(Id),
|
Opt(Id),
|
||||||
Pos(Id),
|
Pos(Id),
|
||||||
MaybeHyphenValue,
|
MaybeHyphenValue,
|
||||||
|
@ -388,7 +387,7 @@ impl<'help, 'app> Parser<'help, 'app> {
|
||||||
subcmd_name = Some(name.to_owned());
|
subcmd_name = Some(name.to_owned());
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
ParseResult::FlagSubCommandShort(_, _) => unreachable!(),
|
ParseResult::FlagSubCommandShort(_) => unreachable!(),
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
} else if arg_os.starts_with("-")
|
} else if arg_os.starts_with("-")
|
||||||
|
@ -411,21 +410,18 @@ impl<'help, 'app> Parser<'help, 'app> {
|
||||||
ParseResult::Opt(..) | ParseResult::Flag | ParseResult::ValuesDone => {
|
ParseResult::Opt(..) | ParseResult::Flag | ParseResult::ValuesDone => {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
ParseResult::FlagSubCommandShort(ref name, done) => {
|
ParseResult::FlagSubCommandShort(ref name) => {
|
||||||
// There are more short args, revisit the current short args skipping the subcommand
|
// If there are more short flags to be processed, we should keep the state, and later
|
||||||
keep_state = !done;
|
// revisit the current group of short flags skipping the subcommand.
|
||||||
if keep_state {
|
keep_state = self
|
||||||
|
.flag_subcmd_at
|
||||||
|
.map(|at| {
|
||||||
it.cursor -= 1;
|
it.cursor -= 1;
|
||||||
// Here `self.flag_subcmd_at` can be safely unwrapped, because the only place to get a true `keep_state`
|
|
||||||
// (ie. a false `done`) is in `Parser::parse_short_arg`, during which `self.flag_subcmd_at` should have
|
|
||||||
// been set anyway.
|
|
||||||
// If not, it's definitely an internal error!
|
|
||||||
// Since we are now saving the current state, the number of flags to skip during state recovery should
|
// Since we are now saving the current state, the number of flags to skip during state recovery should
|
||||||
// be the current index (`cur_idx`) minus ONE UNIT TO THE LEFT of the starting position.
|
// be the current index (`cur_idx`) minus ONE UNIT TO THE LEFT of the starting position.
|
||||||
self.flag_subcmd_skip = self.cur_idx.get()
|
self.flag_subcmd_skip = self.cur_idx.get() - at + 1;
|
||||||
- self.flag_subcmd_at.expect(INTERNAL_ERROR_MSG)
|
})
|
||||||
+ 1;
|
.is_some();
|
||||||
}
|
|
||||||
|
|
||||||
debug!(
|
debug!(
|
||||||
"Parser::get_matches_with:FlagSubCommandShort: subcmd_name={}, keep_state={}, flag_subcmd_skip={}",
|
"Parser::get_matches_with:FlagSubCommandShort: subcmd_name={}, keep_state={}, flag_subcmd_skip={}",
|
||||||
|
@ -1166,7 +1162,7 @@ impl<'help, 'app> Parser<'help, 'app> {
|
||||||
if done_short_args {
|
if done_short_args {
|
||||||
self.flag_subcmd_at = None;
|
self.flag_subcmd_at = None;
|
||||||
}
|
}
|
||||||
return Ok(ParseResult::FlagSubCommandShort(name, done_short_args));
|
return Ok(ParseResult::FlagSubCommandShort(name));
|
||||||
} else {
|
} else {
|
||||||
let arg = format!("-{}", c);
|
let arg = format!("-{}", c);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue