4 ms·
Comparing Python Command-Line Parsing Libraries – Argparse, Docopt, and Click
- dalke 11y agoSome friends recommend docopt over argparse. My major reason against docopt is that my code has several options and arguments that are determined at run-time. As a simple example, consider a "--format" argument which must be one of list of supported formats, and which may depend on your environment (eg, have you compiled PIL with/without jpeg support?). With docopt I think I have to basically re-implement a templating system in order to build the help text, and this system has to be aware of the parse rules. I can do that, but it seems easier to just tell argparse the list of options directly. Click definitely looks interesting, and a quick read of the documentation suggests it might 'fit my brain' better than argparse. For example, I don't like how argparse supports automatic expansion of long arguments because I think it leads to forward compatibility problems. That support can be disabled in recent versions of Python, but I agree with click that it simply shouldn't be supported, except through an explicit alias list.
- jgrowl 11y agoI had a simpler case that I couldn't figure out using docopt. I was trying to add in tls flags into docker-compose to match docker. I could not get a combination working where you can just specify --tls to mean tls is true while still accepting --tls=true|false|0|1.
- bobwaycott 11y agoA well-executed and informative comparison. I particularly liked seeing the final section on packaging your CLI scripts.
- mjhea0 11y agoCheers!
- ericfrederich 11y agoDoesn't seem to be any mention of completion support. I use argparse with argcomplete and get nice completion.
- Semiapies 11y agoInteresting comparison, though the source for the docopt refactoring got mangled. Looks like it may have been double-pasted inside itself.