Add show_envvar for showing environment variables in help
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Matt Haggard · committed by Matt Haggard · parent e6f22c6 · Browse files · 2 files·+41 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+41 added−1 removed
- tests:+24 added−0 removed
- other:+17 added−1 removed
Provenance
- Commit
944cb6f- Tree
6923141- Author
Matt Haggard <haggardii@gmail.com>- Committer
Matt Haggard <haggardii@gmail.com>- Parent
e6f22c6
2 files changed+41 added−1 removed
modified:click/core.py+17 added-1 removed
1428 unmodified lines1429 All other parameters are passed onwards to the parameter constructor.1430 1431 :param show_default: controls if the default value should be shown on the1432 help page. Normally, defaults are not shown.1433+ :param show_envvar: controls if an environment variable should be shown on1434+ the help page. Normally, environment variables1435+ are not shown.1436 :param prompt: if set to `True` or a non empty string then the user will be1437 prompted for input. If set to `True` the prompt will be the1438 option name capitalized.1439 :param confirmation_prompt: if set then the value will need to be confirmed23 unmodified lines1463 def __init__(self, param_decls=None, show_default=False,1464 prompt=False, confirmation_prompt=False,1465 hide_input=False, is_flag=None, flag_value=None,1466 multiple=False, count=False, allow_from_autoenv=True,1464- type=None, help=None, hidden=False, show_choices=True, **attrs):1467+ type=None, help=None, hidden=False, show_choices=True,1468+ show_envvar=False, **attrs):1469 default_is_missing = attrs.get('default', _missing) is _missing1470 Parameter.__init__(self, param_decls, type=type, **attrs)1471 1472 if prompt is True:38 unmodified lines1511 self.allow_from_autoenv = allow_from_autoenv1512 self.help = help1513 self.show_default = show_default1514 self.show_choices = show_choices1515+ self.show_envvar = show_envvar1516 1517 # Sanity check for stuff we don't support1518 if __debug__:1519 if self.nargs < 0:105 unmodified lines1625 rv.append(_write_opts(self.secondary_opts))1626 1627 help = self.help or ''1628 extra = []1629+ if self.show_envvar:1630+ envvar = self.envvar1631+ if envvar is None:1632+ if self.allow_from_autoenv and \1633+ ctx.auto_envvar_prefix is not None:1634+ envvar = '%s_%s' % (ctx.auto_envvar_prefix, self.name.upper())1635+ if envvar is not None:1636+ extra.append('env var: %s' % (1637+ ', '.join('%s' % d for d in envvar)1638+ if isinstance(envvar, (list, tuple))1639+ else envvar, ))1640 if self.default is not None and self.show_default:1641 extra.append('default: %s' % (1642 ', '.join('%s' % d for d in self.default)1643 if isinstance(self.default, (list, tuple))127 unmodified linesmodified:tests/test_options.py+24 added
196 unmodified lines197 assert not result.exception198 assert result.output == 'x|1\ny|2\n'199 200 201+def test_show_envvar(runner):202+ @click.command()203+ @click.option('--arg1', envvar='ARG1',204+ show_envvar=True)205+ def cmd(arg):206+ pass207+208+ result = runner.invoke(cmd, ['--help'])209+ assert not result.exception210+ assert 'ARG1' in result.output211+212+213+def test_show_envvar_auto_prefix(runner):214+ @click.command()215+ @click.option('--arg1', show_envvar=True)216+ def cmd(arg):217+ pass218+219+ result = runner.invoke(cmd, ['--help'],220+ auto_envvar_prefix='TEST')221+ assert not result.exception222+ assert 'TEST_ARG1' in result.output223+224+225 def test_custom_validation(runner):226 def validate_pos_int(ctx, value):227 if value < 0:228 raise click.BadParameter('Value needs to be positive')133 unmodified lines