Skip to content

Commit

Permalink
add functionality to also render env-variables that are created via t…
Browse files Browse the repository at this point in the history
…he 'auto_envvar_prefix' option
  • Loading branch information
Patrik.Hlobil authored and stephenfin committed Jul 4, 2022
1 parent fdea9dd commit 25516ad
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,6 @@ ChangeLog

# vim
*.swp

# Intellij
.idea
11 changes: 10 additions & 1 deletion sphinx_click/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
75 changes: 75 additions & 0 deletions tests/test_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)

0 comments on commit 25516ad

Please sign in to comment.