|
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,28 @@ 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 |
| 633 | + If not supplied, argparse will write to sys.stdout. |
632 | 634 | """
|
633 |
| - # If our parser has subcommands, we must examine the tokens and check if they are subcommands |
| 635 | + # If our parser has subcommands, we must examine the tokens and check if they are subcommands. |
634 | 636 | # 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) |
| 637 | + if tokens and self._subcommand_action is not None: |
| 638 | + parser = cast( |
| 639 | + Optional[argparse.ArgumentParser], |
| 640 | + self._subcommand_action.choices.get(tokens[0]), |
| 641 | + ) |
640 | 642 |
|
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() |
| 643 | + if parser: |
| 644 | + completer_type = self._cmd2_app._determine_ap_completer_type(parser) |
| 645 | + completer = completer_type(parser, self._cmd2_app) |
| 646 | + completer.print_help(tokens[1:]) |
| 647 | + return |
| 648 | + |
| 649 | + self._parser.print_help(file=file) |
645 | 650 |
|
646 | 651 | def _complete_arg(
|
647 | 652 | self,
|
|
0 commit comments