Skip to content

Commit 878de46

Browse files
authored
Move Click-Concepts doc from reST to MyST (#3260)
2 parents c8111c2 + 2be9c4c commit 878de46

2 files changed

Lines changed: 68 additions & 67 deletions

File tree

docs/click-concepts.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Click Concepts
2+
3+
This section covers concepts about Click's design.
4+
5+
```{contents}
6+
---
7+
depth: 1
8+
local: true
9+
---
10+
```
11+
12+
(callback-evaluation-order)=
13+
14+
## Callback Evaluation Order
15+
16+
Click works a bit differently than some other command line parsers in that
17+
it attempts to reconcile the order of arguments as defined by the
18+
programmer with the order of arguments as defined by the user before
19+
invoking any callbacks.
20+
21+
This is an important concept to understand when porting complex
22+
patterns to Click from optparse or other systems. A parameter
23+
callback invocation in optparse happens as part of the parsing step,
24+
whereas a callback invocation in Click happens after the parsing.
25+
26+
The main difference is that in optparse, callbacks are invoked with the raw
27+
value as it happens, whereas a callback in Click is invoked after the
28+
value has been fully converted.
29+
30+
Generally, the order of invocation is driven by the order in which the user
31+
provides the arguments to the script; if there is an option called `--foo`
32+
and an option called `--bar` and the user calls it as `--bar --foo`, then
33+
the callback for `bar` will fire before the one for `foo`.
34+
35+
There are three exceptions to this rule which are important to know:
36+
37+
Eagerness:
38+
> An option can be set to be "eager". All eager parameters are
39+
> evaluated before all non-eager parameters, but again in the order as
40+
> they were provided on the command line by the user.
41+
42+
> This is important for parameters that execute and exit like `--help`
43+
> and `--version`. Both are eager parameters, but whatever parameter
44+
> comes first on the command line will win and exit the program.
45+
46+
Repeated parameters:
47+
> If an option or argument is split up on the command line into multiple
48+
> places because it is repeated -- for instance, `--exclude foo --include
49+
> baz --exclude bar` -- the callback will fire based on the position of
50+
> the first option. In this case, the callback will fire for
51+
> `exclude` and it will be passed both options (`foo` and
52+
> `bar`), then the callback for `include` will fire with `baz`
53+
> only.
54+
55+
> Note that even if a parameter does not allow multiple versions, Click
56+
> will still accept the position of the first, but it will ignore every
57+
> value except the last. The reason for this is to allow composability
58+
> through shell aliases that set defaults.
59+
60+
Missing parameters:
61+
> If a parameter is not defined on the command line, the callback will
62+
> still fire. This is different from how it works in optparse where
63+
> undefined values do not fire the callback. Missing parameters fire
64+
> their callbacks at the very end which makes it possible for them to
65+
> default to values from a parameter that came before.
66+
67+
Most of the time you do not need to be concerned about any of this,
68+
but it is important to know how it works for some advanced cases.

docs/click-concepts.rst

Lines changed: 0 additions & 67 deletions
This file was deleted.

0 commit comments

Comments
 (0)