Skip to content
Myr

why.rst

This file

Path

docs/why.rst

147 lines · 7.0 KiB

Last changed by

docs cleanupDavid Lordbefore adoption7817ae6

History of this file

Take it

The walk stopped after 200 commits: lines older than 430c50a point at it.

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

lines 1–10why, 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 restrictions

Merge pull request #1003 from dougharris/options-741Dan Sullybefore adoption7cc7434

line 11why, one step back
11* supports implementation of Unix/POSIX command line conventions

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

line 12why, one step back
12* supports loading values from environment variables out of the box

Merge pull request #1316 from strawstack/7.xDavid Lordbefore adoption7933849

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

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

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

Merge pull request #1316 from strawstack/7.xDavid Lordbefore adoption7933849

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

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

line 24why, one step back
24

superfluous word ;-)Matthias Urlichsbefore adoptionbe24463

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

Clarify optparse relationshipArmin Ronacherbefore adoption031f4e8

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

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

line 30why, one step back
30

Merge pull request #1316 from strawstack/7.xDavid Lordbefore adoption7933849

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

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

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

reformat changelog, update docs configDavid Lordbefore adoption02756dd

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

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

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

Merge pull request #1316 from strawstack/7.xDavid Lordbefore adoption7933849

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

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

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

docs cleanupDavid Lordbefore adoption7817ae6

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

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

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

docs cleanupDavid Lordbefore adoption7817ae6

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

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

line 52why, one step back
52

docs cleanupDavid Lordbefore adoption7817ae6

lines 53–56why, one step back
53* argparse has built-in behavior to guess if something is an
54 argument or an option. This becomes a problem when dealing with55 incomplete command lines; the behaviour becomes unpredictable56 without full knowledge of a command line. This goes against Click's

Merge pull request #1316 from strawstack/7.xDavid Lordbefore adoption7933849

line 57why, one step back
57 ambitions of dispatching to subparsers.

docs cleanupDavid Lordbefore adoption7817ae6

lines 58–60why, one step back
58* argparse does not support disabling interspersed arguments. Without
59 this feature, it's not possible to safely implement Click's nested60 parsing.

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

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

docs cleanupDavid Lordbefore adoption7817ae6

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

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

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

Merge pull request #1316 from strawstack/7.xDavid Lordbefore adoption7933849

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

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

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

Merge pull request #1316 from strawstack/7.xDavid Lordbefore adoption7933849

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

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

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

Merge pull request #1316 from strawstack/7.xDavid Lordbefore adoption7933849

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

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

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

Merge pull request #1316 from strawstack/7.xDavid Lordbefore adoption7933849

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

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

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

fixed minor typos (#1236)Andrea Tupinibefore adoption30ce8ff

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

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

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

Merge pull request #1316 from strawstack/7.xDavid Lordbefore adoption7933849

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

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

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

Merge pull request #1316 from strawstack/7.xDavid Lordbefore adoption7933849

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

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

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

Merge pull request #1316 from strawstack/7.xDavid Lordbefore adoption7933849

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

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

lines 112–122why, one step back
112
113114Why Hardcoded Behaviors?115------------------------116117The other question is why Click goes away from optparse and hardcodes118certain behaviors instead of staying configurable. There are multiple119reasons for this. The biggest one is that too much configurability makes120it hard to achieve a consistent command line experience.121122The best example for this is optparse's ``callback`` functionality for

fixed minor typos (#1236)Andrea Tupinibefore adoption30ce8ff

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

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

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

Merge pull request #1316 from strawstack/7.xDavid Lordbefore adoption7933849

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

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

lines 143–147why, one step back
143about this.
144145This sort of behavior however can be implemented on a higher level to146support things such as explicit aliases. For more information see147:ref:`aliases`.