Skip to content

Commit 25516ad

Browse files
Patrik.Hlobilstephenfin
Patrik.Hlobil
authored andcommitted
add functionality to also render env-variables that are created via the 'auto_envvar_prefix' option
1 parent fdea9dd commit 25516ad

File tree

3 files changed

+88
-1
lines changed

3 files changed

+88
-1
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,6 @@ ChangeLog
5858

5959
# vim
6060
*.swp
61+
62+
# Intellij
63+
.idea

sphinx_click/ext.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,16 @@ def _format_envvar(
218218

219219
def _format_envvars(ctx: click.Context) -> ty.Generator[str, None, None]:
220220
"""Format all envvars for a `click.Command`."""
221-
params = [x for x in ctx.command.params if x.envvar]
221+
222+
auto_envvar_prefix = ctx.auto_envvar_prefix
223+
if auto_envvar_prefix is not None:
224+
params = []
225+
for param in ctx.command.params:
226+
if not param.envvar:
227+
param.envvar = f"{auto_envvar_prefix}_{param.name.upper()}"
228+
params.append(param)
229+
else:
230+
params = [x for x in ctx.command.params if x.envvar]
222231

223232
for param in params:
224233
yield '.. _{command_name}-{param_name}-{envvar}:'.format(

tests/test_formatter.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -940,3 +940,78 @@ def world():
940940
).lstrip(),
941941
'\n'.join(output),
942942
)
943+
944+
945+
class AutoEnvvarPrefixTestCase(unittest.TestCase):
946+
"""Validate ``click auto_envvar_prefix``-setup instances."""
947+
948+
def test_basics(self):
949+
"""Validate a click application with ``auto_envvar_prefix`` option enabled."""
950+
951+
@click.command(
952+
context_settings={"auto_envvar_prefix": "PREFIX"},
953+
)
954+
@click.option('--param', help='Help for param')
955+
@click.option('--other-param', help='Help for other-param')
956+
@click.option(
957+
'--param-with-explicit-envvar',
958+
help='Help for param-with-explicit-envvar',
959+
envvar="EXPLICIT_ENVVAR",
960+
)
961+
def cli_with_auto_envvars():
962+
"""A simple CLI with auto-env vars ."""
963+
964+
cli = cli_with_auto_envvars
965+
ctx = click.Context(cli, info_name='cli', auto_envvar_prefix="PREFIX")
966+
output = list(ext._format_command(ctx, nested='full'))
967+
968+
self.assertEqual(
969+
textwrap.dedent(
970+
"""
971+
A simple CLI with auto-env vars .
972+
973+
.. program:: cli
974+
.. code-block:: shell
975+
976+
cli [OPTIONS]
977+
978+
.. rubric:: Options
979+
980+
.. option:: --param <param>
981+
982+
Help for param
983+
984+
.. option:: --other-param <other_param>
985+
986+
Help for other-param
987+
988+
.. option:: --param-with-explicit-envvar <param_with_explicit_envvar>
989+
990+
Help for param-with-explicit-envvar
991+
992+
.. rubric:: Environment variables
993+
994+
.. _cli-param-PREFIX_PARAM:
995+
996+
.. envvar:: PREFIX_PARAM
997+
:noindex:
998+
999+
Provide a default for :option:`--param`
1000+
1001+
.. _cli-other_param-PREFIX_OTHER_PARAM:
1002+
1003+
.. envvar:: PREFIX_OTHER_PARAM
1004+
:noindex:
1005+
1006+
Provide a default for :option:`--other-param`
1007+
1008+
.. _cli-param_with_explicit_envvar-EXPLICIT_ENVVAR:
1009+
1010+
.. envvar:: EXPLICIT_ENVVAR
1011+
:noindex:
1012+
1013+
Provide a default for :option:`--param-with-explicit-envvar`
1014+
"""
1015+
).lstrip(),
1016+
'\n'.join(output),
1017+
)

0 commit comments

Comments
 (0)