why.rst
This file
Path
docs/why.rst
148 lines · 7.1 KiB
Last changed by
Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69
History of this fileTake it
The walk stopped after 200 commits: lines older than 09d219d point at it.
Added travis configArmin Ronacherbefore adoption09d219d
lines 1–3why, one step back1Why Click?2==========3Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69
line 4why, one step back4There are so many libraries out there for writing command line utilities;Added travis configArmin Ronacherbefore adoption09d219d
lines 5–21why, one step back5why 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.21Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69
line 22why, one step back22Click is actually implemented as a wrapper around a mild fork ofAdded travis configArmin Ronacherbefore adoption09d219d
lines 23–53why, one step back23``optparse`` and does not implement any parsing itself. The reason it's24not based on ``argparse`` is that ``argparse`` does not allow proper25nesting of commands by design and has some deficiencies when it comes to26POSIX compliant argument handling.2728Click is designed to be fun to work with and at the same time not stand in29your way. It's not overly flexible at the same time either. Currently30for instance it does not allow you to customize the help pages too much.31This is intentional because click is designed to allow you to nest command32line utilities. The idea is that you can have a system that works33together with another system by tacking two click instances together and34they will continue working as they should.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 recommend43looking at the :ref:`complex-guide` chapter to see what it's useful for.4445Why not Argparse?46-----------------4748Click is internally based on optparse instead of argparse. This however49is an implementation detail that a user does not have to be concerned50with. The reason however click is not using argparse is that it has some51problematic behaviors that make handling arbitrary command line interfaces52hard:53Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69
line 54why, one step back54* argparse has built-in magic behavior to guess if something is anAdded travis configArmin Ronacherbefore adoption09d219d
lines 55–67why, one step back55 argument or an option. This becomes a problem when dealing with56 incomplete command lines as it's not possible to know without having a57 full understanding of the command line how the parser is going to58 behave. This goes against click's ambitions of dispatching to59 subparsers.60* argparse currently does not support disabling of interspearsed61 arguments. Without this feature it's not possible to safely implement62 click's nested parsing nature.6364Why not Docopt etc.?65--------------------6667Docopt and many tools like it are cool in how they work but very few ofUpdated notes on docoptArmin Ronacherbefore adoption5d85db9
lines 68–71why, one step back68these tools deal with nesting of commands and composability in a way like69click. To the best of the developer's knowledge, click is the first70Python library that tries to aim for composability of applications that71goes beyond what the system itself supports.Added travis configArmin Ronacherbefore adoption09d219d
line 72why, one step back72Updated notes on docoptArmin Ronacherbefore adoption5d85db9
line 73why, one step back73Docopt for instance acts by parsing your help pages and then parsingMerge pull request #63 from julen/doc-fixesArmin Ronacherbefore adoption0868754
line 74why, one step back74according to those rules. The side effect of this is that docopt is quiteUpdated notes on docoptArmin Ronacherbefore adoption5d85db9
line 75why, one step back75rigid in how it handles the command line interface. The upside of docoptMerge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69
lines 76–77why, one step back76is that it gives you strong control over your help page; the downside is77that due to this it cannot rewrap your output for the current terminalUpdated notes on docoptArmin Ronacherbefore adoption5d85db9
lines 78–82why, one step back78width and it makes translations hard. On top of that docopt is restricted79to basic parsing. It does not handle argument dispatching and callback80invocation or types. This means there is a lot of code that needs to be81written in addition to the basic help page to handle the parsing results.82Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69
line 83why, one step back83Most of all, however, it makes composability hard. While docopt doesUpdated notes on docoptArmin Ronacherbefore adoption5d85db9
lines 84–87why, one step back84support dispatching to subcommands, it for instance does not directly85support any kind of automatic subcommand enumeration based on what's86available or it does not enforce subcommands to work in a consistent way.87Merge pull request #63 from julen/doc-fixesArmin Ronacherbefore adoption0868754
line 88why, one step back88This is fine, but it's different from how click wants to work. Click aimsUpdated notes on docoptArmin Ronacherbefore adoption5d85db9
lines 89–90why, one step back89to support fully composable command line user interfaces by doing this:90Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69
lines 91–92why, one step back91- Click does not just parse, it also dispatches to the appropriate code.92- Click has a strong concept of an invocation context that allowsUpdated notes on docoptArmin Ronacherbefore adoption5d85db9
line 93why, one step back93 subcommands to respond to data from the parent command.Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69
line 94why, one step back94- Click has strong information available for all parameters and commandsUpdated notes on docoptArmin Ronacherbefore adoption5d85db9
lines 95–96why, one step back95 so that it can generate unified help pages for the full CLI and to96 assist the user in converting the input data as necessary.Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69
line 97why, one step back97- Click has a strong understanding of what types are and can give the userUpdated notes on docoptArmin Ronacherbefore adoption5d85db9
lines 98–100why, one step back98 consistent error messages if something goes wrong. A subcommand99 written by a different developer will not suddenly die with a100 different error messsage because it's manually handled.Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69
line 101why, one step back101- Click has enough meta information available for its whole programUpdated notes on docoptArmin Ronacherbefore adoption5d85db9
line 102why, one step back102 that it can evolve over time to improve the user experience withoutMerge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69
line 103why, one step back103 forcing developers to adjust their programs. For instance, if clickUpdated notes on docoptArmin Ronacherbefore adoption5d85db9
lines 104–113why, one step back104 decides to change how help pages are formatted, all click programs105 will automatically benefit from this.106107The 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 are conflicting with each other in subtle ways. Click110actively prevents people from implementing certain patterns in order to111achieve unified command line interfaces. You have very little input on112reformatting your help pages for instance.113Added travis configArmin Ronacherbefore adoption09d219d
lines 114–124why, one step back114115Why Hardcoded Behaviors?116------------------------117118The other question is why click goes away from optparse and hardcodes119certain behaviors instead of staying configurable. There are multiple120reasons for this. The biggest one is that too much configurability makes121it hard to achieve a consistent command line experience.122123The best example for this is optparse's ``callback`` functionality for124accepting arbitrary number of arguments. Due to syntactical ambiguitiesMerge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69
line 125why, one step back125on the command line, there is no way to implement fully variadic arguments.Added travis configArmin Ronacherbefore adoption09d219d
lines 126–132why, one step back126There are always tradeoffs that need to be made and in case of127``argparse`` these tradeoffs have been critical enough, that a system like128click cannot even be implemented on top of it.129130In this particular case click attempts to stay with a handful of accepted131paradigms for building command line interfaces that can be well documented132and tested.Added notes on no auto correctionArmin Ronacherbefore adoption72d26f6
lines 133–142why, one step back133134135Why No Auto Correction?136-----------------------137138The question came up why click does not auto correct parameters given that139even 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. TheseMerge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69
line 143why, one step back143kinds of problems are hard to find so click does not attempt to be magicalAdded notes on no auto correctionArmin Ronacherbefore adoption72d26f6
lines 144–148why, one step back144about this.145146This sort of behavior however can be implemented on a higher level to147support things such as explicit aliases. For more information see148:ref:`aliases`.