why.rst
This file
Path
docs/why.rst
147 lines · 7.0 KiB
Last changed by
docs cleanupDavid Lordbefore adoption7817ae6
History of this fileTake 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 back1Why 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 restrictionsMerge pull request #1003 from dougharris/options-741Dan Sullybefore adoption7cc7434
line 11why, one step back11* supports implementation of Unix/POSIX command line conventionsMerge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a
line 12why, one step back12* supports loading values from environment variables out of the boxMerge pull request #1316 from strawstack/7.xDavid Lordbefore adoption7933849
line 13why, one step back13* support for prompting of custom valuesMerge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a
lines 14–20why, one step back14* is fully nestable and composable15* 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.)20Merge pull request #1316 from strawstack/7.xDavid Lordbefore adoption7933849
lines 21–23why, one step back21There 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 back24superfluous word ;-)Matthias Urlichsbefore adoptionbe24463
line 25why, one step back25Click actually implements its own parsing of arguments and does not useClarify optparse relationshipArmin Ronacherbefore adoption031f4e8
lines 26–29why, one step back26``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 back30Merge pull request #1316 from strawstack/7.xDavid Lordbefore adoption7933849
lines 31–34why, one step back31Click 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 back3536Too much customizability would break this promise.37reformat changelog, update docs configDavid Lordbefore adoption02756dd
line 38why, one step back38Click 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 back39microframework ecosystem because no tool could provide it with the40functionality it needed.4142To get an understanding of what Click is all about, I strongly recommendMerge pull request #1316 from strawstack/7.xDavid Lordbefore adoption7933849
line 43why, one step back43looking at the :ref:`complex-guide` chapter.Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a
lines 44–47why, one step back4445Why not Argparse?46-----------------47docs cleanupDavid Lordbefore adoption7817ae6
line 48why, one step back48Click is internally based on ``optparse`` instead of ``argparse``. ThisMerge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a
line 49why, one step back49is an implementation detail that a user does not have to be concerneddocs cleanupDavid Lordbefore adoption7817ae6
lines 50–51why, one step back50with. Click is not based on argparse because it has some behaviors that51make handling arbitrary command line interfaces hard:Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a
line 52why, one step back52docs cleanupDavid Lordbefore adoption7817ae6
lines 53–56why, one step back53* argparse has built-in behavior to guess if something is an54 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'sMerge pull request #1316 from strawstack/7.xDavid Lordbefore adoption7933849
line 57why, one step back57 ambitions of dispatching to subparsers.docs cleanupDavid Lordbefore adoption7817ae6
lines 58–60why, one step back58* argparse does not support disabling interspersed arguments. Without59 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 back6162Why not Docopt etc.?63--------------------64docs cleanupDavid Lordbefore adoption7817ae6
line 65why, one step back65Docopt, and many tools like it, are cool in how they work, but very few ofMerge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a
lines 66–75why, one step back66these tools deal with nesting of commands and composability in a way like67Click. 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 terminalMerge pull request #1316 from strawstack/7.xDavid Lordbefore adoption7933849
line 76why, one step back76width, and it makes translations hard. On top of that, docopt is restrictedMerge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a
lines 77–81why, one step back77to basic parsing. It does not handle argument dispatching and callback78invocation 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 doesMerge pull request #1316 from strawstack/7.xDavid Lordbefore adoption7933849
line 82why, one step back82support dispatching to subcommands, it, for instance, does not directlyMerge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a
lines 83–92why, one step back83support any kind of automatic subcommand enumeration based on what's84available 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 back93- Click has strong information available for all parameters and commands,94 so it can generate unified help pages for the full CLI andMerge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a
line 95why, one step back95 assist the user in converting the input data as necessary.Merge pull request #1316 from strawstack/7.xDavid Lordbefore adoption7933849
line 96why, one step back96- Click has a strong understanding of what types are, and it can give the userMerge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a
lines 97–98why, one step back97 consistent error messages if something goes wrong. A subcommand98 written by a different developer will not suddenly die with afixed minor typos (#1236)Andrea Tupinibefore adoption30ce8ff
line 99why, one step back99 different error message because it's manually handled.Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a
line 100why, one step back100- Click has enough meta information available for its whole programMerge pull request #1316 from strawstack/7.xDavid Lordbefore adoption7933849
line 101why, one step back101 to evolve over time and improve the user experience withoutMerge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a
lines 102–105why, one step back102 forcing developers to adjust their programs. For instance, if Click103 decides to change how help pages are formatted, all Click programs104 will automatically benefit from this.105Merge pull request #1316 from strawstack/7.xDavid Lordbefore adoption7933849
line 106why, one step back106The aim of Click is to make composable systems. Whereas, the aim of docoptMerge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a
lines 107–109why, one step back107is 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 toMerge pull request #1316 from strawstack/7.xDavid Lordbefore adoption7933849
lines 110–111why, one step back110achieve unified command line interfaces. For instance, as a developer, you111are given very little choice in formatting your help pages.Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a
lines 112–122why, one step back112113114Why 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 forfixed minor typos (#1236)Andrea Tupinibefore adoption30ce8ff
line 123why, one step back123accepting an arbitrary number of arguments. Due to syntactical ambiguitiesMerge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a
lines 124–141why, one step back124on 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. TheseMerge pull request #1316 from strawstack/7.xDavid Lordbefore adoption7933849
line 142why, one step back142kinds of problems are hard to find, so Click does not attempt to be magicalMerge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a
lines 143–147why, one step back143about 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`.