Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: click-contrib/sphinx-click
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 4.2.0
Choose a base ref
...
head repository: click-contrib/sphinx-click
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 4.3.0
Choose a head ref
  • 3 commits
  • 4 files changed
  • 2 contributors

Commits on Jul 4, 2022

  1. add functionality to also render env-variables that are created via t…

    …he 'auto_envvar_prefix' option
    Patrik.Hlobil authored and stephenfin committed Jul 4, 2022
    Copy the full SHA
    25516ad View commit details
  2. Add release note for auto_envvar_prefix support

    Signed-off-by: Stephen Finucane <stephen@that.guru>
    Closes: #105
    stephenfin committed Jul 4, 2022
    Copy the full SHA
    089353f View commit details
  3. Fix typo

    Environmental variables aren't a thing.
    
    Signed-off-by: Stephen Finucane <stephen@that.guru>
    stephenfin committed Jul 4, 2022
    Copy the full SHA
    3ff77a7 View commit details
Showing with 100 additions and 1 deletion.
  1. +7 −0 .gitignore
  2. +8 −0 releasenotes/notes/add-auto_envvar_prefix-support-a08e68aba792ee26.yaml
  3. +10 −1 sphinx_click/ext.py
  4. +75 −0 tests/test_formatter.py
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -49,6 +49,10 @@ coverage.xml
# Sphinx documentation
docs/_build/

# reno
RELEASENOTES.rst
releasenotes/notes/reno.cache

# pbr
AUTHORS
ChangeLog
@@ -58,3 +62,6 @@ ChangeLog

# vim
*.swp

# Intellij
.idea
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
features:
- |
The ``auto_envvar_prefix`` parameter, which enables automatic environment
variables for all parameters in a click application, is now supported. For
more information on this feature, refer to the `click documentation`__.
.. __: https://click.palletsprojects.com/en/8.1.x/options/#values-from-environment-variables
11 changes: 10 additions & 1 deletion sphinx_click/ext.py
Original file line number Diff line number Diff line change
@@ -218,7 +218,16 @@ def _format_envvar(

def _format_envvars(ctx: click.Context) -> ty.Generator[str, None, None]:
"""Format all envvars for a `click.Command`."""
params = [x for x in ctx.command.params if x.envvar]

auto_envvar_prefix = ctx.auto_envvar_prefix
if auto_envvar_prefix is not None:
params = []
for param in ctx.command.params:
if not param.envvar:
param.envvar = f"{auto_envvar_prefix}_{param.name.upper()}"
params.append(param)
else:
params = [x for x in ctx.command.params if x.envvar]

for param in params:
yield '.. _{command_name}-{param_name}-{envvar}:'.format(
75 changes: 75 additions & 0 deletions tests/test_formatter.py
Original file line number Diff line number Diff line change
@@ -940,3 +940,78 @@ def world():
).lstrip(),
'\n'.join(output),
)


class AutoEnvvarPrefixTestCase(unittest.TestCase):
"""Validate ``click auto_envvar_prefix``-setup instances."""

def test_basics(self):
"""Validate a click application with ``auto_envvar_prefix`` option enabled."""

@click.command(
context_settings={"auto_envvar_prefix": "PREFIX"},
)
@click.option('--param', help='Help for param')
@click.option('--other-param', help='Help for other-param')
@click.option(
'--param-with-explicit-envvar',
help='Help for param-with-explicit-envvar',
envvar="EXPLICIT_ENVVAR",
)
def cli_with_auto_envvars():
"""A simple CLI with auto-env vars ."""

cli = cli_with_auto_envvars
ctx = click.Context(cli, info_name='cli', auto_envvar_prefix="PREFIX")
output = list(ext._format_command(ctx, nested='full'))

self.assertEqual(
textwrap.dedent(
"""
A simple CLI with auto-env vars .
.. program:: cli
.. code-block:: shell
cli [OPTIONS]
.. rubric:: Options
.. option:: --param <param>
Help for param
.. option:: --other-param <other_param>
Help for other-param
.. option:: --param-with-explicit-envvar <param_with_explicit_envvar>
Help for param-with-explicit-envvar
.. rubric:: Environment variables
.. _cli-param-PREFIX_PARAM:
.. envvar:: PREFIX_PARAM
:noindex:
Provide a default for :option:`--param`
.. _cli-other_param-PREFIX_OTHER_PARAM:
.. envvar:: PREFIX_OTHER_PARAM
:noindex:
Provide a default for :option:`--other-param`
.. _cli-param_with_explicit_envvar-EXPLICIT_ENVVAR:
.. envvar:: EXPLICIT_ENVVAR
:noindex:
Provide a default for :option:`--param-with-explicit-envvar`
"""
).lstrip(),
'\n'.join(output),
)