Skip to content

Commit a562c69

Browse files
jonrebmBastian-Krause
authored andcommitted
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 b1e6af3 commit a562c69

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
@@ -1758,8 +1758,58 @@ def __str__(self):
17581758
return self.value
17591759

17601760

1761-
def get_parser() -> argparse.ArgumentParser:
1762-
parser = argparse.ArgumentParser()
1761+
class AutoProgramArgumentParser(argparse.ArgumentParser):
1762+
"""Works around sphinxcontrib.autoprogram shortcomings."""
1763+
1764+
class _ActionWrapper:
1765+
"""
1766+
Wraps argparse's special private action object registered for "parsers", see:
1767+
https://docs.python.org/3.14/library/argparse.html#argparse.ArgumentParser.add_subparsers
1768+
https://docs.python.org/3.13/library/argparse.html#argparse.ArgumentParser.register
1769+
"""
1770+
1771+
def __init__(self, action):
1772+
self.action = action
1773+
1774+
def add_parser(self, name, **kwargs):
1775+
# aliases are not supported by autoprogram, they lead to duplicate entries, so
1776+
# show them as "command subcommand|alias --option" instead
1777+
aliases = kwargs.pop("aliases", [])
1778+
if aliases:
1779+
name = "|".join([name] + list(aliases))
1780+
1781+
# "help" text is ignored by autoprogram, move to "description" instead
1782+
if "description" not in kwargs and "help" in kwargs:
1783+
kwargs["description"] = kwargs.pop("help")
1784+
1785+
# add_parser() is the only part of the action object that's not an implementation detail
1786+
return self.action.add_parser(name, **kwargs)
1787+
1788+
def __init__(self, *args, **kwargs):
1789+
# print help texts in fixed width
1790+
os.environ["COLUMNS"] = "80"
1791+
1792+
# hide --help, -h
1793+
kwargs.setdefault("add_help", False)
1794+
1795+
super().__init__(*args, **kwargs)
1796+
1797+
def add_subparsers(self, **kwargs):
1798+
action = super().add_subparsers(**kwargs)
1799+
return self._ActionWrapper(action)
1800+
1801+
def add_argument(self, *args, **kwargs):
1802+
# do not show ranges
1803+
if isinstance(kwargs.get("choices"), range):
1804+
del kwargs["choices"]
1805+
1806+
return super().add_argument(*args, **kwargs)
1807+
1808+
1809+
def get_parser(auto_doc_mode=False) -> "argparse.ArgumentParser | AutoProgramArgumentParser":
1810+
# use custom parser for sphinxcontrib.autoprogram
1811+
parser = AutoProgramArgumentParser() if auto_doc_mode else argparse.ArgumentParser()
1812+
17631813
parser.add_argument(
17641814
"-x",
17651815
"--coordinator",
@@ -1797,11 +1847,13 @@ def get_parser() -> argparse.ArgumentParser:
17971847
metavar="COMMAND",
17981848
)
17991849

1800-
subparser = subparsers.add_parser("help")
1850+
# if the argparse object is to be used for manpage generation, hide some subcommands
1851+
if not auto_doc_mode:
1852+
subparser = subparsers.add_parser("help")
18011853

1802-
subparser = subparsers.add_parser("complete")
1803-
subparser.add_argument("type", choices=["resources", "places", "matches", "match-names"])
1804-
subparser.set_defaults(func=ClientSession.complete)
1854+
subparser = subparsers.add_parser("complete")
1855+
subparser.add_argument("type", choices=["resources", "places", "matches", "match-names"])
1856+
subparser.set_defaults(func=ClientSession.complete)
18051857

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

0 commit comments

Comments
 (0)