Skip to content

Commit 011f80f

Browse files
fixup! remote/client: sphinx autoprogram workarounds
ManpageArgumentParser -> AutoProgramArgumentParser, no monkey patching
1 parent bebd734 commit 011f80f

1 file changed

Lines changed: 31 additions & 20 deletions

File tree

labgrid/remote/client.py

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1708,41 +1708,52 @@ def __str__(self):
17081708
return self.value
17091709

17101710

1711-
class ManpageArgumentParser(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+
17121738
def __init__(self, *args, **kwargs):
17131739
# print help texts in fixed width
17141740
os.environ["COLUMNS"] = "180"
17151741

17161742
# hide --help, -h
17171743
kwargs.setdefault("add_help", False)
1744+
17181745
super().__init__(*args, **kwargs)
17191746

17201747
def add_subparsers(self, **kwargs):
1721-
subparsers = super().add_subparsers(**kwargs)
1722-
original_add_parser = subparsers.add_parser
1723-
1724-
def man_add_parser(name, **kwargs):
1725-
# hide --help, -h
1726-
kwargs.setdefault("add_help", False)
1727-
1728-
# aliases are not supported by autoprogram, they lead to duplicate entries
1729-
# instead, show them as command subcommand|alias --option
1730-
aliases = kwargs.pop("aliases", [])
1731-
if aliases:
1732-
name = "|".join([name] + list(aliases))
1748+
action = super().add_subparsers(**kwargs)
1749+
return self._ActionWrapper(action)
17331750

1734-
# the "help" text is ignore by autoprogram. Move to "description" instead.
1735-
if "description" not in kwargs and "help" in kwargs:
1736-
kwargs["description"] = kwargs.pop("help")
17371751

1738-
return original_add_parser(name, **kwargs)
17391752

1740-
subparsers.add_parser = man_add_parser
1741-
return subparsers
17421753

17431754
def get_parser(auto_doc_mode=False) -> "argparse.ArgumentParser | AutoProgramArgumentParser":
17441755
# use custom parser for sphinxcontrib.autoprogram
1745-
parser = ManpageArgumentParser() if auto_doc_mode else argparse.ArgumentParser()
1756+
parser = AutoProgramArgumentParser() if auto_doc_mode else argparse.ArgumentParser()
17461757

17471758
parser.add_argument(
17481759
"-x",

0 commit comments

Comments
 (0)