Skip to content

Commit 81ea5e3

Browse files
committed
remote/client: sphinx autoprogram workarounds
Hide intentionally undocumented subcommands and overlong lists of choices. Work around shortcomings of autoprogram's subcommand handling: - Don't show the --help, -h option for each subparser - Display aliases as `command subcommand|alias --option` - Show the "help" message of the subparser - Don't show ranges in "choices" Signed-off-by: Jonas Rebmann <jre@pengutronix.de> [bst: renamed for_manpage -> auto_doc_mode, switched its default to False, autoprogram now calls "labgrid.remote.client:get_parser(auto_doc_mode=True)", replaced monkey-patched _SubParsersAction.add_parser() with _ActionWrapper class, don't show `range` in `choices` generically, adjust commit message] Signed-off-by: Bastian Krause <bst@pengutronix.de>
1 parent 2e41e03 commit 81ea5e3

File tree

3 files changed

+225
-540
lines changed

3 files changed

+225
-540
lines changed

doc/man/client.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ This is the client to control a boards status and interface with it on remote ma
1010
.. currentmodule:: labgrid.remote.client
1111

1212

13-
.. autoprogram:: labgrid.remote.client:get_parser()
13+
.. autoprogram:: labgrid.remote.client:get_parser(auto_doc_mode=True)
1414
:prog: labgrid-client
1515

1616
Configuration File

labgrid/remote/client.py

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1708,8 +1708,58 @@ def __str__(self):
17081708
return self.value
17091709

17101710

1711-
def get_parser() -> argparse.ArgumentParser:
1712-
parser = argparse.ArgumentParser()
1711+
class AutoProgramArgumentParser(argparse.ArgumentParser):
1712+
"""Works around sphinxcontrib.autoprogram shortcomings."""
1713+
1714+
class _ActionWrapper:
1715+
"""
1716+
Wraps argparse's special private action object registered for "parsers", see:
1717+
https://docs.python.org/3.14/library/argparse.html#argparse.ArgumentParser.add_subparsers
1718+
https://docs.python.org/3.13/library/argparse.html#argparse.ArgumentParser.register
1719+
"""
1720+
1721+
def __init__(self, action):
1722+
self.action = action
1723+
1724+
def add_parser(self, name, **kwargs):
1725+
# aliases are not supported by autoprogram, they lead to duplicate entries, so
1726+
# show them as "command subcommand|alias --option" instead
1727+
aliases = kwargs.pop("aliases", [])
1728+
if aliases:
1729+
name = "|".join([name] + list(aliases))
1730+
1731+
# "help" text is ignored by autoprogram, move to "description" instead
1732+
if "description" not in kwargs and "help" in kwargs:
1733+
kwargs["description"] = kwargs.pop("help")
1734+
1735+
# add_parser() is the only part of the action object that's not an implementation detail
1736+
return self.action.add_parser(name, **kwargs)
1737+
1738+
def __init__(self, *args, **kwargs):
1739+
# print help texts in fixed width
1740+
os.environ["COLUMNS"] = "80"
1741+
1742+
# hide --help, -h
1743+
kwargs.setdefault("add_help", False)
1744+
1745+
super().__init__(*args, **kwargs)
1746+
1747+
def add_subparsers(self, **kwargs):
1748+
action = super().add_subparsers(**kwargs)
1749+
return self._ActionWrapper(action)
1750+
1751+
def add_argument(self, *args, **kwargs):
1752+
# do not show ranges
1753+
if isinstance(kwargs.get("choices"), range):
1754+
del kwargs["choices"]
1755+
1756+
return super().add_argument(*args, **kwargs)
1757+
1758+
1759+
def get_parser(auto_doc_mode=False) -> "argparse.ArgumentParser | AutoProgramArgumentParser":
1760+
# use custom parser for sphinxcontrib.autoprogram
1761+
parser = AutoProgramArgumentParser() if auto_doc_mode else argparse.ArgumentParser()
1762+
17131763
parser.add_argument(
17141764
"-x",
17151765
"--coordinator",
@@ -1747,11 +1797,13 @@ def get_parser() -> argparse.ArgumentParser:
17471797
metavar="COMMAND",
17481798
)
17491799

1750-
subparser = subparsers.add_parser("help")
1800+
# if the argparse object is to be used for manpage generation, hide some subcommands
1801+
if not auto_doc_mode:
1802+
subparser = subparsers.add_parser("help")
17511803

1752-
subparser = subparsers.add_parser("complete")
1753-
subparser.add_argument("type", choices=["resources", "places", "matches", "match-names"])
1754-
subparser.set_defaults(func=ClientSession.complete)
1804+
subparser = subparsers.add_parser("complete")
1805+
subparser.add_argument("type", choices=["resources", "places", "matches", "match-names"])
1806+
subparser.set_defaults(func=ClientSession.complete)
17551807

17561808
subparser = subparsers.add_parser("monitor", help="monitor events from the coordinator")
17571809
subparser.set_defaults(func=ClientSession.do_monitor)

0 commit comments

Comments
 (0)