Skip to content
Myr

Move auto_envvar_prefix section to context section docs.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Edward G · committed by Edward G · parent d79f0fb · Browse files · 2 files+51 added−50 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+51 added−50 removed

  • other:+51 added−50 removed

2 files+51 added−50 removed

modified:docs/commands-and-groups.rst+51 added
214 unmodified lines
215216Context Object217-------------------218The :class:`Context` object is how commands and groups communicate.219220Auto Envvar Prefix221--------------------222Automatically built environment variables are supported for options only. To enable this feature, the ``auto_envvar_prefix`` parameter needs to be passed to the script that is invoked. Each command and parameter is then added as an uppercase underscore-separated variable. If you have a subcommand223called ``run`` taking an option called ``reload`` and the prefix is ``WEB``, then the variable is ``WEB_RUN_RELOAD``.224225Example usage:226227.. click:example::228229 @click.command()230 @click.option('--username')231 def greet(username):232 click.echo(f'Hello {username}!')233234 if __name__ == '__main__':235 greet(auto_envvar_prefix='GREETER')236237And from the command line:238239.. click:run::240241 invoke(greet, env={'GREETER_USERNAME': 'john'},242 auto_envvar_prefix='GREETER')243244When using ``auto_envvar_prefix`` with command groups, the command name245needs to be included in the environment variable, between the prefix and246the parameter name, *i.e.* ``PREFIX_COMMAND_VARIABLE``. If you have a247subcommand called ``run-server`` taking an option called ``host`` and248the prefix is ``WEB``, then the variable is ``WEB_RUN_SERVER_HOST``.249250.. click:example::251252 @click.group()253 @click.option('--debug/--no-debug')254 def cli(debug):255 click.echo(f"Debug mode is {'on' if debug else 'off'}")256257 @cli.command()258 @click.option('--username')259 def greet(username):260 click.echo(f"Hello {username}!")261262 if __name__ == '__main__':263 cli(auto_envvar_prefix='GREETER')264265.. click:run::266267 invoke(cli, args=['greet',],268 env={'GREETER_GREET_USERNAME': 'John', 'GREETER_DEBUG': 'false'},269 auto_envvar_prefix='GREETER')
modified:docs/options.rst-50 removed
328 unmodified lines
329.. click:run::330331 invoke(greet, env={'ALT_USERNAME': 'Bill', 'USERNAME': 'john'})332333Auto Envvar Prefix334--------------------335Automatically built environment variables are supported for options only. To enable this feature, the ``auto_envvar_prefix`` parameter needs to be passed to the script that is invoked. Each command and parameter is then added as an uppercase underscore-separated variable. If you have a subcommand336called ``run`` taking an option called ``reload`` and the prefix is ``WEB``, then the variable is ``WEB_RUN_RELOAD``.333338Example usage:339340.. click:example::341342 @click.command()343 @click.option('--username')344 def greet(username):345 click.echo(f'Hello {username}!')346347 if __name__ == '__main__':348 greet(auto_envvar_prefix='GREETER')349350And from the command line:351352.. click:run::353354 invoke(greet, env={'GREETER_USERNAME': 'john'},355 auto_envvar_prefix='GREETER')356357When using ``auto_envvar_prefix`` with command groups, the command name358needs to be included in the environment variable, between the prefix and359the parameter name, *i.e.* ``PREFIX_COMMAND_VARIABLE``. If you have a360subcommand called ``run-server`` taking an option called ``host`` and361the prefix is ``WEB``, then the variable is ``WEB_RUN_SERVER_HOST``.362363.. click:example::364365 @click.group()366 @click.option('--debug/--no-debug')367 def cli(debug):368 click.echo(f"Debug mode is {'on' if debug else 'off'}")369370 @cli.command()371 @click.option('--username')372 def greet(username):373 click.echo(f"Hello {username}!")374375 if __name__ == '__main__':376 cli(auto_envvar_prefix='GREETER')377378.. click:run::379380 invoke(cli, args=['greet',],381 env={'GREETER_GREET_USERNAME': 'John', 'GREETER_DEBUG': 'false'},382 auto_envvar_prefix='GREETER')383334Multiple Options from Environment Values335-----------------------------------------336337As options can accept multiple values, pulling in such values from
74 unmodified lines