Skip to content
Myr

Merge pull request #1261 from gpakosz/gh-1253

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

Prevent environment variables from containing '-' characters

authored by David Lord · committed by GitHub · parents 93d947c, 91ea0de · Browse files · 4 files+27 added−4 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+27 added−4 removed

  • tests:+16 added−0 removed
  • other:+11 added−4 removed

4 files+27 added−4 removed

modified:click/core.py+2 added
333 unmodified lines
334 auto_envvar_prefix = '%s_%s' % (parent.auto_envvar_prefix,335 self.info_name.upper())336 else:337 auto_envvar_prefix = auto_envvar_prefix.upper()338 if auto_envvar_prefix is not None:339 auto_envvar_prefix = auto_envvar_prefix.replace("-", "_")340 self.auto_envvar_prefix = auto_envvar_prefix341342 if color is None and parent is not None:343 color = parent.color
1553 unmodified lines
modified:docs/options.rst+7 added-4 removed
591 unmodified lines
592environment variables which is supported for options only. To enable this593feature, the ``auto_envvar_prefix`` parameter needs to be passed to the594script that is invoked. Each command and parameter is then added as an595uppercase underscore-separated variable. If you have a subcommand596called ``foo`` taking an option called ``bar`` and the prefix is597``MY_TOOL``, then the variable is ``MY_TOOL_FOO_BAR``.596called ``run`` taking an option called ``reload`` and the prefix is597``WEB``, then the variable is ``WEB_RUN_RELOAD``.598599Example usage:600601.. click:example::
12 unmodified lines
614615 invoke(greet, env={'GREETER_USERNAME': 'john'},616 auto_envvar_prefix='GREETER')617618When using ``auto_envvar_prefix`` with command groups, the command name needs619to be included in the environment variable, between the prefix and the parameter name, *i.e.* *PREFIX_COMMAND_VARIABLE*.618When using ``auto_envvar_prefix`` with command groups, the command name619needs to be included in the environment variable, between the prefix and620the parameter name, *i.e.* ``PREFIX_COMMAND_VARIABLE``. If you have a621subcommand called ``run-server`` taking an option called ``host`` and622the prefix is ``WEB``, then the variable is ``WEB_RUN_SERVER_HOST``.623624Example:625626.. click:example::
200 unmodified lines
modified:tests/test_options.py+16 added
271 unmodified lines
272 assert not result.exception273 assert 'TEST_ARG1' in result.output274275276def test_show_envvar_auto_prefix_dash_in_command(runner):277 @click.group()278 def cli():279 pass280281 @cli.command()282 @click.option('--baz', show_envvar=True)283 def foo_bar(baz):284 pass285286 result = runner.invoke(cli, ['foo-bar', '--help'],287 auto_envvar_prefix='TEST')288 assert not result.exception289 assert 'TEST_FOO_BAR_BAZ' in result.output290291292def test_custom_validation(runner):293 def validate_pos_int(ctx, param, value):294 if value < 0:295 raise click.BadParameter('Value needs to be positive')
253 unmodified lines
modified:CHANGES.rst+2 added
42 unmodified lines
43- ``MissingParameter`` exceptions are printable in the Python44 interpreter. :issue:`1139`45- Fix how default values for file-type options are shown during46 prompts. :issue:`914`47- Fix environment variable automatic generation for commands48 containing ``-``. :issue:`1253`495051Version 7.052-----------
540 unmodified lines