Skip to content
Myr

Merge pull request #325 from pfmoore/dynamic_default_doc

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

Document use of a callable for the default value of an option

authored by Markus Unterwaditzer · committed by Markus Unterwaditzer · parents 0d9731e, 14bdaf6 · Browse files · 1 file+21 added−0 removed
This commit

State

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

Diff stats

1 file+21 added−0 removed

  • other:+21 added−0 removed

1 file+21 added−0 removed

modified:docs/options.rst+21 added
328 unmodified lines
329 @click.password_option()330 def encrypt(password):331 click.echo('Encrypting password to %s' % password.encode('rot13'))332333Dynamic Defaults for Prompts334----------------------------335336The ``auto_envvar_prefix`` and ``default_map`` options for the context337allow the program to read option values from the environment or a338configuration file. However, this overrides the prompting mechanism, so339that the user does not get the option to change the value interactively.340341If you want to let the user configure the default value, but still be342prompted if the option isn't specified on the command line, you can do so343by supplying a callable as the default value. For example, to get a default344from the environment:345346.. click:example::347348 @click.command()349 @click.option('--username', prompt=True,350 default=lambda: os.environ.get('USER', ''))351 def hello(username):352 print("Hello,", username)353354Callbacks and Eager Options355---------------------------356357Sometimes, you want a parameter to completely change the execution flow.
315 unmodified lines