why.rst
This file
Path
docs/why.rst
146 lines · 7.0 KiB
Last changed by
Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
History of this fileTake it
The walk stopped after 200 commits: lines older than 03e0133 point at it.
Follow PEP8 recommendation for param naming (#1073)Renne Rochabefore adoption03e0133
lines 1–12why, 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 restrictions11* supports implementation of Unix/POSIX command line conventions12* supports loading values from environment variables out of the boxMerge branch '7.x'David Lordbefore adoption2269786
line 13why, one step back13* support for prompting of custom valuesFollow PEP8 recommendation for param naming (#1073)Renne Rochabefore adoption03e0133
lines 14–19why, one step back14* is fully nestable and composable15* supports file handling out of the box16* comes with useful common helpers (getting terminal dimensions,17 ANSI colors, fetching direct keyboard input, screen clearing,18 finding config paths, launching apps and editors, etc.)19Merge branch '7.x'David Lordbefore adoption2269786
lines 20–22why, one step back20There 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.Follow PEP8 recommendation for param naming (#1073)Renne Rochabefore adoption03e0133
line 23why, one step back23Merge pull request #1190 from smurfix/upstream1David Lordbefore adoption7193876
line 24why, one step back24Click actually implements its own parsing of arguments and does not useClarify optparse relationshipArmin Ronacherbefore adoption3ce663c
lines 25–28why, one step back25``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.Follow PEP8 recommendation for param naming (#1073)Renne Rochabefore adoption03e0133
line 29why, one step back29Merge branch '7.x'David Lordbefore adoption2269786
lines 30–33why, one step back30Click 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.Follow PEP8 recommendation for param naming (#1073)Renne Rochabefore adoption03e0133
lines 34–36why, one step back3435Too much customizability would break this promise.36Merge branch '7.x'David Lordbefore adoption11abf2b
line 37why, one step back37Click was written to support the `Flask <https://palletsprojects.com/p/flask/>`_Follow PEP8 recommendation for param naming (#1073)Renne Rochabefore adoption03e0133
lines 38–41why, one step back38microframework ecosystem because no tool could provide it with the39functionality it needed.4041To get an understanding of what Click is all about, I strongly recommendMerge branch '7.x'David Lordbefore adoption2269786
line 42why, one step back42looking at the :ref:`complex-guide` chapter.Follow PEP8 recommendation for param naming (#1073)Renne Rochabefore adoption03e0133
lines 43–46why, one step back4344Why not Argparse?45-----------------46Merge branch '7.x'David Lordbefore adoption2269786
line 47why, one step back47Click is internally based on ``optparse`` instead of ``argparse``. ThisFollow PEP8 recommendation for param naming (#1073)Renne Rochabefore adoption03e0133
line 48why, one step back48is an implementation detail that a user does not have to be concernedMerge branch '7.x'David Lordbefore adoption2269786
lines 49–50why, one step back49with. Click is not based on argparse because it has some behaviors that50make handling arbitrary command line interfaces hard:Follow PEP8 recommendation for param naming (#1073)Renne Rochabefore adoption03e0133
line 51why, one step back51Merge branch '7.x'David Lordbefore adoption2269786
lines 52–59why, one step back52* argparse has built-in behavior to guess if something is an53 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.57* argparse does not support disabling interspersed arguments. Without58 this feature, it's not possible to safely implement Click's nested59 parsing.Follow PEP8 recommendation for param naming (#1073)Renne Rochabefore adoption03e0133
lines 60–63why, one step back6061Why not Docopt etc.?62--------------------63Merge branch '7.x'David Lordbefore adoption2269786
line 64why, one step back64Docopt, and many tools like it, are cool in how they work, but very few ofFollow PEP8 recommendation for param naming (#1073)Renne Rochabefore adoption03e0133
lines 65–74why, one step back65these 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 terminalMerge branch '7.x'David Lordbefore adoption2269786
line 75why, one step back75width, and it makes translations hard. On top of that, docopt is restrictedFollow PEP8 recommendation for param naming (#1073)Renne Rochabefore adoption03e0133
lines 76–80why, one step back76to 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 doesMerge branch '7.x'David Lordbefore adoption2269786
line 81why, one step back81support dispatching to subcommands, it, for instance, does not directlyFollow PEP8 recommendation for param naming (#1073)Renne Rochabefore adoption03e0133
lines 82–91why, one step back82support 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.Merge branch '7.x'David Lordbefore adoption2269786
lines 92–93why, one step back92- Click has strong information available for all parameters and commands,93 so it can generate unified help pages for the full CLI andFollow PEP8 recommendation for param naming (#1073)Renne Rochabefore adoption03e0133
line 94why, one step back94 assist the user in converting the input data as necessary.Merge branch '7.x'David Lordbefore adoption2269786
line 95why, one step back95- Click has a strong understanding of what types are, and it can give the userFollow PEP8 recommendation for param naming (#1073)Renne Rochabefore adoption03e0133
lines 96–97why, one step back96 consistent error messages if something goes wrong. A subcommand97 written by a different developer will not suddenly die with aMerge pull request #1131 from bast/typoStephen Rosenbefore adoption2e856a5
line 98why, one step back98 different error message because it's manually handled.Follow PEP8 recommendation for param naming (#1073)Renne Rochabefore adoption03e0133
line 99why, one step back99- Click has enough meta information available for its whole programMerge branch '7.x'David Lordbefore adoption2269786
line 100why, one step back100 to evolve over time and improve the user experience withoutFollow PEP8 recommendation for param naming (#1073)Renne Rochabefore adoption03e0133
lines 101–104why, one step back101 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.104Merge branch '7.x'David Lordbefore adoption2269786
line 105why, one step back105The aim of Click is to make composable systems. Whereas, the aim of docoptFollow PEP8 recommendation for param naming (#1073)Renne Rochabefore adoption03e0133
lines 106–108why, one step back106is 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 toMerge branch '7.x'David Lordbefore adoption2269786
lines 109–110why, one step back109achieve unified command line interfaces. For instance, as a developer, you110are given very little choice in formatting your help pages.Follow PEP8 recommendation for param naming (#1073)Renne Rochabefore adoption03e0133
lines 111–121why, one step back111112113Why 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 forMerge branch '7.x'David Lordbefore adoptiona438322
line 122why, one step back122accepting an arbitrary number of arguments. Due to syntactical ambiguitiesFollow PEP8 recommendation for param naming (#1073)Renne Rochabefore adoption03e0133
lines 123–140why, one step back123on 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 that137even optparse and argparse support automatic expansion of long arguments.138The 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. TheseMerge branch '7.x'David Lordbefore adoption2269786
line 141why, one step back141kinds of problems are hard to find, so Click does not attempt to be magicalFollow PEP8 recommendation for param naming (#1073)Renne Rochabefore adoption03e0133
lines 142–146why, one step back142about 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`.