prevent automatic environment variables from containing dashes
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Gregory Pakosz · committed by David Lord · parent 93d947c · 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
Provenance
- Commit
91ea0de- Tree
ec10388- Author
Gregory Pakosz <gregory.pakosz@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
93d947c
4 files changed+27 added−4 removed
modified:click/core.py+2 added
333 unmodified lines334 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_prefix341 342 if color is None and parent is not None:343 color = parent.color1553 unmodified linesmodified:docs/options.rst+7 added-4 removed
591 unmodified lines592 environment variables which is supported for options only. To enable this593 feature, the ``auto_envvar_prefix`` parameter needs to be passed to the594 script that is invoked. Each command and parameter is then added as an595 uppercase underscore-separated variable. If you have a subcommand596-called ``foo`` taking an option called ``bar`` and the prefix is597-``MY_TOOL``, then the variable is ``MY_TOOL_FOO_BAR``.596+called ``run`` taking an option called ``reload`` and the prefix is597+``WEB``, then the variable is ``WEB_RUN_RELOAD``.598 599 Example usage:600 601 .. click:example::12 unmodified lines614 615 invoke(greet, env={'GREETER_USERNAME': 'john'},616 auto_envvar_prefix='GREETER')617 618-When using ``auto_envvar_prefix`` with command groups, the command name needs619-to be included in the environment variable, between the prefix and the parameter name, *i.e.* *PREFIX_COMMAND_VARIABLE*.618+When using ``auto_envvar_prefix`` with command groups, the command name619+needs to be included in the environment variable, between the prefix and620+the parameter name, *i.e.* ``PREFIX_COMMAND_VARIABLE``. If you have a621+subcommand called ``run-server`` taking an option called ``host`` and622+the prefix is ``WEB``, then the variable is ``WEB_RUN_SERVER_HOST``.623 624 Example:625 626 .. click:example::200 unmodified linesmodified:tests/test_options.py+16 added
271 unmodified lines272 assert not result.exception273 assert 'TEST_ARG1' in result.output274 275 276+def test_show_envvar_auto_prefix_dash_in_command(runner):277+ @click.group()278+ def cli():279+ pass280+281+ @cli.command()282+ @click.option('--baz', show_envvar=True)283+ def foo_bar(baz):284+ pass285+286+ result = runner.invoke(cli, ['foo-bar', '--help'],287+ auto_envvar_prefix='TEST')288+ assert not result.exception289+ assert 'TEST_FOO_BAR_BAZ' in result.output290+291+292 def 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 linesmodified:CHANGES.rst+2 added
42 unmodified lines43 - ``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`49 50 51 Version 7.052 -----------540 unmodified lines