why.rst
This file
Path
docs/why.rst
146 lines · 7.0 KiB
Last changed by
Merge pull request #1855 from guifelix/patch-1David Lordbefore adoptionfd4b3d8
History of this fileTake it
The walk stopped after 200 commits: lines older than c3eac6b point at it.
Merge branch '7.x'David Lordbefore adoptionc3eac6b
lines 1–9why, 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:9Merge pull request #1855 from guifelix/patch-1David Lordbefore adoptionfd4b3d8
lines 10–16why, one step back10* Is lazily composable without restrictions.11* Supports implementation of Unix/POSIX command line conventions.12* Supports loading values from environment variables out of the box.13* Support for prompting of custom values.14* Is fully nestable and composable.15* Supports file handling out of the box.16* Comes with useful common helpers (getting terminal dimensions,Merge branch '7.x'David Lordbefore adoptionc3eac6b
line 17why, one step back17 ANSI colors, fetching direct keyboard input, screen clearing,Merge pull request #1855 from guifelix/patch-1David Lordbefore adoptionfd4b3d8
line 18why, one step back18 finding config paths, launching apps and editors, etc.).Merge branch '7.x'David Lordbefore adoptionc3eac6b
lines 19–48why, one step back1920There 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.2324Click actually implements its own parsing of arguments and does not use25``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.2930Click 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.3435Too much customizability would break this promise.3637Click was written to support the `Flask <https://palletsprojects.com/p/flask/>`_38microframework ecosystem because no tool could provide it with the39functionality it needed.4041To get an understanding of what Click is all about, I strongly recommend42looking at the :ref:`complex-guide` chapter.4344Why not Argparse?45-----------------4647Click is internally based on ``optparse`` instead of ``argparse``. This48is an implementation detail that a user does not have to be concernedMerge pull request #1855 from guifelix/patch-1David Lordbefore adoptionfd4b3d8
line 49why, one step back49with. Click is not based on ``argparse`` because it has some behaviors thatMerge branch '7.x'David Lordbefore adoptionc3eac6b
lines 50–51why, one step back50make handling arbitrary command line interfaces hard:51Merge pull request #1855 from guifelix/patch-1David Lordbefore adoptionfd4b3d8
line 52why, one step back52* ``argparse`` has built-in behavior to guess if something is anMerge branch '7.x'David Lordbefore adoptionc3eac6b
lines 53–56why, one step back53 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.Merge pull request #1855 from guifelix/patch-1David Lordbefore adoptionfd4b3d8
line 57why, one step back57* ``argparse`` does not support disabling interspersed arguments. WithoutMerge branch '7.x'David Lordbefore adoptionc3eac6b
lines 58–136why, one step back58 this feature, it's not possible to safely implement Click's nested59 parsing.6061Why not Docopt etc.?62--------------------6364Docopt, and many tools like it, are cool in how they work, but very few of65these tools deal with nesting of commands and composability in a way like66Click. 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 terminal75width, and it makes translations hard. On top of that, docopt is restricted76to basic parsing. It does not handle argument dispatching and callback77invocation 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 does81support dispatching to subcommands, it, for instance, does not directly82support any kind of automatic subcommand enumeration based on what's83available 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.92- Click has strong information available for all parameters and commands,93 so it can generate unified help pages for the full CLI and94 assist the user in converting the input data as necessary.95- Click has a strong understanding of what types are, and it can give the user96 consistent error messages if something goes wrong. A subcommand97 written by a different developer will not suddenly die with a98 different error message because it's manually handled.99- Click has enough meta information available for its whole program100 to evolve over time and improve the user experience without101 forcing developers to adjust their programs. For instance, if Click102 decides to change how help pages are formatted, all Click programs103 will automatically benefit from this.104105The aim of Click is to make composable systems. Whereas, the aim of docopt106is 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 to109achieve unified command line interfaces. For instance, as a developer, you110are given very little choice in formatting your help pages.111112113Why 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 for122accepting an arbitrary number of arguments. Due to syntactical ambiguities123on 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 thatMerge pull request #1855 from guifelix/patch-1David Lordbefore adoptionfd4b3d8
line 137why, one step back137even optparse and ``argparse`` support automatic expansion of long arguments.Merge branch '7.x'David Lordbefore adoptionc3eac6b
lines 138–146why, one step back138The 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. These141kinds of problems are hard to find, so Click does not attempt to be magical142about 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`.