Move Option-decorators doc from reST to MyST (#3063)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Rowlando13 · committed by GitHub · parents 0a9c496, d18b564 · Browse files · 2 files·+86 added−84 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+86 added−84 removed
- other:+86 added−84 removed
Provenance
- Commit
29a68ca- Tree
9cde877- Author
Rowlando13 <67291205+Rowlando13@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent 1
0a9c496- Parent 2
d18b564
2 files changed+86 added−84 removed
added:docs/option-decorators.md+86 added
1+# Options Shortcut Decorators2+3+```{currentmodule} click4+```5+6+For convenience commonly used combinations of options arguments are available as their own decorators.7+8+```{contents}9+---10+depth: 211+local: true12+---13+```14+15+## Password Option16+17+Click supports hidden prompts and asking for confirmation. This is useful for password input:18+19+```{eval-rst}20+.. click:example::21+22+ import codecs23+24+ @click.command()25+ @click.option(26+ "--password", prompt=True, hide_input=True,27+ confirmation_prompt=True28+ )29+ def encode(password):30+ click.echo(f"encoded: {codecs.encode(password, 'rot13')}")31+32+.. click:run::33+34+ invoke(encode, input=['secret', 'secret'])35+```36+37+Because this combination of parameters is quite common, this can also be38+replaced with the {func}`password_option` decorator:39+40+```python41+ @click.command()42+ @click.password_option()43+ def encrypt(password):44+ click.echo(f"encoded: to {codecs.encode(password, 'rot13')}")45+```46+47+## Confirmation Option48+49+For dangerous operations, it's very useful to be able to ask a user for confirmation. This can be done by adding a50+boolean `--yes` flag and asking for confirmation if the user did not provide it and to fail in a callback:51+52+```{eval-rst}53+.. click:example::54+55+ def abort_if_false(ctx, param, value):56+ if not value:57+ ctx.abort()58+59+ @click.command()60+ @click.option('--yes', is_flag=True, callback=abort_if_false,61+ expose_value=False,62+ prompt='Are you sure you want to drop the db?')63+ def dropdb():64+ click.echo('Dropped all tables!')65+66+And what it looks like on the command line:67+68+.. click:run::69+70+ invoke(dropdb, input=['n'])71+ invoke(dropdb, args=['--yes'])72+73+Because this combination of parameters is quite common, this can also be74+replaced with the :func:`confirmation_option` decorator:75+76+.. click:example::77+78+ @click.command()79+ @click.confirmation_option(prompt='Are you sure you want to drop the db?')80+ def dropdb():81+ click.echo('Dropped all tables!')82+```83+84+## Version Option85+86+{func}`version_option` adds a `--version` option which immediately prints the version number and exits the program.deleted:docs/option-decorators.rst-84 removed
1-Options Shortcut Decorators2-===========================3-4-.. currentmodule:: click5-6-For convenience commonly used combinations of options arguments are available as their own decorators.7-8-.. contents::9- :depth: 210- :local:11-12-Password Option13-------------------14-15-Click supports hidden prompts and asking for confirmation. This is16-useful for password input:17-18-.. click:example::19-20- import codecs21-22- @click.command()23- @click.option(24- "--password", prompt=True, hide_input=True,25- confirmation_prompt=True26- )27- def encode(password):28- click.echo(f"encoded: {codecs.encode(password, 'rot13')}")29-30-.. click:run::31-32- invoke(encode, input=['secret', 'secret'])33-34-Because this combination of parameters is quite common, this can also be35-replaced with the :func:`password_option` decorator:36-37-.. code-block:: python38-39- @click.command()40- @click.password_option()41- def encrypt(password):42- click.echo(f"encoded: to {codecs.encode(password, 'rot13')}")43-44-Confirmation Option45---------------------46-47-For dangerous operations, it's very useful to be able to ask a user for48-confirmation. This can be done by adding a boolean ``--yes`` flag and49-asking for confirmation if the user did not provide it and to fail in a50-callback:51-52-.. click:example::53-54- def abort_if_false(ctx, param, value):55- if not value:56- ctx.abort()57-58- @click.command()59- @click.option('--yes', is_flag=True, callback=abort_if_false,60- expose_value=False,61- prompt='Are you sure you want to drop the db?')62- def dropdb():63- click.echo('Dropped all tables!')64-65-And what it looks like on the command line:66-67-.. click:run::68-69- invoke(dropdb, input=['n'])70- invoke(dropdb, args=['--yes'])71-72-Because this combination of parameters is quite common, this can also be73-replaced with the :func:`confirmation_option` decorator:74-75-.. click:example::76-77- @click.command()78- @click.confirmation_option(prompt='Are you sure you want to drop the db?')79- def dropdb():80- click.echo('Dropped all tables!')81-82-Version Option83-----------------84-:func:`version_option` adds a ``--version`` option which immediately prints the version number and exits the program.