-
-
Notifications
You must be signed in to change notification settings - Fork 805
π Make second column of Rich help output reflect the type consistently, even when using metavar
#1410
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
π Make second column of Rich help output reflect the type consistently, even when using metavar
#1410
Changes from all commits
adc39e2
fa7d8d5
6bb0977
13bd5e4
a6282f7
5b105f6
8f8f080
98b8469
a6314ca
b7d902b
da5e707
0208421
987eaaf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import subprocess | ||
| import sys | ||
|
|
||
| import typer | ||
| from typer.testing import CliRunner | ||
|
|
||
| from docs_src.arguments.help import tutorial006_an as mod | ||
|
|
||
| runner = CliRunner() | ||
|
|
||
| app = typer.Typer(rich_markup_mode="rich") | ||
| app.command()(mod.main) | ||
|
|
||
|
|
||
| def test_help(): | ||
| result = runner.invoke(app, ["--help"]) | ||
| assert result.exit_code == 0 | ||
| assert "[OPTIONS] β¨userβ¨" in result.output | ||
| assert "Arguments" in result.output | ||
| assert "β¨userβ¨" in result.output | ||
| assert "name" not in result.output | ||
| assert "[default: World]" in result.output | ||
|
|
||
|
|
||
| def test_call_arg(): | ||
| result = runner.invoke(app, ["Camila"]) | ||
| assert result.exit_code == 0 | ||
| assert "Hello Camila" in result.output | ||
|
|
||
|
|
||
| def test_script(): | ||
| result = subprocess.run( | ||
| [sys.executable, "-m", "coverage", "run", mod.__file__, "--help"], | ||
| capture_output=True, | ||
| encoding="utf-8", | ||
| ) | ||
| assert "Usage" in result.stdout |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import subprocess | ||
| import sys | ||
|
|
||
| import typer | ||
| from typer.testing import CliRunner | ||
|
|
||
| from docs_src.arguments.help import tutorial006 as mod | ||
|
|
||
| runner = CliRunner() | ||
|
|
||
| app = typer.Typer(rich_markup_mode="rich") | ||
| app.command()(mod.main) | ||
|
|
||
|
|
||
| def test_help(): | ||
| result = runner.invoke(app, ["--help"]) | ||
| assert result.exit_code == 0 | ||
| assert "[OPTIONS] β¨userβ¨" in result.output | ||
| assert "Arguments" in result.output | ||
| assert "β¨userβ¨" in result.output | ||
| assert "name" not in result.output | ||
| assert "[default: World]" in result.output | ||
|
|
||
|
|
||
| def test_call_arg(): | ||
| result = runner.invoke(app, ["Camila"]) | ||
| assert result.exit_code == 0 | ||
| assert "Hello Camila" in result.output | ||
|
|
||
|
|
||
| def test_script(): | ||
| result = subprocess.run( | ||
| [sys.executable, "-m", "coverage", "run", mod.__file__, "--help"], | ||
| capture_output=True, | ||
| encoding="utf-8", | ||
| ) | ||
| assert "Usage" in result.stdout |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,8 +35,8 @@ | |
| STYLE_SWITCH = "bold green" | ||
| STYLE_NEGATIVE_OPTION = "bold magenta" | ||
| STYLE_NEGATIVE_SWITCH = "bold red" | ||
| STYLE_METAVAR = "bold yellow" | ||
| STYLE_METAVAR_SEPARATOR = "dim" | ||
| STYLE_TYPES = "bold yellow" | ||
| STYLE_TYPES_SEPARATOR = "dim" | ||
| STYLE_USAGE = "yellow" | ||
| STYLE_USAGE_COMMAND = "bold" | ||
| STYLE_DEPRECATED = "red" | ||
|
|
@@ -113,7 +113,7 @@ class OptionHighlighter(RegexHighlighter): | |
| highlights = [ | ||
| r"(^|\W)(?P<switch>\-\w+)(?![a-zA-Z0-9])", | ||
| r"(^|\W)(?P<option>\-\-[\w\-]+)(?![a-zA-Z0-9])", | ||
| r"(?P<metavar>\<[^\>]+\>)", | ||
| r"(?P<types>\<[^\>]+\>)", | ||
| r"(?P<usage>Usage: )", | ||
| ] | ||
|
|
||
|
|
@@ -137,8 +137,8 @@ def _get_rich_console(stderr: bool = False) -> Console: | |
| "switch": STYLE_SWITCH, | ||
| "negative_option": STYLE_NEGATIVE_OPTION, | ||
| "negative_switch": STYLE_NEGATIVE_SWITCH, | ||
| "metavar": STYLE_METAVAR, | ||
| "metavar_sep": STYLE_METAVAR_SEPARATOR, | ||
| "types": STYLE_TYPES, | ||
| "types_sep": STYLE_TYPES_SEPARATOR, | ||
| "usage": STYLE_USAGE, | ||
| }, | ||
| ), | ||
|
|
@@ -361,38 +361,47 @@ def _print_options_panel( | |
| opt_short_strs = [] | ||
| secondary_opt_long_strs = [] | ||
| secondary_opt_short_strs = [] | ||
|
|
||
| # check whether argument has a metavar name or type set | ||
| metavar_name = None | ||
| metavar_type = None | ||
| # TODO: when deprecating Click < 8.2, make ctx required | ||
| signature = inspect.signature(param.make_metavar) | ||
| if "ctx" in signature.parameters: | ||
| metavar_str = param.make_metavar(ctx=ctx) | ||
| else: | ||
| # Click < 8.2 | ||
| metavar_str = param.make_metavar() # type: ignore[call-arg] | ||
| if isinstance(param, click.Argument): | ||
| metavar_name = metavar_str | ||
| if isinstance(param, click.Option): | ||
| metavar_type = metavar_str | ||
|
Comment on lines
+377
to
+378
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that this part is crucial to ensure the current |
||
|
|
||
| for opt_str in param.opts: | ||
| if "--" in opt_str: | ||
| opt_long_strs.append(opt_str) | ||
| elif metavar_name: | ||
| opt_short_strs.append(metavar_name) | ||
| else: | ||
| opt_short_strs.append(opt_str) | ||
| for opt_str in param.secondary_opts: | ||
| if "--" in opt_str: | ||
| secondary_opt_long_strs.append(opt_str) | ||
| elif metavar_name: # pragma: no cover | ||
| secondary_opt_short_strs.append(metavar_name) | ||
|
Comment on lines
+390
to
+391
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not too sure yet about these |
||
| else: | ||
| secondary_opt_short_strs.append(opt_str) | ||
|
|
||
| # Column for a metavar, if we have one | ||
| metavar = Text(style=STYLE_METAVAR, overflow="fold") | ||
| # TODO: when deprecating Click < 8.2, make ctx required | ||
| signature = inspect.signature(param.make_metavar) | ||
| if "ctx" in signature.parameters: | ||
| metavar_str = param.make_metavar(ctx=ctx) | ||
| else: | ||
| # Click < 8.2 | ||
| metavar_str = param.make_metavar() # type: ignore[call-arg] | ||
| # Column for recording the type | ||
| types = Text(style=STYLE_TYPES, overflow="fold") | ||
|
|
||
| # Do it ourselves if this is a positional argument | ||
| if ( | ||
| isinstance(param, click.Argument) | ||
| and param.name | ||
| and metavar_str == param.name.upper() | ||
| ): | ||
| metavar_str = param.type.name.upper() | ||
|
|
||
| # Skip booleans and choices (handled above) | ||
| if metavar_str != "BOOLEAN": | ||
| metavar.append(metavar_str) | ||
| # Fetch type | ||
| if metavar_type and metavar_type != "BOOLEAN": | ||
| types.append(metavar_type) | ||
| else: | ||
| type_str = param.type.name.upper() | ||
| if type_str != "BOOLEAN": | ||
| types.append(type_str) | ||
|
|
||
| # Range - from | ||
| # https://github.com/pallets/click/blob/c63c70dabd3f86ca68678b4f00951f78f52d0270/src/click/core.py#L2698-L2706 # noqa: E501 | ||
|
|
@@ -404,22 +413,22 @@ def _print_options_panel( | |
| ): | ||
| range_str = param.type._describe_range() | ||
| if range_str: | ||
| metavar.append(RANGE_STRING.format(range_str)) | ||
| types.append(RANGE_STRING.format(range_str)) | ||
|
|
||
| # Required asterisk | ||
| required: Union[str, Text] = "" | ||
| if param.required: | ||
| required = Text(REQUIRED_SHORT_STRING, style=STYLE_REQUIRED_SHORT) | ||
|
|
||
| # Highlighter to make [ | ] and <> dim | ||
| class MetavarHighlighter(RegexHighlighter): | ||
| class TypesHighlighter(RegexHighlighter): | ||
| highlights = [ | ||
| r"^(?P<metavar_sep>(\[|<))", | ||
| r"(?P<metavar_sep>\|)", | ||
| r"(?P<metavar_sep>(\]|>)$)", | ||
| r"^(?P<types_sep>(\[|<))", | ||
| r"(?P<types_sep>\|)", | ||
| r"(?P<types_sep>(\]|>)$)", | ||
| ] | ||
|
|
||
| metavar_highlighter = MetavarHighlighter() | ||
| types_highlighter = TypesHighlighter() | ||
|
|
||
| required_rows.append(required) | ||
| options_rows.append( | ||
|
|
@@ -428,7 +437,7 @@ class MetavarHighlighter(RegexHighlighter): | |
| highlighter(",".join(opt_short_strs)), | ||
| negative_highlighter(",".join(secondary_opt_long_strs)), | ||
| negative_highlighter(",".join(secondary_opt_short_strs)), | ||
| metavar_highlighter(metavar), | ||
| types_highlighter(types), | ||
| _get_parameter_help( | ||
| param=param, | ||
| ctx=ctx, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The commented line on L123 and L133 would be the correct
assertonce #1409 is merged. Suggest to review #1409 first before this one.