Datasets:
Tasks:
Text Generation
Formats:
parquet
Sub-tasks:
language-modeling
Languages:
English
Size:
< 1K
License:
Download harbor/tasks/help_defaults/solution/reference.patch from ExpertCrowd/ec-swe: direct link, hf CLI and curl.
- Browser
- Download file 8.24 kB
-
https://huggingface.co/datasets/ExpertCrowd/ec-swe/resolve/main/harbor/tasks/help_defaults/solution/reference.patch
- Command line
-
hf download hf://datasets/ExpertCrowd/ec-swe/harbor/tasks/help_defaults/solution/reference.patch
-
curl -L -o reference.patch https://huggingface.co/datasets/ExpertCrowd/ec-swe/resolve/main/harbor/tasks/help_defaults/solution/reference.patch
8.24 kB
| diff --git a/CHANGES.md b/CHANGES.md | |
| index 8853860..4976b6c 100644 | |
| --- a/CHANGES.md | |
| +++ b/CHANGES.md | |
| Unreleased | |
| Unreleased | |
| +- The `[default: ...]` shown in help for an option with `multiple=True` | |
| + renders each value the way a single value is rendered: `Enum` members by | |
| + name, empty strings as `""`. The prompt of an option lists its choices and | |
| + previews its default the same way, so what it shows can be typed back. | |
| - Fix `copy.deepcopy()` and `pickle` on a `Parameter`, `Option` or `Command`. {pr}`3805` | |
| - A `KeyboardInterrupt` arriving while `Command.main()` reports an abort or an error, | |
| or while it exits, no longer escapes as an unhandled traceback. The command still | |
| diff --git a/src/click/_utils.py b/src/click/_utils.py | |
| index 2d4e8f2..e2811bb 100644 | |
| --- a/src/click/_utils.py | |
| +++ b/src/click/_utils.py | |
| from __future__ import annotations | |
| import enum | |
| +import inspect | |
| import typing as t | |
| +from gettext import gettext as _ | |
| class Sentinel(enum.Enum): | |
| T_UNSET: t.TypeAlias = t.Literal[Sentinel.UNSET] | |
| T_FLAG_NEEDS_VALUE: t.TypeAlias = t.Literal[Sentinel.FLAG_NEEDS_VALUE] | |
| """Type hint for the :data:`FLAG_NEEDS_VALUE` sentinel value.""" | |
| + | |
| + | |
| +def _format_default_value(value: t.Any) -> str: | |
| + """Render a value the way ``[default: ...]`` and prompts show it. | |
| + | |
| + A list or tuple renders as its values, comma separated, each rendered by | |
| + the same rules as a single value: an Enum member by its name, an empty | |
| + string as ``""``, a plain function as ``(dynamic)``, anything else with | |
| + ``str()``. | |
| + | |
| + :meta private: | |
| + """ | |
| + if isinstance(value, (list, tuple)): | |
| + return ", ".join(_format_default_value(item) for item in value) | |
| + if isinstance(value, enum.Enum): | |
| + return value.name | |
| + if inspect.isfunction(value): | |
| + return _("(dynamic)") | |
| + if isinstance(value, str) and value == "": | |
| + return '""' | |
| + return str(value) | |
| + | |
| + | |
| +def _format_choice(choice: t.Any) -> str: | |
| + """Render one choice the way prompts and help list it: an Enum member by | |
| + its name, anything else with ``str()``. | |
| + | |
| + :meta private: | |
| + """ | |
| + return choice.name if isinstance(choice, enum.Enum) else str(choice) | |
| diff --git a/src/click/core.py b/src/click/core.py | |
| index 1f9323a..e82eaa0 100644 | |
| --- a/src/click/core.py | |
| +++ b/src/click/core.py | |
| from types import TracebackType | |
| from . import types | |
| from ._utils import FLAG_NEEDS_VALUE | |
| +from ._utils import _format_default_value | |
| from ._utils import UNSET | |
| from .exceptions import Abort | |
| from .exceptions import BadParameter | |
| class Option(Parameter): | |
| ): | |
| if show_default_is_str: | |
| default_string = f"({self.show_default})" | |
| - elif isinstance(default_value, (list, tuple)): | |
| - default_string = ", ".join(str(d) for d in default_value) | |
| - elif isinstance(default_value, enum.Enum): | |
| - default_string = default_value.name | |
| - elif inspect.isfunction(default_value): | |
| - default_string = _("(dynamic)") | |
| + elif isinstance(default_value, (list, tuple)) or ( | |
| + isinstance(default_value, enum.Enum) | |
| + or inspect.isfunction(default_value) | |
| + ): | |
| + # Multi-valued defaults render each value the way the same | |
| + # value renders for a single-valued option. | |
| + default_string = _format_default_value(default_value) | |
| elif self.is_bool_flag and self.secondary_opts: | |
| # For boolean flags that have distinct True/False opts, | |
| # use the opt without prefix instead of the value. | |
| diff --git a/src/click/termui.py b/src/click/termui.py | |
| index 8fe0085..0caa7cd 100644 | |
| --- a/src/click/termui.py | |
| +++ b/src/click/termui.py | |
| from gettext import gettext as _ | |
| from . import _compat | |
| from ._compat import isatty | |
| from ._compat import strip_ansi | |
| +from ._utils import _format_choice | |
| +from ._utils import _format_default_value | |
| from .exceptions import Abort | |
| from .exceptions import UsageError | |
| from .globals import resolve_color_default | |
| def _build_prompt( | |
| ) -> str: | |
| prompt = text | |
| if type is not None and show_choices and isinstance(type, Choice): | |
| - prompt += f" ({', '.join(map(str, type.choices))})" | |
| + # Each choice is listed the way it can be typed back, which for an | |
| + # Enum member is its name, as the help and the error message show it. | |
| + prompt += f" ({', '.join(_format_choice(choice) for choice in type.choices)})" | |
| default_preview = "" | |
| if show_default: | |
| if isinstance(show_default, str): | |
| def _format_default(default: V) -> V | str: | |
| if name is not None: | |
| return str(name) | |
| - return default | |
| + # The default is previewed the way the help renders it, so that what is | |
| + # shown is what the user can type back. | |
| + return _format_default_value(default) | |
| @t.overload | |
| def prompt( | |
| prompt = _build_prompt( | |
| text, prompt_suffix, show_default, default, show_choices, type | |
| ) | |
| + # The default exactly as the prompt shows it. Typing that text back | |
| + # selects the default itself, as pressing Enter does, so that a value | |
| + # rendered for display (a quoted empty string, a list of values) can be | |
| + # typed back as shown. | |
| + shown_default = ( | |
| + str(_format_default(default)) | |
| + if default is not None and show_default and not isinstance(show_default, str) | |
| + else None | |
| + ) | |
| if confirmation_prompt: | |
| if confirmation_prompt is True: | |
| def prompt( | |
| value = t.cast("str", default) | |
| break | |
| try: | |
| - result = value_proc(value) | |
| + result = value_proc( | |
| + default if shown_default is not None and value == shown_default else value | |
| + ) | |
| except UsageError as e: | |
| message = _mask_hidden_input(e.message, value) if hide_input else e.message | |
| echo(_("Error: {message}").format(message=message), err=err) | |
| diff --git a/tests/test_help_defaults_multiple.py b/tests/test_help_defaults_multiple.py | |
| new file mode 100644 | |
| index 0000000..cb6eb77 | |
| --- /dev/null | |
| +++ b/tests/test_help_defaults_multiple.py | |
| +import enum | |
| + | |
| +import click | |
| +from click.testing import CliRunner | |
| + | |
| + | |
| +class Color(enum.Enum): | |
| + Red = "r" | |
| + Blue = "b" | |
| + | |
| + | |
| +def _help(**kwargs): | |
| + @click.command() | |
| + @click.option("--opt", show_default=True, **kwargs) | |
| + def cmd(opt): | |
| + pass | |
| + | |
| + return CliRunner().invoke(cmd, ["--help"]).output | |
| + | |
| + | |
| +def test_multiple_enum_default_shows_names(): | |
| + assert "[default: Red, Blue]" in _help( | |
| + type=click.Choice(Color), multiple=True, default=[Color.Red, Color.Blue] | |
| + ) | |
| + | |
| + | |
| +def test_multiple_empty_string_default_is_quoted(): | |
| + assert '[default: "", x]' in _help(multiple=True, default=["", "x"]) | |
| + | |
| + | |
| +def test_nargs_with_multiple_renders_every_value(): | |
| + assert "[default: 1, 2, 3, 4]" in _help(nargs=2, multiple=True, default=[(1, 2), (3, 4)]) | |
| + | |
| + | |
| +def _prompt_line(input_text="\n", **kwargs): | |
| + @click.command() | |
| + @click.option("--opt", prompt="Value", **kwargs) | |
| + def cmd(opt): | |
| + pass | |
| + | |
| + return CliRunner().invoke(cmd, [], input=input_text).output.split("\n", 1)[0] | |
| + | |
| + | |
| +def test_prompt_lists_enum_choices_and_default_by_name(): | |
| + line = _prompt_line(type=click.Choice(Color), default=Color.Blue) | |
| + assert "(Red, Blue)" in line | |
| + assert "[Blue]" in line | |
| + | |
| + | |
| +def test_prompt_previews_empty_string_default_quoted(): | |
| + assert '[""]' in _prompt_line(default="") | |
| + | |
| + | |
| +def test_prompt_default_typed_back_as_shown(): | |
| + line = CliRunner().invoke( | |
| + _typed_back_cmd(multiple=True, default=["", "x"]), [], input='"", x\n' | |
| + ).output | |
| + assert "GOT=('', 'x')" in line | |
| + line = CliRunner().invoke(_typed_back_cmd(default=""), [], input='""\n').output | |
| + assert "GOT=''" in line | |
| + | |
| + | |
| +def _typed_back_cmd(**kwargs): | |
| + @click.command() | |
| + @click.option("--opt", prompt="Value", **kwargs) | |
| + def cmd(opt): | |
| + click.echo(f"GOT={opt!r}") | |
| + | |
| + return cmd | |