Skip to content
Myr

Reorder ParameterSource from most to least explicit

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Closes #2879

authored by Kevin Deldycke · committed by Kevin Deldycke · parent 60cad4a · Browse files · 4 files+56 added−13 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+56 added−13 removed

  • other:+56 added−13 removed

4 files+56 added−13 removed

modified:docs/commands-and-groups.md+29 added
340 unmodified lines
341allowed to read from the context, but not to perform any modifications on342it.343344345### Parameter Source Priority346347When a parameter's value is resolved, Click checks several sources348in the following order of precedence:3493501. **Prompt** (`PROMPT`): interactively provided by the user at a prompt.3512. **Command line** (`COMMANDLINE`): provided as a CLI argument or option.3523. **Environment variable** (`ENVIRONMENT`): read from an environment variable.3534. **Default map** (`DEFAULT_MAP`): looked up from {attr}`Context.default_map`.3545. **Default** (`DEFAULT`): the default value defined on the parameter.355356{class}`~click.core.ParameterSource` members are ordered from most357explicit to least explicit. Because it is an {class}`~enum.IntEnum`,358you can use comparison operators to check how explicitly a value was359provided:360361```python362source = ctx.get_parameter_source("port")363364# True if the value was explicitly set (command line, prompt, or env var).365if source < click.ParameterSource.DEFAULT_MAP:366 ...367368# True if the value came from any kind of default.369if source >= click.ParameterSource.DEFAULT_MAP:370 ...371```372373374### Detecting the Source of a Parameter375376In some situations it's helpful to understand whether or not an option377or parameter came from the command line, the environment, the default
24 unmodified lines
modified:src/click/__init__.py+1 added
12 unmodified lines
13from .core import Context as Context14from .core import Group as Group15from .core import Option as Option16from .core import Parameter as Parameter17from .core import ParameterSource as ParameterSource18from .decorators import argument as argument19from .decorators import command as command20from .decorators import confirmation_option as confirmation_option21from .decorators import group as group
103 unmodified lines
modified:src/click/core.py+23 added-13 removed
138 unmodified lines
139140 return sorted(declaration_order, key=sort_key)141142143class ParameterSource(enum.Enum):144 """This is an :class:`~enum.Enum` that indicates the source of a143class ParameterSource(enum.IntEnum):144 """This is an :class:`~enum.IntEnum` that indicates the source of a145 parameter's value.146147 Use :meth:`click.Context.get_parameter_source` to get the148 source for a parameter by name.149150 Members are ordered from most explicit to least explicit source.151 This allows comparison to check if a value was explicitly provided:152153 .. code-block:: python154155 source = ctx.get_parameter_source("port")156 if source < click.ParameterSource.DEFAULT_MAP:157 ... # value was explicitly set158159 .. versionchanged:: 8.3.3160 Use :class:`~enum.IntEnum` and reorder members from most to161 least explicit. Supports comparison operators.162163 .. versionchanged:: 8.0164 Use :class:`~enum.Enum` and drop the ``validate`` method.165166 .. versionchanged:: 8.0167 Added the ``PROMPT`` value.168 """169170 PROMPT = enum.auto()171 """Used a prompt to confirm a default or provide a value."""172 COMMANDLINE = enum.auto()173 """The value was provided by the command line args."""174 ENVIRONMENT = enum.auto()175 """The value was provided with an environment variable."""161 DEFAULT = enum.auto()162 """Used the default specified by the parameter."""176 DEFAULT_MAP = enum.auto()177 """Used a default provided by :attr:`Context.default_map`."""165 PROMPT = enum.auto()166 """Used a prompt to confirm a default or provide a value."""178 DEFAULT = enum.auto()179 """Used the default specified by the parameter."""180181182class Context:183 """The context is a special internal object that holds state relevant
2391 unmodified lines
2575 # Display a deprecation warning if necessary.2576 if (2577 self.deprecated2578 and value is not UNSET2566 and source not in (ParameterSource.DEFAULT, ParameterSource.DEFAULT_MAP)2579 and source < ParameterSource.DEFAULT_MAP2580 ):2581 extra_message = (2582 f" {self.deprecated}" if isinstance(self.deprecated, str) else ""2583 )
713 unmodified lines
3297 elif (3298 self.is_flag3299 and value is True3300 and not self.is_bool_flag3288 and source not in (ParameterSource.DEFAULT, ParameterSource.DEFAULT_MAP)3301 and source < ParameterSource.DEFAULT_MAP3302 ):3303 value = self.flag_value33043305 # Re-interpret a multiple option which has been sent as-is by the parser.
1 unmodified line
3307 # FLAG_NEEDS_VALUE sentinel) with the flag_value.3308 elif (3309 self.multiple3310 and value is not UNSET3298 and source not in (ParameterSource.DEFAULT, ParameterSource.DEFAULT_MAP)3311 and source < ParameterSource.DEFAULT_MAP3312 and any(v is FLAG_NEEDS_VALUE for v in value)3313 ):3314 value = [self.flag_value if v is FLAG_NEEDS_VALUE else v for v in value]3315 source = ParameterSource.COMMANDLINE33163317 # The value wasn't set, or used the param's default, prompt for one to the user3318 # if prompting is enabled.3319 elif (3307 (3308 value is UNSET3309 or source in (ParameterSource.DEFAULT, ParameterSource.DEFAULT_MAP)3310 )3320 (value is UNSET or source >= ParameterSource.DEFAULT_MAP)3321 and self.prompt is not None3322 and (self.required or self.prompt_required)3323 and not ctx.resilient_parsing3324 ):
123 unmodified lines
modified:CHANGES.rst+3 added
24 unmodified lines
25- ``CliRunner``'s redirected streams now expose the original file descriptor26 via ``fileno()``, so that ``faulthandler``, ``subprocess``, and other27 C-level consumers no longer crash with ``io.UnsupportedOperation``.28 :issue:`2865`29- Change :class:`ParameterSource` to an :class:`~enum.IntEnum` and reorder30 its members from most to least explicit, so values can be compared to31 check whether a parameter was explicitly provided. :issue:`2879` :pr:`3248`3233Version 8.3.234-------------35
1323 unmodified lines