Create an ArgparseCompleter. :param parser: Cmd2ArgumentParser instance :param cmd2_app: reference to the Cmd2 application that owns this ArgparseCompleter :param parent_tokens: optional Mapping of parent parsers' arg names to their tokens This
(
self,
parser: Cmd2ArgumentParser,
cmd2_app: "Cmd",
*,
parent_tokens: Mapping[str, MutableSequence[str]] | None = None,
)
| 156 | """Automatic command line completion based on argparse parameters.""" |
| 157 | |
| 158 | def __init__( |
| 159 | self, |
| 160 | parser: Cmd2ArgumentParser, |
| 161 | cmd2_app: "Cmd", |
| 162 | *, |
| 163 | parent_tokens: Mapping[str, MutableSequence[str]] | None = None, |
| 164 | ) -> None: |
| 165 | """Create an ArgparseCompleter. |
| 166 | |
| 167 | :param parser: Cmd2ArgumentParser instance |
| 168 | :param cmd2_app: reference to the Cmd2 application that owns this ArgparseCompleter |
| 169 | :param parent_tokens: optional Mapping of parent parsers' arg names to their tokens |
| 170 | This is only used by ArgparseCompleter when recursing on subcommand parsers |
| 171 | Defaults to None |
| 172 | """ |
| 173 | self._parser = parser |
| 174 | self._cmd2_app = cmd2_app |
| 175 | |
| 176 | if parent_tokens is None: |
| 177 | parent_tokens = {} |
| 178 | self._parent_tokens = parent_tokens |
| 179 | |
| 180 | # All flags in this command |
| 181 | self._flags: list[str] = [] |
| 182 | |
| 183 | # Maps flags to the argparse action object |
| 184 | self._flag_to_action: dict[str, argparse.Action] = {} |
| 185 | |
| 186 | # Actions for positional arguments (by position index) |
| 187 | self._positional_actions: list[argparse.Action] = [] |
| 188 | |
| 189 | # This will be set if self._parser has subcommands |
| 190 | self._subcommand_action: argparse._SubParsersAction[Cmd2ArgumentParser] | None = None |
| 191 | |
| 192 | # Start digging through the argparse structures. |
| 193 | # _actions is the top level container of parameter definitions |
| 194 | for action in self._parser._actions: |
| 195 | # if the parameter is flag based, it will have option_strings |
| 196 | if action.option_strings: |
| 197 | # record each option flag |
| 198 | for option in action.option_strings: |
| 199 | self._flags.append(option) |
| 200 | self._flag_to_action[option] = action |
| 201 | |
| 202 | # Otherwise this is a positional parameter |
| 203 | else: |
| 204 | self._positional_actions.append(action) |
| 205 | # Check if this action defines subcommands |
| 206 | if isinstance(action, argparse._SubParsersAction): |
| 207 | self._subcommand_action = action |
| 208 | |
| 209 | def complete( |
| 210 | self, |