Complete text using argparse metadata. :param text: the string prefix we are attempting to match (all matches must begin with it) :param line: the current input line with leading whitespace removed :param begidx: the beginning index of the prefix text :param endidx:
(
self,
text: str,
line: str,
begidx: int,
endidx: int,
tokens: Sequence[str],
*,
cmd_set: CommandSet[Any] | None = None,
)
| 207 | self._subcommand_action = action |
| 208 | |
| 209 | def complete( |
| 210 | self, |
| 211 | text: str, |
| 212 | line: str, |
| 213 | begidx: int, |
| 214 | endidx: int, |
| 215 | tokens: Sequence[str], |
| 216 | *, |
| 217 | cmd_set: CommandSet[Any] | None = None, |
| 218 | ) -> Completions: |
| 219 | """Complete text using argparse metadata. |
| 220 | |
| 221 | :param text: the string prefix we are attempting to match (all matches must begin with it) |
| 222 | :param line: the current input line with leading whitespace removed |
| 223 | :param begidx: the beginning index of the prefix text |
| 224 | :param endidx: the ending index of the prefix text |
| 225 | :param tokens: Sequence of argument tokens being passed to the parser |
| 226 | :param cmd_set: if completing a command, the CommandSet the command's function belongs to, if applicable. |
| 227 | Defaults to None. |
| 228 | :return: a Completions object |
| 229 | :raises CompletionError: for various types of completion errors |
| 230 | """ |
| 231 | if not tokens: |
| 232 | return Completions() |
| 233 | |
| 234 | # Positionals args that are left to parse |
| 235 | remaining_positionals = deque(self._positional_actions) |
| 236 | |
| 237 | # This gets set to True when flags will no longer be processed as argparse flags |
| 238 | # That can happen when -- is used or an argument with nargs=argparse.REMAINDER is used |
| 239 | skip_remaining_flags = False |
| 240 | |
| 241 | # _ArgumentState of the current positional |
| 242 | pos_arg_state: _ArgumentState | None = None |
| 243 | |
| 244 | # _ArgumentState of the current flag |
| 245 | flag_arg_state: _ArgumentState | None = None |
| 246 | |
| 247 | # Non-reusable flags that we've parsed |
| 248 | used_flags: set[str] = set() |
| 249 | |
| 250 | # Keeps track of arguments we've seen and any tokens they consumed |
| 251 | consumed_arg_values: dict[str, list[str]] = {} |
| 252 | |
| 253 | # Completed mutually exclusive groups |
| 254 | completed_mutex_groups: dict[argparse._MutuallyExclusiveGroup, argparse.Action] = {} |
| 255 | |
| 256 | def consume_argument(arg_state: _ArgumentState, arg_token: str) -> None: |
| 257 | """Consume token as an argument.""" |
| 258 | arg_state.count += 1 |
| 259 | consumed_arg_values.setdefault(arg_state.action.dest, []).append(arg_token) |
| 260 | |
| 261 | ############################################################################################# |
| 262 | # Parse all but the last token |
| 263 | ############################################################################################# |
| 264 | for token_index, token in enumerate(tokens[:-1]): |
| 265 | # If we're in a positional REMAINDER arg, force all future tokens to go to that |
| 266 | if pos_arg_state is not None and pos_arg_state.is_remainder: |