Skip to content
Myr

Merge pull request #1783 from pallets/fix-docs-examples

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

fix some examples in options docs

authored by David Lord · committed by GitHub · parents b905ebc, f7e6f25 · Browse files · 1 file+36 added−27 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+36 added−27 removed

  • other:+36 added−27 removed

1 file+36 added−27 removed

modified:docs/options.rst+36 added-27 removed
446 unmodified lines
447useful for password input:448449.. click:example::450451 import codecs452453 @click.command()452 @click.option('--password', prompt=True, hide_input=True,453 confirmation_prompt=True)454 def encrypt(password):455 click.echo(f"Encrypting password to {password.encode('rot13')}")454 @click.option(455 "--password", prompt=True, hide_input=True,456 confirmation_prompt=True457 )458 def encode(password):459 click.echo(f"encoded: {codecs.encode(password, 'rot13')}")460457What it looks like:458461.. click:run::462461 invoke(encrypt, input=['secret', 'secret'])463 invoke(encode, input=['secret', 'secret'])464465Because this combination of parameters is quite common, this can also be466replaced with the :func:`password_option` decorator:467466.. click:example::468.. code-block:: python469470 @click.command()471 @click.password_option()472 def encrypt(password):471 click.echo(f"Encrypting password to {password.encode('rot13')}")473 click.echo(f"encoded: to {codecs.encode(password, 'rot13')}")474475476Dynamic Defaults for Prompts477----------------------------478479The ``auto_envvar_prefix`` and ``default_map`` options for the context
5 unmodified lines
485prompted if the option isn't specified on the command line, you can do so486by supplying a callable as the default value. For example, to get a default487from the environment:488486.. click:example::489.. code-block:: python490491 import os492493 @click.command()489 @click.option('--username', prompt=True,490 default=lambda: os.environ.get('USER', ''))494 @click.option(495 "--username", prompt=True,496 default=lambda: os.environ.get("USER", "")497 )498 def hello(username):492 print("Hello,", username)499 click.echo(f"Hello, {username}!")500501To describe what the default value will be, set it in ``show_default``.502503.. click:example::504505 import os506507 @click.command()499 @click.option('--username', prompt=True,500 default=lambda: os.environ.get('USER', ''),501 show_default='current user')508 @click.option(509 "--username", prompt=True,510 default=lambda: os.environ.get("USER", ""),511 show_default="current user"512 )513 def hello(username):503 print("Hello,", username)514 click.echo(f"Hello, {username}!")515516.. click:run::517507 invoke(hello, args=['--help'])518 invoke(hello, args=["--help"])519520521Callbacks and Eager Options522---------------------------523524Sometimes, you want a parameter to completely change the execution flow.
316 unmodified lines
841.. click:example::842843 def validate_rolls(ctx, param, value):844 try:833 rolls, dice = map(int, value.split('d', 2))834 return (dice, rolls)845 rolls, _, dice = value.partition("d")846 return int(dice), int(rolls)847 except ValueError:836 raise click.BadParameter('rolls need to be in format NdM')848 raise click.BadParameter("format must be 'NdM'")849850 @click.command()839 @click.option('--rolls', callback=validate_rolls, default='1d6')851 @click.option("--rolls", callback=validate_rolls, default="1d6")852 def roll(rolls):853 sides, times = rolls854 click.echo(f"Rolling a {sides}-sided dice {times} time(s)")855844 if __name__ == '__main__':845 roll()846856And what it looks like:857858.. click:run::859851 invoke(roll, args=['--rolls=42'])860 invoke(roll, args=["--rolls=42"])861 println()853 invoke(roll, args=['--rolls=2d12'])862 invoke(roll, args=["--rolls=2d12"])863864865.. _optional-value:866
43 unmodified lines