Skip to content
Myr

why.md

This file

Path

docs/why.md

106 lines · 6.8 KiB · Markdown

Last changed by

convert from reST to MySTdenuowebbefore adoptione57ba32

History of this file

Take it

convert from reST to MySTdenuowebbefore adoptione57ba32

line 1why, one step back
1# Why Click?

change extension to .mddenuowebbefore adoption79ba04f

line 2why, one step back
2

convert from reST to MySTdenuowebbefore adoptione57ba32

line 3why, one step back
3There are so many libraries out there for writing command line utilities; why does Click exist?

change extension to .mddenuowebbefore adoption79ba04f

line 4why, one step back
4

convert from reST to MySTdenuowebbefore adoptione57ba32

lines 5–6why, one step back
5This question is easy to answer: because there is not a single command line utility for Python out there which ticks the
6following boxes:

change extension to .mddenuowebbefore adoption79ba04f

line 7why, one step back
7

convert from reST to MySTdenuowebbefore adoptione57ba32

lines 8–15why, one step back
8- Is lazily composable without restrictions.
9- Supports implementation of Unix/POSIX command line conventions.10- Supports loading values from environment variables out of the box.11- Support for prompting of custom values.12- Is fully nestable and composable.13- Supports file handling out of the box.14- Comes with useful common helpers (getting terminal dimensions, ANSI colors, fetching direct keyboard input, screen15 clearing, finding config paths, launching apps and editors, etc.).

change extension to .mddenuowebbefore adoption79ba04f

line 16why, one step back
16

convert from reST to MySTdenuowebbefore adoptione57ba32

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

change extension to .mddenuowebbefore adoption79ba04f

line 19why, one step back
19

convert from reST to MySTdenuowebbefore adoptione57ba32

lines 20–22why, one step back
20Click actually implements its own parsing of arguments and does not use `optparse` or `argparse` following the
21`optparse` parsing behavior. The reason it's not based on `argparse` is that `argparse` does not allow proper nesting of22commands by design and has some deficiencies when it comes to POSIX compliant argument handling.

change extension to .mddenuowebbefore adoption79ba04f

line 23why, one step back
23

convert from reST to MySTdenuowebbefore adoptione57ba32

lines 24–26why, one step back
24Click is designed to be fun and customizable but not overly flexible. For instance, the customizability of help pages is
25constrained. This constraint is intentional because Click promises multiple Click instances will continue to function as26intended when strung together.

change extension to .mddenuowebbefore adoption79ba04f

lines 27–29why, one step back
27
28Too much customizability would break this promise.29

convert from reST to MySTdenuowebbefore adoptione57ba32

lines 30–31why, one step back
30Click was written to support the [Flask](https://palletsprojects.com/p/flask/) microframework ecosystem because no tool
31could provide it with the functionality it needed.

change extension to .mddenuowebbefore adoption79ba04f

line 32why, one step back
32

convert from reST to MySTdenuowebbefore adoptione57ba32

line 33why, one step back
33To get an understanding of what Click is all about, I strongly recommend looking at the {ref}`complex-guide` chapter.

change extension to .mddenuowebbefore adoption79ba04f

line 34why, one step back
34

convert from reST to MySTdenuowebbefore adoptione57ba32

line 35why, one step back
35## Why not Argparse?

change extension to .mddenuowebbefore adoption79ba04f

line 36why, one step back
36

convert from reST to MySTdenuowebbefore adoptione57ba32

lines 37–39why, one step back
37Click is internally based on `optparse` instead of `argparse`. This is an implementation detail that a user does not
38have to be concerned with. Click is not based on `argparse` because it has some behaviors that make handling arbitrary39command line interfaces hard:

change extension to .mddenuowebbefore adoption79ba04f

line 40why, one step back
40

convert from reST to MySTdenuowebbefore adoptione57ba32

lines 41–45why, one step back
41- `argparse` has built-in behavior to guess if something is an argument or an option. This becomes a problem when
42 dealing with incomplete command lines; the behaviour becomes unpredictable without full knowledge of a command line.43 This goes against Click's ambitions of dispatching to subparsers.44- `argparse` does not support disabling interspersed arguments. Without this feature, it's not possible to safely45 implement Click's nested parsing.

change extension to .mddenuowebbefore adoption79ba04f

line 46why, one step back
46

convert from reST to MySTdenuowebbefore adoptione57ba32

line 47why, one step back
47## Why not Docopt etc.?

change extension to .mddenuowebbefore adoption79ba04f

line 48why, one step back
48

convert from reST to MySTdenuowebbefore adoptione57ba32

lines 49–51why, one step back
49Docopt, and many tools like it, are cool in how they work, but very few of these tools deal with nesting of commands and
50composability in a way like Click. To the best of the developer's knowledge, Click is the first Python library that aims51to create a level of composability of applications that goes beyond what the system itself supports.

change extension to .mddenuowebbefore adoption79ba04f

line 52why, one step back
52

convert from reST to MySTdenuowebbefore adoptione57ba32

lines 53–58why, one step back
53Docopt, for instance, acts by parsing your help pages and then parsing according to those rules. The side effect of this
54is that docopt is quite rigid in how it handles the command line interface. The upside of docopt is that it gives you55strong control over your help page; the downside is that due to this it cannot rewrap your output for the current56terminal width, and it makes translations hard. On top of that, docopt is restricted to basic parsing. It does not57handle argument dispatching and callback invocation or types. This means there is a lot of code that needs to be written58in addition to the basic help page to handle the parsing results.

change extension to .mddenuowebbefore adoption79ba04f

line 59why, one step back
59

convert from reST to MySTdenuowebbefore adoptione57ba32

lines 60–62why, one step back
60Most of all, however, it makes composability hard. While docopt does support dispatching to subcommands, it, for
61instance, does not directly support any kind of automatic subcommand enumeration based on what's available or it does62not enforce subcommands to work in a consistent way.

change extension to .mddenuowebbefore adoption79ba04f

line 63why, one step back
63

convert from reST to MySTdenuowebbefore adoptione57ba32

lines 64–65why, one step back
64This is fine, but it's different from how Click wants to work. Click aims to support fully composable command line user
65interfaces by doing the following:

change extension to .mddenuowebbefore adoption79ba04f

line 66why, one step back
66

convert from reST to MySTdenuowebbefore adoptione57ba32

lines 67–77why, one step back
67- Click does not just parse, it also dispatches to the appropriate code.
68- Click has a strong concept of an invocation context that allows subcommands to respond to data from the parent69 command.70- Click has strong information available for all parameters and commands, so it can generate unified help pages for the71 full CLI and assist the user in converting the input data as necessary.72- Click has a strong understanding of what types are, and it can give the user consistent error messages if something73 goes wrong. A subcommand written by a different developer will not suddenly die with a different error message because74 it's manually handled.75- Click has enough meta information available for its whole program to evolve over time and improve the user experience76 without forcing developers to adjust their programs. For instance, if Click decides to change how help pages are77 formatted, all Click programs will automatically benefit from this.

change extension to .mddenuowebbefore adoption79ba04f

line 78why, one step back
78

convert from reST to MySTdenuowebbefore adoptione57ba32

lines 79–82why, one step back
79The aim of Click is to make composable systems. Whereas, the aim of docopt is to build the most beautiful and
80hand-crafted command line interfaces. These two goals conflict with one another in subtle ways. Click actively prevents81people from implementing certain patterns in order to achieve unified command line interfaces. For instance, as a82developer, you are given very little choice in formatting your help pages.

change extension to .mddenuowebbefore adoption79ba04f

line 83why, one step back
83

convert from reST to MySTdenuowebbefore adoptione57ba32

line 84why, one step back
84## Why Hardcoded Behaviors?

change extension to .mddenuowebbefore adoption79ba04f

line 85why, one step back
85

convert from reST to MySTdenuowebbefore adoptione57ba32

lines 86–88why, one step back
86The other question is why Click goes away from optparse and hardcodes certain behaviors instead of staying configurable.
87There are multiple reasons for this. The biggest one is that too much configurability makes it hard to achieve a88consistent command line experience.

change extension to .mddenuowebbefore adoption79ba04f

line 89why, one step back
89

convert from reST to MySTdenuowebbefore adoptione57ba32

lines 90–92why, one step back
90The best example for this is optparse's `callback` functionality for accepting an arbitrary number of arguments. Due to
91syntactical ambiguities on the command line, there is no way to implement fully variadic arguments. There are always92tradeoffs that need to be made and in case of `argparse` these tradeoffs have been critical enough, that a system like

change extension to .mddenuowebbefore adoption79ba04f

lines 93–94why, one step back
93Click cannot even be implemented on top of it.
94

convert from reST to MySTdenuowebbefore adoptione57ba32

lines 95–96why, one step back
95In this particular case, Click attempts to stay with a handful of accepted paradigms for building command line
96interfaces that can be well documented and tested.

change extension to .mddenuowebbefore adoption79ba04f

line 97why, one step back
97

convert from reST to MySTdenuowebbefore adoptione57ba32

line 98why, one step back
98## Why No Auto Correction?

change extension to .mddenuowebbefore adoption79ba04f

line 99why, one step back
99

convert from reST to MySTdenuowebbefore adoptione57ba32

lines 100–103why, one step back
100The question came up why Click does not auto correct parameters given that even optparse and `argparse` support
101automatic expansion of long arguments. The reason for this is that it's a liability for backwards compatibility. If102people start relying on automatically modified parameters and someone adds a new parameter in the future, the script103might stop working. These kinds of problems are hard to find, so Click does not attempt to be magical about this.

change extension to .mddenuowebbefore adoption79ba04f

line 104why, one step back
104

convert from reST to MySTdenuowebbefore adoptione57ba32

lines 105–106why, one step back
105This sort of behavior however can be implemented on a higher level to support things such as explicit aliases. For more
106information see {ref}`aliases`.