Skip to content

Changed do_help() so that argparsers print their own help. #1467

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions cmd2/argparse_completer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
deque,
)
from typing import (
IO,
TYPE_CHECKING,
Optional,
Union,
Expand Down Expand Up @@ -624,24 +625,28 @@ def complete_subcommand_help(self, text: str, line: str, begidx: int, endidx: in
break
return []

def format_help(self, tokens: list[str]) -> str:
"""Supports cmd2's help command in the retrieval of help text.
def print_help(self, tokens: list[str], file: Optional[IO[str]] = None) -> None:
"""Supports cmd2's help command in the printing of help text.

:param tokens: arguments passed to help command
:return: help text of the command being queried.
:param file: optional file object where the argparse should write help text
If not supplied, argparse will write to sys.stdout.
"""
# If our parser has subcommands, we must examine the tokens and check if they are subcommands
# If our parser has subcommands, we must examine the tokens and check if they are subcommands.
# If so, we will let the subcommand's parser handle the rest of the tokens via another ArgparseCompleter.
if self._subcommand_action is not None:
for token_index, token in enumerate(tokens):
if token in self._subcommand_action.choices:
parser: argparse.ArgumentParser = self._subcommand_action.choices[token]
completer_type = self._cmd2_app._determine_ap_completer_type(parser)
if tokens and self._subcommand_action is not None:
parser = cast(
Optional[argparse.ArgumentParser],
self._subcommand_action.choices.get(tokens[0]),
)

completer = completer_type(parser, self._cmd2_app)
return completer.format_help(tokens[token_index + 1 :])
break
return self._parser.format_help()
if parser:
completer_type = self._cmd2_app._determine_ap_completer_type(parser)
completer = completer_type(parser, self._cmd2_app)
completer.print_help(tokens[1:])
return

self._parser.print_help(file=file)

def _complete_arg(
self,
Expand Down
4 changes: 1 addition & 3 deletions cmd2/cmd2.py
Original file line number Diff line number Diff line change
Expand Up @@ -3876,9 +3876,7 @@ def do_help(self, args: argparse.Namespace) -> None:
# If the command function uses argparse, then use argparse's help
if func is not None and argparser is not None:
completer = argparse_completer.DEFAULT_AP_COMPLETER(argparser, self)

# Set end to blank so the help output matches how it looks when "command -h" is used
self.poutput(completer.format_help(args.subcommands), end='')
completer.print_help(args.subcommands, self.stdout)

# If there is a help func delegate to do_help
elif help_func is not None:
Expand Down
Loading