aboutsummaryrefslogtreecommitdiffstats
path: root/packages/meshbay-cli/src/meshbay_cli/commands/help.py
blob: 6515800cc2e8bfd7c909ca80b74b7b63f6640e0a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
"""`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