Skip to content
Myr

Reorder `ParameterSource` (#3248)

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

authored by Rowlando13 · committed by GitHub · parents 76552ff, ac5cec5 · 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 )
732 unmodified lines
3316 elif (3317 self.is_flag3318 and value is True3319 and not self.is_bool_flag3307 and source not in (ParameterSource.DEFAULT, ParameterSource.DEFAULT_MAP)3320 and source < ParameterSource.DEFAULT_MAP3321 ):3322 value = self.flag_value33233324 # Re-interpret a multiple option which has been sent as-is by the parser.
1 unmodified line
3326 # FLAG_NEEDS_VALUE sentinel) with the flag_value.3327 elif (3328 self.multiple3329 and value is not UNSET3317 and source not in (ParameterSource.DEFAULT, ParameterSource.DEFAULT_MAP)3330 and source < ParameterSource.DEFAULT_MAP3331 and any(v is FLAG_NEEDS_VALUE for v in value)3332 ):3333 value = [self.flag_value if v is FLAG_NEEDS_VALUE else v for v in value]3334 source = ParameterSource.COMMANDLINE33353336 # The value wasn't set, or used the param's default, prompt for one to the user3337 # if prompting is enabled.3338 elif (3326 (3327 value is UNSET3328 or source in (ParameterSource.DEFAULT, ParameterSource.DEFAULT_MAP)3329 )3339 (value is UNSET or source >= ParameterSource.DEFAULT_MAP)3340 and self.prompt is not None3341 and (self.required or self.prompt_required)3342 and not ctx.resilient_parsing3343 ):
123 unmodified lines
modified:CHANGES.rst+3 added
32 unmodified lines
33- ``CliRunner``'s redirected streams now expose the original file descriptor34 via ``fileno()``, so that ``faulthandler``, ``subprocess``, and other35 C-level consumers no longer crash with ``io.UnsupportedOperation``.36 :issue:`2865`37- Change :class:`ParameterSource` to an :class:`~enum.IntEnum` and reorder38 its members from most to least explicit, so values can be compared to39 check whether a parameter was explicitly provided. :issue:`2879` :pr:`3248`4041Version 8.3.242-------------43
1323 unmodified lines