Skip to content

Commit

Permalink
Set usable sub-command cli description
Browse files Browse the repository at this point in the history
`Argparse` parsers use `description` to access the overall help for
a command, unlike the `add_argument` calls which use the `help`
attribute.

Documentation: https://docs.python.org/3/library/argparse.html#description
  • Loading branch information
edward-evans-aiven committed Jun 28, 2023
1 parent f868fb5 commit 9e90bf2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion aiven/client/argx.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ def add_cmd(self, func: Callable) -> None:
self._cats[cat] = parser.add_subparsers()
subparsers = self._cats[cat]

parser = subparsers.add_parser(cmd, help=func.__doc__, formatter_class=CustomFormatter)
parser = subparsers.add_parser(cmd, help=func.__doc__, description=func.__doc__, formatter_class=CustomFormatter)
parser.set_defaults(func=func)

for arg_prop in getattr(func, ARG_LIST_PROP, []):
Expand Down
18 changes: 17 additions & 1 deletion tests/test_argx.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ def yyy(self) -> None:

@arg()
def bbb(self) -> None:
"""2"""
"""2
With more explaining
"""

@arg()
def ddd(self) -> None:
Expand Down Expand Up @@ -69,6 +72,19 @@ def test_extended_commands_remain_alphabetically_ordered() -> None:
assert action_order == ["aaa", "bbb", "bbc", "ccc", "ddd", "dde", "xxx", "yyy", "yyz"]


def test_extended_command_has_function_help() -> None:
cli = TestCLI("testcli")
cli.extend_commands(cli) # Force the CLI to have its full arg set at execution

sl = SubCLI("subcli")

cli.extend_commands(sl)

help = cli.subparsers.choices[sl.bbb.__name__].format_help()
assert sl.bbb.__doc__ is not None
assert sl.bbb.__doc__ in help


class DescriptorCLI(CommandLineTool):
@property
def raise1(self) -> NoReturn:
Expand Down

0 comments on commit 9e90bf2

Please sign in to comment.