Skip to content
Myr

why.rst

This file

Path

docs/why.rst

152 lines · 7.3 KiB

Last changed by

Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde

History of this file

Take it

The walk stopped after 200 commits: lines older than 469ba25 point at it.

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

lines 1–3why, one step back
1Why Click?
2==========3

Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69

line 4why, one step back
4There are so many libraries out there for writing command line utilities;

Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde

line 5why, one step back
5why does Click exist?

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

lines 6–10why, one step back
6
7This 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 #113 from iKevinY/doc-fixesArmin Ronacherbefore adoptione0ea94a

line 11why, one step back
11* fully follows the Unix command line conventions

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

lines 12–14why, one step back
12* supports loading values from environment variables out of the box
13* supports for prompting of custom values14* is fully nestable and composable

Merge pull request #113 from iKevinY/doc-fixesArmin Ronacherbefore adoptione0ea94a

line 15why, one step back
15* works the same in Python 2 and 3

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

line 16why, one step back
16* supports file handling out of the box

Added a new item to the why listArmin Ronacherbefore adoption6eeead5

lines 17–19why, one step back
17* 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.)

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

line 20why, one step back
20

Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde

line 21why, one step back
21There are many alternatives to Click and you can have a look at them if

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

lines 22–24why, one step back
22you enjoy them better. The obvious ones are ``optparse`` and ``argparse``
23from the standard library.24

Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69

line 25why, one step back
25Click is actually implemented as a wrapper around a mild fork of

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

lines 26–31why, one step back
26``optparse`` and does not implement any parsing itself. The reason it's
27not based on ``argparse`` is that ``argparse`` does not allow proper28nesting of commands by design and has some deficiencies when it comes to29POSIX compliant argument handling.3031Click is designed to be fun to work with and at the same time not stand in

Merge pull request #125 from iKevinY/doc-fixesArmin Ronacherbefore adoption1d25afd

lines 32–33why, one step back
32your way. It's not overly flexible either. Currently, for instance, it
33does not allow you to customize the help pages too much. This is intentional

Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde

line 34why, one step back
34because Click is designed to allow you to nest command line utilities. The

Merge pull request #125 from iKevinY/doc-fixesArmin Ronacherbefore adoption1d25afd

line 35why, one step back
35idea is that you can have a system that works together with another system by

Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde

line 36why, one step back
36tacking two Click instances together and they will continue working as they

Merge pull request #125 from iKevinY/doc-fixesArmin Ronacherbefore adoption1d25afd

line 37why, one step back
37should.

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

lines 38–44why, one step back
38
39Too much customizability would break this promise.4041Click was written to support the `Flask <http://flask.pocoo.org/>`_42microframework ecosystem because no tool could provide it with the43functionality it needed.44

Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde

line 45why, one step back
45To get an understanding of what Click is all about, I strongly recommend

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

lines 46–52why, one step back
46looking at the :ref:`complex-guide` chapter to see what it's useful for.
4748Why not Argparse?49-----------------5051Click is internally based on optparse instead of argparse. This however52is an implementation detail that a user does not have to be concerned

Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde

line 53why, one step back
53with. The reason however Click is not using argparse is that it has some

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

lines 54–56why, one step back
54problematic behaviors that make handling arbitrary command line interfaces
55hard:56

Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69

line 57why, one step back
57* argparse has built-in magic behavior to guess if something is an

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

lines 58–60why, one step back
58 argument or an option. This becomes a problem when dealing with
59 incomplete command lines as it's not possible to know without having a60 full understanding of the command line how the parser is going to

Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde

line 61why, one step back
61 behave. This goes against Click's ambitions of dispatching to

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

lines 62–64why, one step back
62 subparsers.
63* argparse currently does not support disabling of interspearsed64 arguments. Without this feature it's not possible to safely implement

Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde

line 65why, one step back
65 Click's nested parsing nature.

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

lines 66–69why, one step back
66
67Why not Docopt etc.?68--------------------69

Merge pull request #125 from iKevinY/doc-fixesArmin Ronacherbefore adoption1d25afd

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

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

line 71why, one step back
71these tools deal with nesting of commands and composability in a way like

Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde

line 72why, one step back
72Click. To the best of the developer's knowledge, Click is the first

Merge pull request #125 from iKevinY/doc-fixesArmin Ronacherbefore adoption1d25afd

lines 73–74why, one step back
73Python library that aims to create a level of composability of applications
74that goes beyond what the system itself supports.

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

line 75why, one step back
75

Merge pull request #125 from iKevinY/doc-fixesArmin Ronacherbefore adoption1d25afd

line 76why, one step back
76Docopt, for instance, acts by parsing your help pages and then parsing

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

lines 77–78why, one step back
77according to those rules. The side effect of this is that docopt is quite
78rigid in how it handles the command line interface. The upside of docopt

Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69

lines 79–80why, one step back
79is that it gives you strong control over your help page; the downside is
80that due to this it cannot rewrap your output for the current terminal

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

lines 81–85why, one step back
81width and it makes translations hard. On top of that docopt is restricted
82to basic parsing. It does not handle argument dispatching and callback83invocation or types. This means there is a lot of code that needs to be84written in addition to the basic help page to handle the parsing results.85

Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69

line 86why, one step back
86Most of all, however, it makes composability hard. While docopt does

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

lines 87–90why, one step back
87support dispatching to subcommands, it for instance does not directly
88support any kind of automatic subcommand enumeration based on what's89available or it does not enforce subcommands to work in a consistent way.90

Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde

line 91why, one step back
91This is fine, but it's different from how Click wants to work. Click aims

Documentation fixesKevin Yapbefore adoption67523af

lines 92–93why, one step back
92to support fully composable command line user interfaces by doing the
93following:

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

line 94why, one step back
94

Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69

lines 95–96why, one step back
95- Click does not just parse, it also dispatches to the appropriate code.
96- Click has a strong concept of an invocation context that allows

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

line 97why, one step back
97 subcommands to respond to data from the parent command.

Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69

line 98why, one step back
98- Click has strong information available for all parameters and commands

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

lines 99–100why, one step back
99 so that it can generate unified help pages for the full CLI and to
100 assist the user in converting the input data as necessary.

Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69

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

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

lines 102–104why, one step back
102 consistent error messages if something goes wrong. A subcommand
103 written by a different developer will not suddenly die with a104 different error messsage because it's manually handled.

Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69

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

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

line 106why, one step back
106 that it can evolve over time to improve the user experience without

Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde

lines 107–108why, one step back
107 forcing developers to adjust their programs. For instance, if Click
108 decides to change how help pages are formatted, all Click programs

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

lines 109–110why, one step back
109 will automatically benefit from this.
110

Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde

line 111why, one step back
111The aim of Click is to make composable systems, whereas the aim of docopt

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

line 112why, one step back
112is to build the most beautiful and hand-crafted command line interfaces.

Documentation fixesKevin Yapbefore adoption67523af

line 113why, one step back
113These two goals conflict with one another in subtle ways. Click

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

lines 114–121why, one step back
114actively prevents people from implementing certain patterns in order to
115achieve unified command line interfaces. You have very little input on116reformatting your help pages for instance.117118119Why Hardcoded Behaviors?120------------------------121

Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde

line 122why, one step back
122The other question is why Click goes away from optparse and hardcodes

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

lines 123–128why, one step back
123certain behaviors instead of staying configurable. There are multiple
124reasons for this. The biggest one is that too much configurability makes125it hard to achieve a consistent command line experience.126127The best example for this is optparse's ``callback`` functionality for128accepting arbitrary number of arguments. Due to syntactical ambiguities

Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69

line 129why, one step back
129on the command line, there is no way to implement fully variadic arguments.

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

lines 130–131why, one step back
130There are always tradeoffs that need to be made and in case of
131``argparse`` these tradeoffs have been critical enough, that a system like

Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde

line 132why, one step back
132Click cannot even be implemented on top of it.

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

line 133why, one step back
133

Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde

line 134why, one step back
134In this particular case, Click attempts to stay with a handful of accepted

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

lines 135–141why, one step back
135paradigms for building command line interfaces that can be well documented
136and tested.137138139Why No Auto Correction?140-----------------------141

Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde

line 142why, one step back
142The question came up why Click does not auto correct parameters given that

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

lines 143–146why, one step back
143even optparse and argparse support automatic expansion of long arguments.
144The reason for this is that it's a liability for backwards compatibility.145If people start relying on automatically modified parameters and someone146adds a new parameter in the future, the script might stop working. These

Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde

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

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

lines 148–152why, one step back
148about this.
149150This sort of behavior however can be implemented on a higher level to151support things such as explicit aliases. For more information see152:ref:`aliases`.