Skip to content
Myr

Increase readibility of 'why' page in docs

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Richard · committed by Richard Hayes · parent 224b4b2 · Browse files · 1 file+27 added−31 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+27 added−31 removed

  • other:+27 added−31 removed

1 file+27 added−31 removed

modified:docs/why.rst+27 added-31 removed
8 unmodified lines
910* is lazily composable without restrictions11* supports implementation of Unix/POSIX command line conventions12* supports loading values from environment variables out of the box13* supports for prompting of custom values13* 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 and you can have a look at them if22you enjoy them better. The obvious ones are ``optparse`` and ``argparse``23from the standard library.21There 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 to work with and at the same time not stand in32your way. It's not overly flexible either. Currently, for instance, it33does not allow you to customize the help pages too much. This is intentional34because Click is designed to allow you to nest command line utilities. The35idea is that you can have a system that works together with another system by36tacking two Click instances together and they will continue working as they37should.31Click 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 <http://flask.pocoo.org/>`_39microframework ecosystem because no tool could provide it with the40functionality it needed.4142To 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.43looking at the :ref:`complex-guide` chapter.4445Why not Argparse?46-----------------4751Click is internally based on optparse instead of argparse. This however48Click is internally based on ``optparse`` instead of ``argparse``. This49is an implementation detail that a user does not have to be concerned53with. The reason however Click is not using argparse is that it has some50with. The reason Click is not using argparse is it has some51problematic behaviors that make handling arbitrary command line interfaces52hard:5354* argparse has built-in 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.55 argument or an option. This behavior becomes a problem when dealing with56 incomplete command lines; the parsers behaviour becomes unpredictable57 without full knowledge of a command line. This goes against Click's58 ambitions of dispatching to subparsers.59* argparse currently does not support disabling of interspersed64 arguments. Without this feature it's not possible to safely implement60 arguments. Without this feature, it's not possible to safely implement61 Click's nested parsing nature.6263Why not Docopt etc.?64--------------------6570Docopt and many tools like it are cool in how they work, but very few of66Docopt, and many tools like, it are cool in how they work, but very few of67these tools deal with nesting of commands and composability in a way like68Click. To the best of the developer's knowledge, Click is the first69Python library that aims to create a level of composability of applications70that goes beyond what the system itself supports.
2 unmodified lines
73according to those rules. The side effect of this is that docopt is quite74rigid in how it handles the command line interface. The upside of docopt75is that it gives you strong control over your help page; the downside is76that due to this it cannot rewrap your output for the current terminal81width and it makes translations hard. On top of that docopt is restricted77width, and it makes translations hard. On top of that, docopt is restricted78to basic parsing. It does not handle argument dispatching and callback79invocation or types. This means there is a lot of code that needs to be80written in addition to the basic help page to handle the parsing results.8182Most of all, however, it makes composability hard. While docopt does87support dispatching to subcommands, it for instance does not directly83support dispatching to subcommands, it, for instance, does not directly84support any kind of automatic subcommand enumeration based on what's85available or it does not enforce subcommands to work in a consistent way.8687This is fine, but it's different from how Click wants to work. Click aims
2 unmodified lines
9091- Click does not just parse, it also dispatches to the appropriate code.92- Click has a strong concept of an invocation context that allows93 subcommands to respond to data from the parent command.98- Click has strong information available for all parameters and commands99 so that it can generate unified help pages for the full CLI and to94- Click has strong information available for all parameters and commands,95 so it can generate unified help pages for the full CLI and96 assist the user in converting the input data as necessary.101- Click has a strong understanding of what types are and can give the user97- Click has a strong understanding of what types are, and it can give the user98 consistent error messages if something goes wrong. A subcommand99 written by a different developer will not suddenly die with a100 different error message because it's manually handled.101- Click has enough meta information available for its whole program106 that it can evolve over time to improve the user experience without102 to evolve over time and improve the user experience without103 forcing developers to adjust their programs. For instance, if Click104 decides to change how help pages are formatted, all Click programs105 will automatically benefit from this.106111The aim of Click is to make composable systems, whereas the aim of docopt107The aim of Click is to make composable systems. Whereas, the aim of docopt108is to build the most beautiful and hand-crafted command line interfaces.109These two goals conflict with one another in subtle ways. Click110actively prevents people from implementing certain patterns in order to115achieve unified command line interfaces. You have very little input on116reformatting your help pages for instance.111achieve unified command line interfaces. For instance, as a developer, you112are given very little choice in formatting your help pages.113114115Why Hardcoded Behaviors?116------------------------
22 unmodified lines
139even optparse and argparse support automatic expansion of long arguments.140The reason for this is that it's a liability for backwards compatibility.141If people start relying on automatically modified parameters and someone142adds a new parameter in the future, the script might stop working. These147kinds of problems are hard to find so Click does not attempt to be magical143kinds of problems are hard to find, so Click does not attempt to be magical144about this.145146This sort of behavior however can be implemented on a higher level to147support things such as explicit aliases. For more information see
1 unmodified line