Skip to content

Commit 82089af

Browse files
Rewrite first half of options docs (pallets#2825)
Co-authored-by: Andreas Backx <[email protected]>
1 parent 4ece17a commit 82089af

7 files changed

+419
-399
lines changed

docs/api.rst

+1
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ Context
113113
:members:
114114
:member-order: bysource
115115

116+
.. _click-api-types:
116117

117118
Types
118119
-----

docs/documentation.rst

+36
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,42 @@ Or more explicitly:
108108
109109
invoke(touch, args=['--help'])
110110

111+
112+
Showing Defaults
113+
---------------------------
114+
To control the appearance of defaults pass ``show_default``.
115+
116+
.. click:example::
117+
118+
@click.command()
119+
@click.option('--n', default=1, show_default=False, help='number of dots')
120+
def dots(n):
121+
click.echo('.' * n)
122+
123+
.. click:run::
124+
125+
invoke(dots, args=['--help'])
126+
127+
For single option boolean flags, the default remains hidden if the default value is False even if show default is true.
128+
129+
.. click:example::
130+
131+
@click.command()
132+
@click.option('--n', default=1, show_default=True)
133+
@click.option("--gr", is_flag=True, show_default=True, default=False, help="Greet the world.")
134+
@click.option("--br", is_flag=True, show_default=True, default=True, help="Add a thematic break")
135+
def dots(n, gr, br):
136+
if gr:
137+
click.echo('Hello world!')
138+
click.echo('.' * n)
139+
if br:
140+
click.echo('-' * n)
141+
142+
.. click:run::
143+
144+
invoke(dots, args=['--help'])
145+
146+
111147
Click's Wrapping Behavior
112148
----------------------------
113149
Click's default wrapping ignores single new lines and rewraps the text based on the width of the terminal, to a maximum of 80 characters. In the example notice how the second grouping of three lines is rewrapped into a single paragraph.

docs/index.rst

+3-1
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,10 @@ usage patterns.
7272
virtualenv
7373
setuptools
7474
parameters
75-
arguments
75+
parameter-types
7676
options
77+
option-decorators
78+
arguments
7779
commands
7880
documentation
7981
prompts

docs/option-decorators.rst

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
Options Shortcut Decorators
2+
===========================
3+
4+
.. currentmodule:: click
5+
6+
For convenience commonly used combinations of options arguments are available as their own decorators.
7+
8+
.. contents::
9+
:depth: 2
10+
:local:
11+
12+
Password Option
13+
------------------
14+
15+
Click supports hidden prompts and asking for confirmation. This is
16+
useful for password input:
17+
18+
.. click:example::
19+
20+
import codecs
21+
22+
@click.command()
23+
@click.option(
24+
"--password", prompt=True, hide_input=True,
25+
confirmation_prompt=True
26+
)
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 be
35+
replaced with the :func:`password_option` decorator:
36+
37+
.. code-block:: python
38+
39+
@click.command()
40+
@click.password_option()
41+
def encrypt(password):
42+
click.echo(f"encoded: to {codecs.encode(password, 'rot13')}")
43+
44+
Confirmation Option
45+
--------------------
46+
47+
For dangerous operations, it's very useful to be able to ask a user for
48+
confirmation. This can be done by adding a boolean ``--yes`` flag and
49+
asking for confirmation if the user did not provide it and to fail in a
50+
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 be
73+
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!')

0 commit comments

Comments
 (0)