Skip to content

Commit

Permalink
Merge pull request #1126 from mulkieran/print-all-help-option
Browse files Browse the repository at this point in the history
Add secret command-line option to print all help text
  • Loading branch information
mulkieran authored Jan 31, 2025
2 parents ac1b7c1 + e807a31 commit 9df4358
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 53 deletions.
6 changes: 6 additions & 0 deletions src/stratis_cli/_parser/_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from ._logical import LOGICAL_SUBCMDS
from ._physical import PHYSICAL_SUBCMDS
from ._pool import POOL_SUBCMDS
from ._range import PrintHelpAction


def print_help(parser):
Expand Down Expand Up @@ -213,6 +214,11 @@ def gen_parser():
# version is special, it has explicit support in argparse
parser.add_argument("--version", action="version", version=__version__)

# --print-all-help is special because it introspects on the parser
parser.add_argument(
"--print-all-help", action=PrintHelpAction, help=argparse.SUPPRESS, nargs=0
)

_add_args(parser, GEN_ARGS)

subparsers = parser.add_subparsers(title="subcommands")
Expand Down
33 changes: 33 additions & 0 deletions src/stratis_cli/_parser/_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
# isort: STDLIB
import argparse
import re
import sys

# isort: THIRDPARTY
from justbytes import B, GiB, KiB, MiB, PiB, Range, TiB
Expand Down Expand Up @@ -98,3 +99,35 @@ class DefaultAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
setattr(namespace, self.dest, values)
setattr(namespace, self.dest + "_default", False)


def gen_subparsers(parser, command_line):
"""
Yield all subparser/command_lines pairs for this parser and this prefix
command line.
:param parser: an argparse parser
:param command_line: a prefix command line
:type command_line: list of str
"""
yield (parser, command_line)
for action in (
action
for action in parser._actions # pylint: disable=protected-access
if isinstance(
action, argparse._SubParsersAction # pylint: disable=protected-access
)
):
for name, subparser in sorted(action.choices.items(), key=lambda x: x[0]):
yield from gen_subparsers(subparser, command_line + [name])


class PrintHelpAction(argparse.Action):
"""
Print the help text for every subcommand.
"""

def __call__(self, parser, namespace, values, option_string=None):
for subparser, _ in gen_subparsers(parser, []):
subparser.print_help()
sys.exit(0)
53 changes: 0 additions & 53 deletions tests/whitebox/integration/test_help_text.py

This file was deleted.

17 changes: 17 additions & 0 deletions tests/whitebox/integration/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
Test command-line argument parsing.
"""

# isort: STDLIB
from io import StringIO
from unittest import mock

# isort: LOCAL
from stratis_cli import StratisCliErrorCodes

Expand Down Expand Up @@ -432,3 +436,16 @@ def test_stratis_list_default(self):
for subcommand in [["pool"], ["filesystem"], ["blockdev"]]:
for prefix in [[], ["--propagate"]]:
self.assertEqual(RUNNER(prefix + subcommand), 0)


class TestAllHelp(RunTestCase):
"""
Verify that --print-all-help option succeeds.
"""

def test_print_all_help(self):
"""
Test the --print-all-help option.
"""
with mock.patch("sys.stdout", new=StringIO()):
self.check_system_exit(["--print-all-help"], 0)

0 comments on commit 9df4358

Please sign in to comment.