click-concepts.md
This file
Path
docs/click-concepts.md
68 lines · 2.8 KiB · Markdown
Last changed by
Move Click-Concepts doc from reST to MyST (#3260)Rowlando13before adoption878de46
History of this fileTake it
Move Click-Concepts doc from reST to MyST (#3260)Rowlando13before adoption878de46
lines 1–68why, one step back1# Click Concepts23This section covers concepts about Click's design.45```{contents}6---7depth: 18local: true9---10```1112(callback-evaluation-order)=1314## Callback Evaluation Order1516Click works a bit differently than some other command line parsers in that17it attempts to reconcile the order of arguments as defined by the18programmer with the order of arguments as defined by the user before19invoking any callbacks.2021This is an important concept to understand when porting complex22patterns to Click from optparse or other systems. A parameter23callback invocation in optparse happens as part of the parsing step,24whereas a callback invocation in Click happens after the parsing.2526The main difference is that in optparse, callbacks are invoked with the raw27value as it happens, whereas a callback in Click is invoked after the28value has been fully converted.2930Generally, the order of invocation is driven by the order in which the user31provides the arguments to the script; if there is an option called `--foo`32and an option called `--bar` and the user calls it as `--bar --foo`, then33the callback for `bar` will fire before the one for `foo`.3435There are three exceptions to this rule which are important to know:3637Eagerness:38> An option can be set to be "eager". All eager parameters are39> evaluated before all non-eager parameters, but again in the order as40> they were provided on the command line by the user.4142> This is important for parameters that execute and exit like `--help`43> and `--version`. Both are eager parameters, but whatever parameter44> comes first on the command line will win and exit the program.4546Repeated parameters:47> If an option or argument is split up on the command line into multiple48> places because it is repeated -- for instance, `--exclude foo --include49> baz --exclude bar` -- the callback will fire based on the position of50> the first option. In this case, the callback will fire for51> `exclude` and it will be passed both options (`foo` and52> `bar`), then the callback for `include` will fire with `baz`53> only.5455> Note that even if a parameter does not allow multiple versions, Click56> will still accept the position of the first, but it will ignore every57> value except the last. The reason for this is to allow composability58> through shell aliases that set defaults.5960Missing parameters:61> If a parameter is not defined on the command line, the callback will62> still fire. This is different from how it works in optparse where63> undefined values do not fire the callback. Missing parameters fire64> their callbacks at the very end which makes it possible for them to65> default to values from a parameter that came before.6667Most of the time you do not need to be concerned about any of this,68but it is important to know how it works for some advanced cases.