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
Provenance
- Commit
ea3bb6f- Tree
84d7fe7- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
b905ebc- Parent 2
f7e6f25
1 file changed+36 added−27 removed
modified:docs/options.rst+36 added-27 removed
446 unmodified lines447 useful for password input:448 449 .. click:example::450 451+ import codecs452+453 @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')}")460 457-What it looks like:458-461 .. click:run::462 461- invoke(encrypt, input=['secret', 'secret'])463+ invoke(encode, input=['secret', 'secret'])464 465 Because this combination of parameters is quite common, this can also be466 replaced with the :func:`password_option` decorator:467 466-.. click:example::468+.. code-block:: python469 470 @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')}")474 475+476 Dynamic Defaults for Prompts477 ----------------------------478 479 The ``auto_envvar_prefix`` and ``default_map`` options for the context5 unmodified lines485 prompted if the option isn't specified on the command line, you can do so486 by supplying a callable as the default value. For example, to get a default487 from the environment:488 486-.. click:example::489+.. code-block:: python490 491+ import os492+493 @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}!")500 501 To describe what the default value will be, set it in ``show_default``.502 503 .. click:example::504 505+ import os506+507 @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}!")515 516 .. click:run::517 507- invoke(hello, args=['--help'])518+ invoke(hello, args=["--help"])519 520+521 Callbacks and Eager Options522 ---------------------------523 524 Sometimes, you want a parameter to completely change the execution flow.316 unmodified lines841 .. click:example::842 843 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'")849 850 @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)")855 844- if __name__ == '__main__':845- roll()846-856 And what it looks like:857 858 .. click:run::859 851- 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"])863 864 865 .. _optional-value:866 43 unmodified lines