MCPcopy Index your code
hub / github.com/python-cmd2/cmd2 / _handle_last_token

Method _handle_last_token

cmd2/argparse_completer.py:460–527  ·  view source on GitHub ↗

Perform final completion step handling positionals and flags.

(
        self,
        text: str,
        line: str,
        begidx: int,
        endidx: int,
        flag_arg_state: _ArgumentState | None,
        pos_arg_state: _ArgumentState | None,
        remaining_positionals: deque[argparse.Action],
        consumed_arg_values: dict[str, list[str]],
        used_flags: set[str],
        skip_remaining_flags: bool,
        cmd_set: CommandSet[Any] | None,
    )

Source from the content-addressed store, hash-verified

458 break
459
460 def _handle_last_token(
461 self,
462 text: str,
463 line: str,
464 begidx: int,
465 endidx: int,
466 flag_arg_state: _ArgumentState | None,
467 pos_arg_state: _ArgumentState | None,
468 remaining_positionals: deque[argparse.Action],
469 consumed_arg_values: dict[str, list[str]],
470 used_flags: set[str],
471 skip_remaining_flags: bool,
472 cmd_set: CommandSet[Any] | None,
473 ) -> Completions:
474 """Perform final completion step handling positionals and flags."""
475 # Check if we are completing a flag name. This check ignores strings with a length of one, like '-'.
476 # This is because that could be the start of a negative number which may be a valid completion for
477 # the current argument. We will handle the completion of flags that start with only one prefix
478 # character (-f) at the end.
479 if _looks_like_flag(text, self._parser) and not skip_remaining_flags:
480 if (
481 flag_arg_state is not None
482 and isinstance(flag_arg_state.min, int)
483 and flag_arg_state.count < flag_arg_state.min
484 ):
485 raise _UnfinishedFlagError(flag_arg_state)
486 return self._complete_flags(text, line, begidx, endidx, used_flags)
487
488 # Check if we are completing a flag's argument
489 if flag_arg_state is not None:
490 completions = self._complete_arg(text, line, begidx, endidx, flag_arg_state, consumed_arg_values, cmd_set=cmd_set)
491
492 # If we have results, then return them
493 if completions:
494 return completions
495
496 # Otherwise, print a hint if the flag isn't finished or text isn't possibly the start of a flag
497 if (
498 (isinstance(flag_arg_state.min, int) and flag_arg_state.count < flag_arg_state.min)
499 or not _single_prefix_char(text, self._parser)
500 or skip_remaining_flags
501 ):
502 raise _NoResultsError(self._parser, flag_arg_state.action)
503
504 # Otherwise check if we have a positional to complete
505 elif pos_arg_state is not None or remaining_positionals:
506 # If we aren't current tracking a positional, then get the next positional arg to handle this token
507 if pos_arg_state is None:
508 action = remaining_positionals.popleft()
509 pos_arg_state = _ArgumentState(action)
510
511 completions = self._complete_arg(text, line, begidx, endidx, pos_arg_state, consumed_arg_values, cmd_set=cmd_set)
512
513 # If we have results, then return them
514 if completions:
515 return completions
516
517 # Otherwise, print a hint if text isn't possibly the start of a flag

Callers 1

completeMethod · 0.95

Calls 8

_complete_flagsMethod · 0.95
_complete_argMethod · 0.95
_looks_like_flagFunction · 0.85
_single_prefix_charFunction · 0.85
_NoResultsErrorClass · 0.85
_ArgumentStateClass · 0.85
CompletionsClass · 0.85

Tested by

no test coverage detected