why.rst
This file
Path
docs/why.rst
97 lines · 4.2 KiB
Last changed by
Added some more statements on argparse and how verbosity can be implemented in click.Armin Ronacherbefore adoption84fbcb2
History of this fileTake 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* fully follows the UNIX command line conventions12* supports loading values from environment variables out of the box13* supports for prompting of custom values14* is fully nestable and composable15* works the same on Python 2 and 316* supports file handling out of the box1718There are many alternatives to click and you can have a look at them if19you enjoy them better. The obvious ones are ``optparse`` and ``argparse``20from the standard library.2122click is actually implemented as a wrapper around ``optparse`` and does23not implement any parsing itself. The reason it's not based on24``argparse`` is that ``argparse`` does not allow proper nesting of25commands by design and has some deficiencies when it comes to POSIX26compliant argument handling.2728In case ``optparse`` ever gets deprecated and removed from Python,29``click`` will ship a version of it.3031Click is designed to be fun to work with and at the same time not stand in32your way. It's not overly flexible at the same time either. Currently33for instance it does not allow you to customize the help pages too much.34This is intentional because click is designed to allow you to nest command35line utilities. The idea is that you can have a system that works36together with another system by tacking two click instances together and37they will continue working as they should.3839Too much customizability would break this promise.4041Click was written to support the `Flask <http://flask.pocoo.org/>`_42microframework ecosystem because no tool could provide it with the43functionality it needed.4445To get an understanding of what click is all about I strongly recommend46looking at the :ref:`complex-guide` chapter to see what it's useful for.4748Why not Argparse?49-----------------5051Click is internally based on optparse instead of argparse. This however52is an implementation detail that a user does not have to concern himself53with. The reason however click is not using argparse is that it has some54problematic behaviors that make handling arbitrary command line interfaces55hard:5657* argparse has builtin magic behavior to guess if something is an58 argument or an option. This becomes a problem when dealing with59 incomplete command lines as it's not possible to know without having a60 full understanding of the command line how the parser is going to61 behave. This goes against click's ambitions of dispatching to62 subparsers.63* argparse currently does not support disabling of interspearsed64 arguments. Without this feature it's not possible to safely implement65 click's nested parsing nature.6667Why not Docopt etc.?68--------------------6970Docopt and many tools like it are cool in how they work but very few of71these tools deal with nesting of commands. To the best of the developer's72knowledge, click is the first Python library that tries to aim for73composability of applications that goes beyond what the system itself74supports.7576For instance a Click subcommand could accept all the arguments unparsed77and then be parsed with docopt if that command would want to work that78way.7980Why Hardcoded Behaviors?81------------------------8283The other question is why click goes away from optparse and hardcodes84certain behaviors instead of staying configurable. There are multiple85reasons for this. The biggest one is that too much configurability makes86it hard to achieve a consistent command line experience.8788The best example for this is optparse's ``callback`` functionality for89accepting arbitrary number of arguments. Due to syntactical ambuiguities90on the command line there is no way to implement fully variadic arguments.91There are always tradeoffs that need to be made and in case of92``argparse`` these tradeoffs have been critical enough, that a system like93click cannot even be implemented on top of it.9495In this particular case click attempts to stay with a handful of accepted96paradigms for building command line interfaces that can be well documented97and tested.