Skip to content
Myr

why.rst

This file

Path

docs/why.rst

146 lines · 7.0 KiB

Last changed by

Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9

History of this file

Take it

The walk stopped after 200 commits: lines older than 03e0133 point at it.

Follow PEP8 recommendation for param naming (#1073)Renne Rochabefore adoption03e0133

lines 1–12why, one step back
1Why Click?
2==========34There are so many libraries out there for writing command line utilities;5why does Click exist?67This question is easy to answer: because there is not a single command8line utility for Python out there which ticks the following boxes:910* is lazily composable without restrictions11* supports implementation of Unix/POSIX command line conventions12* supports loading values from environment variables out of the box

Merge branch '7.x'David Lordbefore adoption2269786

line 13why, one step back
13* support for prompting of custom values

Follow PEP8 recommendation for param naming (#1073)Renne Rochabefore adoption03e0133

lines 14–19why, one step back
14* is fully nestable and composable
15* supports file handling out of the box16* comes with useful common helpers (getting terminal dimensions,17 ANSI colors, fetching direct keyboard input, screen clearing,18 finding config paths, launching apps and editors, etc.)19

Merge branch '7.x'David Lordbefore adoption2269786

lines 20–22why, one step back
20There are many alternatives to Click; the obvious ones are ``optparse``
21and ``argparse`` from the standard library. Have a look to see if something22else resonates with you.

Follow PEP8 recommendation for param naming (#1073)Renne Rochabefore adoption03e0133

line 23why, one step back
23

Merge pull request #1190 from smurfix/upstream1David Lordbefore adoption7193876

line 24why, one step back
24Click actually implements its own parsing of arguments and does not use

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

lines 25–28why, one step back
25``optparse`` or ``argparse`` following the ``optparse`` parsing behavior.
26The reason it's not based on ``argparse`` is that ``argparse`` does not27allow proper nesting of commands by design and has some deficiencies when28it comes to POSIX compliant argument handling.

Follow PEP8 recommendation for param naming (#1073)Renne Rochabefore adoption03e0133

line 29why, one step back
29

Merge branch '7.x'David Lordbefore adoption2269786

lines 30–33why, one step back
30Click is designed to be fun and customizable but not overly flexible.
31For instance, the customizability of help pages is constrained. This32constraint is intentional because Click promises multiple Click instances33will continue to function as intended when strung together.

Follow PEP8 recommendation for param naming (#1073)Renne Rochabefore adoption03e0133

lines 34–36why, one step back
34
35Too much customizability would break this promise.36

Merge branch '7.x'David Lordbefore adoption11abf2b

line 37why, one step back
37Click was written to support the `Flask <https://palletsprojects.com/p/flask/>`_

Follow PEP8 recommendation for param naming (#1073)Renne Rochabefore adoption03e0133

lines 38–41why, one step back
38microframework ecosystem because no tool could provide it with the
39functionality it needed.4041To get an understanding of what Click is all about, I strongly recommend

Merge branch '7.x'David Lordbefore adoption2269786

line 42why, one step back
42looking at the :ref:`complex-guide` chapter.

Follow PEP8 recommendation for param naming (#1073)Renne Rochabefore adoption03e0133

lines 43–46why, one step back
43
44Why not Argparse?45-----------------46

Merge branch '7.x'David Lordbefore adoption2269786

line 47why, one step back
47Click is internally based on ``optparse`` instead of ``argparse``. This

Follow PEP8 recommendation for param naming (#1073)Renne Rochabefore adoption03e0133

line 48why, one step back
48is an implementation detail that a user does not have to be concerned

Merge branch '7.x'David Lordbefore adoption2269786

lines 49–50why, one step back
49with. Click is not based on argparse because it has some behaviors that
50make handling arbitrary command line interfaces hard:

Follow PEP8 recommendation for param naming (#1073)Renne Rochabefore adoption03e0133

line 51why, one step back
51

Merge branch '7.x'David Lordbefore adoption2269786

lines 52–59why, one step back
52* argparse has built-in behavior to guess if something is an
53 argument or an option. This becomes a problem when dealing with54 incomplete command lines; the behaviour becomes unpredictable55 without full knowledge of a command line. This goes against Click's56 ambitions of dispatching to subparsers.57* argparse does not support disabling interspersed arguments. Without58 this feature, it's not possible to safely implement Click's nested59 parsing.

Follow PEP8 recommendation for param naming (#1073)Renne Rochabefore adoption03e0133

lines 60–63why, one step back
60
61Why not Docopt etc.?62--------------------63

Merge branch '7.x'David Lordbefore adoption2269786

line 64why, one step back
64Docopt, and many tools like it, are cool in how they work, but very few of

Follow PEP8 recommendation for param naming (#1073)Renne Rochabefore adoption03e0133

lines 65–74why, one step back
65these tools deal with nesting of commands and composability in a way like
66Click. To the best of the developer's knowledge, Click is the first67Python library that aims to create a level of composability of applications68that goes beyond what the system itself supports.6970Docopt, for instance, acts by parsing your help pages and then parsing71according to those rules. The side effect of this is that docopt is quite72rigid in how it handles the command line interface. The upside of docopt73is that it gives you strong control over your help page; the downside is74that due to this it cannot rewrap your output for the current terminal

Merge branch '7.x'David Lordbefore adoption2269786

line 75why, one step back
75width, and it makes translations hard. On top of that, docopt is restricted

Follow PEP8 recommendation for param naming (#1073)Renne Rochabefore adoption03e0133

lines 76–80why, one step back
76to basic parsing. It does not handle argument dispatching and callback
77invocation or types. This means there is a lot of code that needs to be78written in addition to the basic help page to handle the parsing results.7980Most of all, however, it makes composability hard. While docopt does

Merge branch '7.x'David Lordbefore adoption2269786

line 81why, one step back
81support dispatching to subcommands, it, for instance, does not directly

Follow PEP8 recommendation for param naming (#1073)Renne Rochabefore adoption03e0133

lines 82–91why, one step back
82support any kind of automatic subcommand enumeration based on what's
83available or it does not enforce subcommands to work in a consistent way.8485This is fine, but it's different from how Click wants to work. Click aims86to support fully composable command line user interfaces by doing the87following:8889- Click does not just parse, it also dispatches to the appropriate code.90- Click has a strong concept of an invocation context that allows91 subcommands to respond to data from the parent command.

Merge branch '7.x'David Lordbefore adoption2269786

lines 92–93why, one step back
92- Click has strong information available for all parameters and commands,
93 so it can generate unified help pages for the full CLI and

Follow PEP8 recommendation for param naming (#1073)Renne Rochabefore adoption03e0133

line 94why, one step back
94 assist the user in converting the input data as necessary.

Merge branch '7.x'David Lordbefore adoption2269786

line 95why, one step back
95- Click has a strong understanding of what types are, and it can give the user

Follow PEP8 recommendation for param naming (#1073)Renne Rochabefore adoption03e0133

lines 96–97why, one step back
96 consistent error messages if something goes wrong. A subcommand
97 written by a different developer will not suddenly die with a

Merge pull request #1131 from bast/typoStephen Rosenbefore adoption2e856a5

line 98why, one step back
98 different error message because it's manually handled.

Follow PEP8 recommendation for param naming (#1073)Renne Rochabefore adoption03e0133

line 99why, one step back
99- Click has enough meta information available for its whole program

Merge branch '7.x'David Lordbefore adoption2269786

line 100why, one step back
100 to evolve over time and improve the user experience without

Follow PEP8 recommendation for param naming (#1073)Renne Rochabefore adoption03e0133

lines 101–104why, one step back
101 forcing developers to adjust their programs. For instance, if Click
102 decides to change how help pages are formatted, all Click programs103 will automatically benefit from this.104

Merge branch '7.x'David Lordbefore adoption2269786

line 105why, one step back
105The aim of Click is to make composable systems. Whereas, the aim of docopt

Follow PEP8 recommendation for param naming (#1073)Renne Rochabefore adoption03e0133

lines 106–108why, one step back
106is to build the most beautiful and hand-crafted command line interfaces.
107These two goals conflict with one another in subtle ways. Click108actively prevents people from implementing certain patterns in order to

Merge branch '7.x'David Lordbefore adoption2269786

lines 109–110why, one step back
109achieve unified command line interfaces. For instance, as a developer, you
110are given very little choice in formatting your help pages.

Follow PEP8 recommendation for param naming (#1073)Renne Rochabefore adoption03e0133

lines 111–121why, one step back
111
112113Why Hardcoded Behaviors?114------------------------115116The other question is why Click goes away from optparse and hardcodes117certain behaviors instead of staying configurable. There are multiple118reasons for this. The biggest one is that too much configurability makes119it hard to achieve a consistent command line experience.120121The best example for this is optparse's ``callback`` functionality for

Merge branch '7.x'David Lordbefore adoptiona438322

line 122why, one step back
122accepting an arbitrary number of arguments. Due to syntactical ambiguities

Follow PEP8 recommendation for param naming (#1073)Renne Rochabefore adoption03e0133

lines 123–140why, one step back
123on the command line, there is no way to implement fully variadic arguments.
124There are always tradeoffs that need to be made and in case of125``argparse`` these tradeoffs have been critical enough, that a system like126Click cannot even be implemented on top of it.127128In this particular case, Click attempts to stay with a handful of accepted129paradigms for building command line interfaces that can be well documented130and tested.131132133Why No Auto Correction?134-----------------------135136The question came up why Click does not auto correct parameters given that137even optparse and argparse support automatic expansion of long arguments.138The reason for this is that it's a liability for backwards compatibility.139If people start relying on automatically modified parameters and someone140adds a new parameter in the future, the script might stop working. These

Merge branch '7.x'David Lordbefore adoption2269786

line 141why, one step back
141kinds of problems are hard to find, so Click does not attempt to be magical

Follow PEP8 recommendation for param naming (#1073)Renne Rochabefore adoption03e0133

lines 142–146why, one step back
142about this.
143144This sort of behavior however can be implemented on a higher level to145support things such as explicit aliases. For more information see146:ref:`aliases`.