-
-
Notifications
You must be signed in to change notification settings - Fork 832
π 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
Open
svlandeg
wants to merge
21
commits into
fastapi:master
Choose a base branch
from
svlandeg:fix/metavar
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+107
β42
Open
π Make second column of Rich help output reflect the type consistently, even when using metavar
#1410
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
adc39e2
add two more unit tests for rich formatting
svlandeg fa7d8d5
add test from #438
svlandeg 6bb0977
test correct usage string
svlandeg 13bd5e4
fix usage string
svlandeg a6282f7
expand new unit test
svlandeg 5b105f6
fix newline
svlandeg 8f8f080
change metavar column into type column (WIP)
svlandeg 98b8469
fix other tests that didn't check for the right capitalization
svlandeg a6314ca
depending on argument/option, parse the metavar string differently
svlandeg b7d902b
fix one more old test
svlandeg da5e707
fix comments related to PR 1409
svlandeg 0208421
pragma no cover for example app output
svlandeg 987eaaf
add no cover to L390
svlandeg 62b6b59
Merge branch 'master' into fix/metavar
svlandeg c601832
revert back to old format to explicitely set Rich formatting in app
svlandeg a256d02
rename tutorial files to follow new py39 format
svlandeg ceb8e67
Merge branch 'master' into fix/metavar
svlandeg d6859fe
π¨ Auto format
pre-commit-ci-lite[bot] d495801
test for different rich_markup_mode's within the same test file
svlandeg bb27c0b
fix assert now that #1409 is merged
svlandeg 412d7ed
fix assert now that #1409 is merged (part 2)
svlandeg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,8 +30,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" | ||
|
|
@@ -108,7 +108,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: )", | ||
| ] | ||
|
|
||
|
|
@@ -132,8 +132,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, | ||
| }, | ||
| ), | ||
|
|
@@ -343,38 +343,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 | ||
|
|
||
| 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
+372
to
+373
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 | ||
|
|
@@ -386,22 +395,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( | ||
|
|
@@ -410,7 +419,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, | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Note that this part is crucial to ensure the current
Enumtests won't fail. This is the part that will replaceChoicewith something like[simple|conv|lstm]