Skip to content
Myr

why.rst

This file

Path

docs/why.rst

147 lines · 7.0 KiB

Last changed by

docs cleanupDavid Lordbefore adoption7817ae6

History of this file

Take it

1Why 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 box13*   support for prompting of custom values14*   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.)2021There 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.2425Click actually implements its own parsing of arguments and does not use26``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.3031Click 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.3536Too much customizability would break this promise.3738Click was written to support the `Flask <https://palletsprojects.com/p/flask/>`_39microframework ecosystem because no tool could provide it with the40functionality it needed.4142To get an understanding of what Click is all about, I strongly recommend43looking at the :ref:`complex-guide` chapter.4445Why not Argparse?46-----------------4748Click is internally based on ``optparse`` instead of ``argparse``. This49is an implementation detail that a user does not have to be concerned50with. Click is not based on argparse because it has some behaviors that51make handling arbitrary command line interfaces hard:5253*   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's57    ambitions of dispatching to subparsers.58*   argparse does not support disabling interspersed arguments. Without59    this feature, it's not possible to safely implement Click's nested60    parsing.6162Why not Docopt etc.?63--------------------6465Docopt, and many tools like it, are cool in how they work, but very few of66these 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 terminal76width, and it makes translations hard.  On top of that, docopt is restricted77to 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 does82support dispatching to subcommands, it, for instance, does not directly83support 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.93-   Click has strong information available for all parameters and commands,94    so it can generate unified help pages for the full CLI and95    assist the user in converting the input data as necessary.96-   Click has a strong understanding of what types are, and it can give the user97    consistent error messages if something goes wrong.  A subcommand98    written by a different developer will not suddenly die with a99    different error message because it's manually handled.100-   Click has enough meta information available for its whole program101    to evolve over time and improve the user experience without102    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.105106The aim of Click is to make composable systems. Whereas, the aim of docopt107is 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 to110achieve unified command line interfaces.  For instance, as a developer, you111are given very little choice in formatting your help pages.112113114Why 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 for123accepting an arbitrary number of arguments.  Due to syntactical ambiguities124on 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.  These142kinds of problems are hard to find, so Click does not attempt to be magical143about 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`.