Skip to content
Myr

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

2 files+86 added−84 removed

added:docs/option-decorators.md+86 added
1# Options Shortcut Decorators23```{currentmodule} click4```56For convenience commonly used combinations of options arguments are available as their own decorators.78```{contents}9---10depth: 211local: true12---13```1415## Password Option1617Click supports hidden prompts and asking for confirmation. This is useful for password input:1819```{eval-rst}20.. click:example::2122 import codecs2324 @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')}")3132.. click:run::3334 invoke(encode, input=['secret', 'secret'])35```3637Because this combination of parameters is quite common, this can also be38replaced with the {func}`password_option` decorator:3940```python41 @click.command()42 @click.password_option()43 def encrypt(password):44 click.echo(f"encoded: to {codecs.encode(password, 'rot13')}")45```4647## Confirmation Option4849For dangerous operations, it's very useful to be able to ask a user for confirmation. This can be done by adding a50boolean `--yes` flag and asking for confirmation if the user did not provide it and to fail in a callback:5152```{eval-rst}53.. click:example::5455 def abort_if_false(ctx, param, value):56 if not value:57 ctx.abort()5859 @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!')6566And what it looks like on the command line:6768.. click:run::6970 invoke(dropdb, input=['n'])71 invoke(dropdb, args=['--yes'])7273Because this combination of parameters is quite common, this can also be74replaced with the :func:`confirmation_option` decorator:7576.. click:example::7778 @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```8384## Version Option8586{func}`version_option` adds a `--version` option which immediately prints the version number and exits the program.
deleted:docs/option-decorators.rst-84 removed
1Options Shortcut Decorators2===========================34.. currentmodule:: click56For convenience commonly used combinations of options arguments are available as their own decorators.78.. contents::9 :depth: 210 :local:1112Password Option13------------------1415Click supports hidden prompts and asking for confirmation. This is16useful for password input:1718.. click:example::1920 import codecs2122 @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')}")2930.. click:run::3132 invoke(encode, input=['secret', 'secret'])3334Because this combination of parameters is quite common, this can also be35replaced with the :func:`password_option` decorator:3637.. code-block:: python3839 @click.command()40 @click.password_option()41 def encrypt(password):42 click.echo(f"encoded: to {codecs.encode(password, 'rot13')}")4344Confirmation Option45--------------------4647For dangerous operations, it's very useful to be able to ask a user for48confirmation. This can be done by adding a boolean ``--yes`` flag and49asking for confirmation if the user did not provide it and to fail in a50callback:5152.. click:example::5354 def abort_if_false(ctx, param, value):55 if not value:56 ctx.abort()5758 @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!')6465And what it looks like on the command line:6667.. click:run::6869 invoke(dropdb, input=['n'])70 invoke(dropdb, args=['--yes'])7172Because this combination of parameters is quite common, this can also be73replaced with the :func:`confirmation_option` decorator:7475.. click:example::7677 @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!')8182Version Option83----------------84:func:`version_option` adds a ``--version`` option which immediately prints the version number and exits the program.