Skip to content

Commit

Permalink
Merge pull request #54 from dwreeves/main
Browse files Browse the repository at this point in the history
  • Loading branch information
ewels committed Apr 9, 2022
2 parents 29c8746 + dfda159 commit 964ebbd
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Version 1.3.1.dev0

- Bumped minimum version of `rich` from `10` to `10.7.0` (when `Group` was introduced)
- Refactored CLI's patching functionality to support `from rich_click.cli import patch`.

## Version 1.3.0 (2022-03-29)

Expand Down
36 changes: 35 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ This means that you can continue to use the unmodified `click` package in parall

> See [`examples/click/02_declarative.py`](examples/click/02_declarative.py) for an example.
## Typer support
### Typer support

[`Typer`](https://github.com/tiangolo/typer) is also supported.
You need to use rich-click with the `typer` [extra](https://packaging.python.org/en/latest/tutorials/installing-packages/#installing-setuptools-extras) in your package requirements: `rich-click[typer]`
Expand Down Expand Up @@ -104,6 +104,40 @@ Usage: awesometool [OPTIONS]
..more richified output below..
```

### Patching

In some situations, you might be registering a command from another Click CLI that does not use Rich-Click:

```python
import rich_click as click
from some_library import another_cli

@click.group("my-cli")
def cli():
pass

# `another_cli` will NOT have Rich-Click markup. :(
cli.add_command(another_cli)
```

In this situation, `another_cli` retains its original behavior. In order to make `another_cli` work with Rick-Click, you need to patch `click` before you import `another_cli`. You can patch Click with `rich_click.cli.patch` like this:

```python
import rich_click as click
from rich_click.cli import patch

patch()

from some_library import another_cli # noqa: E402

@click.group("my-cli")
def cli():
pass

# `another_cli` will have Rich-Click markup. :)
cli.add_command(another_cli)
```

## Customisation

There are a large number of customisation options in rich-click.
Expand Down
13 changes: 9 additions & 4 deletions src/rich_click/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ def _print_help() -> None:
)


def patch() -> None:
"""Patch Click internals to use Rich-Click types."""
click.group = rich_group
click.command = rich_command
click.Group = RichGroup
click.Command = RichCommand


def main(args: Optional[List[str]] = None) -> Any:
"""
The [link=https://github.com/ewels/rich-click]rich-click[/] CLI provides attractive help output from any
Expand Down Expand Up @@ -122,10 +130,7 @@ def main(args: Optional[List[str]] = None) -> Any:
del args[1]
sys.argv = [prog, *args[1:]]
# patch click before importing the program function
click.group = rich_group
click.command = rich_command
click.Group = RichGroup
click.Command = RichCommand
patch()
# import the program function
module = import_module(module_path)
function = getattr(module, function_name)
Expand Down

0 comments on commit 964ebbd

Please sign in to comment.