You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Rewrite second half of options docs.
Sections moved:
- Callbacks and Eager Options from options to advanced
- Callbacks for Validation from options to advanced
- Auto Envvar Prefix from options to commands-and-groups
- Option prompts from options to prompts
- Optional prompts from options to prompts
- Dynamic Default for Prompts from options to prompts
Changes:
- Version option added
- Flag value rewritten
- Feature Switches merged into Flag Value
Copy file name to clipboardExpand all lines: docs/commands-and-groups.rst
+51Lines changed: 51 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -216,3 +216,54 @@ Most examples so far have attached the commands to a group immediately, but comm
216
216
Context Object
217
217
-------------------
218
218
The :class:`Context` object is how commands and groups communicate.
219
+
220
+
Auto Envvar Prefix
221
+
--------------------
222
+
Automatically 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 subcommand
223
+
called ``run`` taking an option called ``reload`` and the prefix is ``WEB``, then the variable is ``WEB_RUN_RELOAD``.
224
+
225
+
Example usage:
226
+
227
+
.. click:example::
228
+
229
+
@click.command()
230
+
@click.option('--username')
231
+
def greet(username):
232
+
click.echo(f'Hello {username}!')
233
+
234
+
if __name__ == '__main__':
235
+
greet(auto_envvar_prefix='GREETER')
236
+
237
+
And from the command line:
238
+
239
+
.. click:run::
240
+
241
+
invoke(greet, env={'GREETER_USERNAME': 'john'},
242
+
auto_envvar_prefix='GREETER')
243
+
244
+
When using ``auto_envvar_prefix`` with command groups, the command name
245
+
needs to be included in the environment variable, between the prefix and
246
+
the parameter name, *i.e.* ``PREFIX_COMMAND_VARIABLE``. If you have a
247
+
subcommand called ``run-server`` taking an option called ``host`` and
248
+
the prefix is ``WEB``, then the variable is ``WEB_RUN_SERVER_HOST``.
249
+
250
+
.. click:example::
251
+
252
+
@click.group()
253
+
@click.option('--debug/--no-debug')
254
+
def cli(debug):
255
+
click.echo(f"Debug mode is {'on' if debug else 'off'}")
0 commit comments