mirror of
https://github.com/clap-rs/clap
synced 2025-03-04 23:37:32 +00:00
* fix(parser): replace `unwrap` with `expect` * docs(parser): add more comments explaining `done_short_args` * docs(parser): add more comments explaining `keep_state`
This commit is contained in:
parent
fbd338ce26
commit
65b3892ef6
1 changed files with 21 additions and 13 deletions
|
@ -416,8 +416,15 @@ impl<'help, 'app> Parser<'help, 'app> {
|
|||
keep_state = !done;
|
||||
if keep_state {
|
||||
it.cursor -= 1;
|
||||
self.flag_subcmd_skip =
|
||||
self.cur_idx.get() - self.flag_subcmd_at.unwrap() + 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
|
||||
// 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_at.expect(INTERNAL_ERROR_MSG)
|
||||
+ 1;
|
||||
}
|
||||
|
||||
debug!(
|
||||
|
@ -536,7 +543,7 @@ impl<'help, 'app> Parser<'help, 'app> {
|
|||
let append = self.arg_have_val(matcher, p);
|
||||
self.add_val_to_arg(p, arg_os, matcher, ValueType::CommandLine, append);
|
||||
|
||||
// Increase occurence no matter if we are appending, Occurences
|
||||
// Increase occurrence no matter if we are appending, occurrences
|
||||
// of positional argument equals to number of values rather than
|
||||
// the number of value groups.
|
||||
self.inc_occurrence_of_arg(matcher, p);
|
||||
|
@ -1121,7 +1128,7 @@ impl<'help, 'app> Parser<'help, 'app> {
|
|||
}
|
||||
|
||||
// Check for trailing concatenated value
|
||||
let i = arg_os.split(c).next().unwrap().len() + c.len_utf8();
|
||||
let i = arg_os.split(c).next().expect(INTERNAL_ERROR_MSG).len() + c.len_utf8();
|
||||
debug!(
|
||||
"Parser::parse_short_arg:iter:{}: i={}, arg_os={:?}",
|
||||
c, i, arg_os
|
||||
|
@ -1145,18 +1152,19 @@ impl<'help, 'app> Parser<'help, 'app> {
|
|||
} else if let Some(sc_name) = self.app.find_short_subcmd(c) {
|
||||
debug!("Parser::parse_short_arg:iter:{}: subcommand={}", c, sc_name);
|
||||
let name = sc_name.to_string();
|
||||
let cur_idx = self.cur_idx.get();
|
||||
let done_short_args = if let Some(at) = self.flag_subcmd_at {
|
||||
let done_short_args = {
|
||||
let cur_idx = self.cur_idx.get();
|
||||
// Get the index of the previously saved flag subcommand in the group of flags (if exists).
|
||||
// If it is a new flag subcommand, then the formentioned index should be the current one
|
||||
// (ie. `cur_idx`), and should be registered.
|
||||
let at = *self.flag_subcmd_at.get_or_insert(cur_idx);
|
||||
// If we are done, then the difference of indices (cur_idx - at) should be (end - at) which
|
||||
// should equal to (arg.len() - 1),
|
||||
// where `end` is the index of the end of the group.
|
||||
cur_idx - at == arg.len() - 1
|
||||
} else {
|
||||
// This is a new flag subcommand and should be registered.
|
||||
self.flag_subcmd_at = Some(cur_idx);
|
||||
// If we have only a letter in `arg`, eg. '-S', then we are done.
|
||||
// Otherwise, we are not.
|
||||
arg.len() == 1
|
||||
};
|
||||
if done_short_args {
|
||||
self.flag_subcmd_at.take();
|
||||
self.flag_subcmd_at = None;
|
||||
}
|
||||
return Ok(ParseResult::FlagSubCommandShort(name, done_short_args));
|
||||
} else {
|
||||
|
|
Loading…
Add table
Reference in a new issue