docs: add more explanation to #2501 fix (#2507)

* 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:
rami3l 2021-05-28 22:51:00 +02:00 committed by GitHub
parent fbd338ce26
commit 65b3892ef6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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 {