Skip to content

Commit 78ad7eb

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 Signed-off-by: Jonas Rebmann <jre@pengutronix.de>
1 parent 74897ea commit 78ad7eb

File tree

2 files changed

+191
-541
lines changed

2 files changed

+191
-541
lines changed

labgrid/remote/client.py

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

17101710

1711-
def get_parser() -> argparse.ArgumentParser:
1712-
parser = argparse.ArgumentParser()
1711+
def get_parser(include_undocumented=False) -> argparse.ArgumentParser:
1712+
1713+
# A fixed-width formatter to make usage string formatting predictable for manpage generation
1714+
def formatter(prog):
1715+
return argparse.HelpFormatter(prog, width=(None if include_undocumented else 180))
1716+
1717+
parser = argparse.ArgumentParser(add_help=include_undocumented, formatter_class=formatter)
1718+
1719+
# if the parser is requested for manpage generation, inject some workarounds for subcommands
1720+
if not include_undocumented:
1721+
argparse_add_parser = argparse._SubParsersAction.add_parser
1722+
1723+
def man_add_parser(self, name, **kwargs):
1724+
# hide --help,-h
1725+
kwargs.setdefault("add_help", False)
1726+
# aliases are not supported by autoprogram, they lead to duplicate entries
1727+
# instead, show them as `command subcommand|alias --option`
1728+
aliases = kwargs.pop("aliases", [])
1729+
if aliases:
1730+
name = "|".join([name] + list(aliases))
1731+
# the "help" message is ignore by autoprogram. Use "description" instead.
1732+
if "description" not in kwargs and "help" in kwargs:
1733+
kwargs["description"] = kwargs.pop("help")
1734+
# make usage string formatting predictable
1735+
kwargs["formatter_class"] = formatter
1736+
return argparse_add_parser(self, name, **kwargs)
1737+
1738+
argparse._SubParsersAction.add_parser = man_add_parser
1739+
17131740
parser.add_argument(
17141741
"-x",
17151742
"--coordinator",
@@ -1747,11 +1774,12 @@ def get_parser() -> argparse.ArgumentParser:
17471774
metavar="COMMAND",
17481775
)
17491776

1750-
subparser = subparsers.add_parser("help")
1777+
if include_undocumented:
1778+
subparser = subparsers.add_parser("help")
17511779

1752-
subparser = subparsers.add_parser("complete")
1753-
subparser.add_argument("type", choices=["resources", "places", "matches", "match-names"])
1754-
subparser.set_defaults(func=ClientSession.complete)
1780+
subparser = subparsers.add_parser("complete")
1781+
subparser.add_argument("type", choices=["resources", "places", "matches", "match-names"])
1782+
subparser.set_defaults(func=ClientSession.complete)
17551783

17561784
subparser = subparsers.add_parser("monitor", help="monitor events from the coordinator")
17571785
subparser.set_defaults(func=ClientSession.do_monitor)
@@ -2000,7 +2028,7 @@ def get_parser() -> argparse.ArgumentParser:
20002028
"-p",
20012029
"--partition",
20022030
type=int,
2003-
choices=range(0, 256),
2031+
choices=(range(0, 256) if include_undocumented else None),
20042032
metavar="0-255",
20052033
default=1,
20062034
help="partition number to mount or 0 to mount whole disk (default: %(default)s)",
@@ -2094,7 +2122,7 @@ def main():
20942122
initial_state = os.environ.get("LG_INITIAL_STATE", None)
20952123
token = os.environ.get("LG_TOKEN", None)
20962124

2097-
parser = get_parser()
2125+
parser = get_parser(include_undocumented=True)
20982126

20992127
# make any leftover arguments available for some commands
21002128
args, leftover = parser.parse_known_args()

0 commit comments

Comments
 (0)