|
10 | 10 | deque, |
11 | 11 | ) |
12 | 12 | from typing import ( |
| 13 | + IO, |
13 | 14 | TYPE_CHECKING, |
14 | 15 | Optional, |
15 | 16 | Union, |
@@ -624,24 +625,27 @@ def complete_subcommand_help(self, text: str, line: str, begidx: int, endidx: in |
624 | 625 | break |
625 | 626 | return [] |
626 | 627 |
|
627 | | - def format_help(self, tokens: list[str]) -> str: |
628 | | - """Supports cmd2's help command in the retrieval of help text. |
| 628 | + def print_help(self, tokens: list[str], file: Optional[IO[str]] = None) -> None: |
| 629 | + """Supports cmd2's help command in the printing of help text. |
629 | 630 |
|
630 | 631 | :param tokens: arguments passed to help command |
631 | | - :return: help text of the command being queried. |
| 632 | + :param file: optional file object where the argparse should write help text |
632 | 633 | """ |
633 | | - # If our parser has subcommands, we must examine the tokens and check if they are subcommands |
| 634 | + # If our parser has subcommands, we must examine the tokens and check if they are subcommands. |
634 | 635 | # If so, we will let the subcommand's parser handle the rest of the tokens via another ArgparseCompleter. |
635 | | - if self._subcommand_action is not None: |
636 | | - for token_index, token in enumerate(tokens): |
637 | | - if token in self._subcommand_action.choices: |
638 | | - parser: argparse.ArgumentParser = self._subcommand_action.choices[token] |
639 | | - completer_type = self._cmd2_app._determine_ap_completer_type(parser) |
| 636 | + if tokens and self._subcommand_action is not None: |
| 637 | + parser = cast( |
| 638 | + Optional[argparse.ArgumentParser], |
| 639 | + self._subcommand_action.choices.get(tokens[0]), |
| 640 | + ) |
640 | 641 |
|
641 | | - completer = completer_type(parser, self._cmd2_app) |
642 | | - return completer.format_help(tokens[token_index + 1 :]) |
643 | | - break |
644 | | - return self._parser.format_help() |
| 642 | + if parser: |
| 643 | + completer_type = self._cmd2_app._determine_ap_completer_type(parser) |
| 644 | + completer = completer_type(parser, self._cmd2_app) |
| 645 | + completer.print_help(tokens[1:]) |
| 646 | + return |
| 647 | + |
| 648 | + self._parser.print_help(file=file) |
645 | 649 |
|
646 | 650 | def _complete_arg( |
647 | 651 | self, |
|
0 commit comments