Repository: pallets/click
Pull request #710 (2017-01-04 17:12:55)

Add show_envvar for showing environment variables in help


Patch:
diff --git a/click/core.py b/click/core.py
index 18c1766..e7073c1 100644
--- a/click/core.py
+++ b/click/core.py
@@ -1430,6 +1430,9 @@ class Option(Parameter):
 
     :param show_default: controls if the default value should be shown on the
                          help page.  Normally, defaults are not shown.
+    :param show_envvar: controls if an environment variable should be shown on
+                        the help page.  Normally, environment variables
+                        are not shown.
     :param prompt: if set to `True` or a non empty string then the user will be
                    prompted for input.  If set to `True` the prompt will be the
                    option name capitalized.
@@ -1461,7 +1464,8 @@ class Option(Parameter):
                  prompt=False, confirmation_prompt=False,
                  hide_input=False, is_flag=None, flag_value=None,
                  multiple=False, count=False, allow_from_autoenv=True,
-                 type=None, help=None, hidden=False, show_choices=True, **attrs):
+                 type=None, help=None, hidden=False, show_choices=True,
+                 show_envvar=False, **attrs):
         default_is_missing = attrs.get('default', _missing) is _missing
         Parameter.__init__(self, param_decls, type=type, **attrs)
 
@@ -1508,6 +1512,7 @@ class Option(Parameter):
         self.help = help
         self.show_default = show_default
         self.show_choices = show_choices
+        self.show_envvar = show_envvar
 
         # Sanity check for stuff we don't support
         if __debug__:
@@ -1621,6 +1626,17 @@ class Option(Parameter):
 
         help = self.help or ''
         extra = []
+        if self.show_envvar:
+            envvar = self.envvar
+            if envvar is None:
+                if self.allow_from_autoenv and \
+                    ctx.auto_envvar_prefix is not None:
+                    envvar = '%s_%s' % (ctx.auto_envvar_prefix, self.name.upper())
+            if envvar is not None:
+              extra.append('env var: %s' % (
+                           ', '.join('%s' % d for d in envvar)
+                           if isinstance(envvar, (list, tuple))
+                           else envvar, ))
         if self.default is not None and self.show_default:
             extra.append('default: %s' % (
                          ', '.join('%s' % d for d in self.default)


Test patch:
diff --git a/tests/test_options.py b/tests/test_options.py
index d196fe2..d258ee3 100644
--- a/tests/test_options.py
+++ b/tests/test_options.py
@@ -198,6 +198,30 @@ def test_nargs_envvar(runner):
     assert result.output == 'x|1\ny|2\n'
 
 
+def test_show_envvar(runner):
+    @click.command()
+    @click.option('--arg1', envvar='ARG1',
+                  show_envvar=True)
+    def cmd(arg):
+        pass
+
+    result = runner.invoke(cmd, ['--help'])
+    assert not result.exception
+    assert 'ARG1' in result.output
+
+
+def test_show_envvar_auto_prefix(runner):
+    @click.command()
+    @click.option('--arg1', show_envvar=True)
+    def cmd(arg):
+        pass
+
+    result = runner.invoke(cmd, ['--help'],
+                           auto_envvar_prefix='TEST')
+    assert not result.exception
+    assert 'TEST_ARG1' in result.output
+
+
 def test_custom_validation(runner):
     def validate_pos_int(ctx, value):
         if value < 0: