ewels / rich-click

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

Use a RichCommand for the actual rich-click CLI tool + add formatting options #155

Closed dwreeves closed 9 months ago

dwreeves commented 10 months ago

Right now the cli.py uses a custom CLI implementation, I believe for historic reasons. I think this can be refactored into being a RichCommand.

There are two main benefits of this.

First: simpler, more idiomatic, and more testable code. Testability is the most important sub-bullet here. Right now cli.py is not covered by tests. See #124. This lack of test coverage of cli.py has caused multiple regressions over the last couple months; see #142 and #152.

Second: it would be to allow for things like passing of configuration flags, which can allow users who are aliasing rich-click to set options such as color themes. For example:

alias foo="rich-click --rich-config '{"\style_option\": \"red\", \"width\": 100}' foo"
foo bar

In the above example, the user sets some command in their Python environment named foo to an alias that invokes rich-click with custom style options.

There may be other options and flags that are of interest here; that's just the first that comes to mind.


This is doable and theoretically simple since Click allows for arbitrary passing of args to a command:

# foo.py
import click

@click.command(name='my-cmd', context_settings=dict(
    ignore_unknown_options=True,
    allow_extra_args=True,
))
@click.option('--option1')
@click.option('--option2')
@click.pass_context
def cli(ctx, option1, option2):
    print(ctx.args)

cli()
>>> python foo.py --option1 a --option2 b c d e --f g
['c', 'd', 'e', '--f', 'g']