Skip to content

Commit 076d0e0

Browse files
committed
Changed do_help() so that argparsers print their own help.
1 parent 3ee5e93 commit 076d0e0

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

cmd2/argparse_completer.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
deque,
1111
)
1212
from typing import (
13+
IO,
1314
TYPE_CHECKING,
1415
Optional,
1516
Union,
@@ -624,24 +625,28 @@ def complete_subcommand_help(self, text: str, line: str, begidx: int, endidx: in
624625
break
625626
return []
626627

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.
629630
630631
: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.
632634
"""
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.
634636
# 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+
)
640642

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)
645650

646651
def _complete_arg(
647652
self,

cmd2/cmd2.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3876,9 +3876,7 @@ def do_help(self, args: argparse.Namespace) -> None:
38763876
# If the command function uses argparse, then use argparse's help
38773877
if func is not None and argparser is not None:
38783878
completer = argparse_completer.DEFAULT_AP_COMPLETER(argparser, self)
3879-
3880-
# Set end to blank so the help output matches how it looks when "command -h" is used
3881-
self.poutput(completer.format_help(args.subcommands), end='')
3879+
completer.print_help(args.subcommands, self.stdout)
38823880

38833881
# If there is a help func delegate to do_help
38843882
elif help_func is not None:

0 commit comments

Comments
 (0)