Skip to content

Commit

Permalink
Improve readme for long metavars.
Browse files Browse the repository at this point in the history
Add explicit mention of choices, add example and screenshots.

See #33
  • Loading branch information
ewels committed Mar 3, 2022
1 parent 27574a0 commit 334478a
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 2 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,17 @@ click.rich_click.GROUP_ARGUMENTS_OPTIONS = True

> See [`examples/06_arguments.py`](examples/06_arguments.py) for an example.
### Metavars
### Metavars and option choices

Metavars are click's way of showing expected input types.
For example, if you have an option that must be an integer, the metavar is `INTEGER`.
If you have a choice, the metavar is a list of the possible values.

By default, rich-click shows metavars in their own column.
However, with some tools this column can be quite wide and result in a lot of white space.
However, if you have a long list of choices, this column can be quite wide and result in a lot of white space:

![Default metavar display](https://raw.githubusercontent.com/ewels/rich-click/main/docs/images/metavars_default.png)

It may look better to show metavars appended to the help text, instead of in their own column.
For this, use the following:

Expand All @@ -147,6 +150,10 @@ click.rich_click.SHOW_METAVARS_COLUMN = False
click.rich_click.APPEND_METAVARS_HELP = True
```

![Appended metavar](https://raw.githubusercontent.com/ewels/rich-click/main/docs/images/metavars_appended.png)

> See [`examples/08_metavars.py`](examples/08_metavars.py) for an example.
### Error messages

By default, rich-click gives some nice formatting to error messages:
Expand Down
Binary file added docs/images/metavars_appended.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/metavars_default.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
68 changes: 68 additions & 0 deletions examples/08_metavars.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import rich_click as click

click.rich_click.SHOW_METAVARS_COLUMN = False
click.rich_click.APPEND_METAVARS_HELP = True


@click.command()
@click.option(
"--debug/--no-debug",
"-d/-n",
default=False,
help="""Enable debug mode.""",
)
@click.option(
"--number",
"-n",
type=click.Choice(
[
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine",
"ten",
"eleven",
"twelve",
"thirteen",
"fourteen",
"fifteen",
"sixteen",
"seventeen",
"eighteen",
"nineteen",
"twenty",
"twenty-one",
"twenty-two",
"twenty-three",
"twenty-four",
"twenty-five",
"twenty-six",
"twenty-seven",
"twenty-eight",
"twenty-nine",
"thirty",
]
),
show_default=True,
help="This click choice has loads of options.",
)
def cli(debug, number):
"""
My amazing tool does all the things.
This is a minimal example based on documentation
from the 'click' package.
You can try using --help at the top level and also for
specific group subcommands.
"""
print(f"Debug mode is {'on' if debug else 'off'}")


if __name__ == "__main__":
cli()

0 comments on commit 334478a

Please sign in to comment.