click_extra.envvar module¶
Implements environment variable utilities.
- click_extra.envvar.parse_envvar_flag(value)[source]¶
Read an environment flag’s value as a boolean, permissively.
The single interpretation shared by the flag-like variables Click Extra reads by hand as booleans (
ACCESSIBLE,DO_NOT_TRACK): the value is read as one of the boolean spellingsBOOLEAN_STATESlists, in any case and with surrounding whitespace ignored, and anything else counts as activation, the empty string included: the variable’s bare presence is the signal. The color variables are not read this way: each follows its own convention, seeresolve_color_env().Callers handle presence themselves: pass the value only when the variable is set, an unset variable being no vote at all.
- Return type:
- click_extra.envvar.machine_envvars_apply(ctx, param)[source]¶
Whether the machine-wide environment variables may set the value of
param.A machine-wide variable speaks for every CLI on the machine, like
ACCESSIBLE,CLICK_EXTRA_THEMEorNO_COLOR. It fills in only where nothing more specific set the value: the command line, the variable of the option itself and a configuration file each outrank it. So it applies only whileparamholds its built-in default.Call it from the callback of
param, once Click recorded where the value came from.- Return type:
- click_extra.envvar.normalize_envvar_id(envvar_id)[source]¶
Spell an environment variable name the way the running platform reads it.
Windows looks variables up case-insensitively, and the standard library upper-cases every name it stores: see
merge_envvar_ids(). Every other platform keeps the name as given.- Return type:
- click_extra.envvar.merge_envvar_ids(*envvar_ids)[source]¶
Merge and deduplicate environment variables.
Multiple parameters are accepted and can be single strings or arbitrary-nested iterables of strings.
Nonevalues are ignored.Variable names are deduplicated while preserving their initial order.
Caution
On Windows, environment variable names are case-insensitive, so they are normalized to uppercase as the standard library does.
Returns a tuple of strings. The result is ready to be used as the
envvarparameter for Click’s options or arguments.
- click_extra.envvar.clean_envvar_id(envvar_id)[source]¶
Utility to produce a user-friendly environment variable name from a string.
Separates all contiguous alphanumeric string segments, eliminate empty strings, join them with an underscore and uppercase the result.
Attention
This utility does not try to reproduce the current behavior of Click, which is not consistent regarding case-handling of environment variable.
- Return type:
- click_extra.envvar.param_auto_envvar_id(param, ctx)[source]¶
Compute the auto-generated environment variable of an option or argument.
Returns the auto envvar exactly as computed within Click’s internals, by
click.core.Parameter.resolve_envvar_value()andclick.core.Option.resolve_envvar_value().
- click_extra.envvar.param_envvar_ids(param, ctx)[source]¶
Returns the deduplicated, ordered list of environment variables for an option or argument, including the auto-generated one.
The auto-generated environment variable is added at the end of the list, so that user-defined envvars takes precedence. This respects the current implementation of
click.core.Option.resolve_envvar_value().Names are normalized to uppercase on Windows by
merge_envvar_ids().
- click_extra.envvar.temporary_env(set_vars=None, unset_vars=())[source]¶
Apply environment variable changes for the block’s duration, then restore.
set_vars are written into
os.environand unset_vars removed. On exit, every touched variable is restored to its pre-block state: recreated with its former value, or removed when it did not exist before.The process environment is patched directly (not through test-framework fixtures) so the helper serves production code paths and test harnesses alike, with a single restore discipline.
- click_extra.envvar.env_copy(extend=None)[source]¶
Returns a copy of the current environment variables, updated with
extend.Mimics Python’s original implementation by returning
Nonewhenextendis empty or missing, so a child process inherits the environment unchanged.A
Nonevalue removes its variable from the copy instead of setting it, which is the only way to hide an inherited variable from a child process: assigning the empty string leaves it set, and a flag read by bare presence (seeparse_envvar_flag()) counts that as activation. Same convention asclick.testing.CliRunner.invoke()’s ownenvargument, and the reasonTEnvVarsvalues are typed optional.