I have to write a command-line interface and I've seen I can use docopt
and argparse
.
I would like to know what are the main differences between the two so that I can make an enlightened choice.
Please stick to the facts. I don't want Wow. docopt. So beautiful. Very useful.
Docopt parses a doc string, whereas argparse constructs its parsing by creating an object instance and adding behaviour to it by function calls.
Example for argparse:
Example for docopt:
Note, that docopt uses
Usage:
andOptions:
sections for parsing. HereArguments:
is provided only for end-user convenience.argparse
is in the python default library, so this doesn't add any extra dependencies to your program. The differences are mainly the way of writing your code. Usingargparse
it is possible to add hooks for plugins so they can add their own argumnets to your program. For example flake8 uses this.docopt
is a third party module provides a simple way of parsing arguments. I personally likedocopt
because of its simplicity, but I'm not saying it is the best to use in all cases. In their documentation they mention that usingdocopt
it is possible to use more combinations of argument passing than when usingargparse
.The Why page for Click:
http://click.pocoo.org/5/why/
has a nice comparison between argparse, docopt and click itself.
Click
is another command-line parsing utility for Python.