ewels / rich-click

Format click help output nicely with rich.
https://ewels.github.io/rich-click/
MIT License
630 stars 36 forks source link

Grouped `--help` output is not shown for `__main__.py` CLIs (`python -m foo`) / add support for `prog_name` #201

Closed Julian closed 5 months ago

Julian commented 5 months ago

Given a basic package called foo, with:

foo/_cli.py

from rich_click.utils import CommandGroupDict
import rich_click as click

COMMAND_GROUPS = dict(
    foo=[
        CommandGroupDict(
            name="Group 1",
            commands=["bar"],
        ),
        CommandGroupDict(
            name="Group 2",
            commands=["baz", "quux"],
        ),
    ],
)

@click.rich_config(
    help_config=click.RichHelpConfiguration(command_groups=COMMAND_GROUPS),
)
@click.group(context_settings=dict(help_option_names=["--help", "-h"]))
def main():
    pass

@main.command()
def bar():
    pass

@main.command()
def baz():
    pass

foo/__main__.py

from foo import _cli
_cli.main

pyproject.toml

[build-system]
requires = ["hatchling", "hatch-vcs"]
build-backend = "hatchling.build"

[project]
name = "foo"
version = "0.1.0"
description = ""
requires-python = ">=3.10"
readme = "README.rst"
license = {text = "MIT"}
keywords = []
authors = [{name = "Julian Berman"}]
dependencies = ["rich-click"]

[project.scripts]
foo = "foo._cli:main"

i.e. a __main__.py for python -m foo, the results of python -m foo --help does not contain the command groups, even though foo --help will:

~/Desktop/foo is a git repository on main 
⊙  venv/bin/foo --help                                                                                                                                                                                                                                                        julian@Airm ●

 Usage: foo [OPTIONS] COMMAND [ARGS]...                                                                                                                                                                                                                                                     

╭─ Options ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ --help  -h    Show this message and exit.                                                                                                                                                                                                                                                │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─ Group 1 ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ bar                                                                                                                                                                                                                                                                                      │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─ Group 2 ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ baz                                                                                                                                                                                                                                                                                      │
│ quux                                                                                                                                                                                                                                                                                     │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯

~/Desktop/foo is a git repository on main 
⊙  venv/bin/python -m foo --help                                                                                                                                                                                                                                              julian@Airm ●

 Usage: python -m foo [OPTIONS] COMMAND [ARGS]...                                                                                                                                                                                                                                           

╭─ Options ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ --help  -h    Show this message and exit.                                                                                                                                                                                                                                                │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─ Commands ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ bar                                                                                                                                                                                                                                                                                      │
│ baz                                                                                                                                                                                                                                                                                      │
│ quux                                                                                                                                                                                                                                                                                     │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯

In "native" click, the prog_name option exists in a few places to explicitly say what the program name is -- in my personal experience I always set this, as otherwise it's simply incorrect when the program is used via python -m. Perhaps something like that is needed here as well in order to tell rich-click that when I say foo in COMMAND_GROUPS that we still are talking about the same program?

Not sure if there's another obvious solution here. Thanks again!

dwreeves commented 5 months ago

🤔 I'll look into this. Thanks for raising it!

In the meanwhile, I believe if you do this, it should work:

@click.rich_config(
    help_config=click.RichHelpConfiguration(command_groups=COMMAND_GROUPS),
)
@click.group("foo", context_settings=dict(help_option_names=["--help", "-h"]))
def main():
    pass

^ Here, I added foo as the name of the group.

dwreeves commented 5 months ago

Fixed this for 1.8.3, which I just released. I used the minimally reproducible example you provided (thank you for that!!! ❤️) and confirmed the change works, both for python -m foo and python3 -m foo.

Thanks for opening the issue!