"""`help`: what `--help` prints, for the program or for one action.""" from ..errors import USAGE, CliError from ..output import sentence from .context import Context HELP = "what --help prints: the actions, or one action's options" def add_parser(sub, parent) -> None: p = sub.add_parser("help", parents=[parent], help=HELP, description=sentence(HELP) + ".") p.add_argument("topic", nargs="?", metavar="ACTION", help="the action to explain") async def run(ctx: Context) -> int: from ..__main__ import build_parser parser = build_parser() topic = ctx.args.topic if topic is None: parser.print_help() return 0 actions = next(a for a in parser._actions if hasattr(a, "choices") and a.choices and "help" in a.choices).choices if topic not in actions: raise CliError(USAGE, f"No action {topic!r}. The actions: {', '.join(actions)}.") actions[topic].print_help() return 0