The thing that bugs me about click is that now your python script has a python dependency that you have to install every time you need to run the script. This is fine sometimes, but a lot of the time I want a script that will be usable with minimal setup, which is why I always use argparse, which is in the standard library, and gets the job done despite its quirky API.
My favorite library for this is docopt[0], which parses the docstring at the top of your script. It’s a lot easier for people reading your code to see the usage up front rather than scrolling to the bottom and finding your main() and reading the argparse calls:<p><pre><code> #!/usr/bin/env python3
“””
Usage: ./myscript.py <arg> [<optionalarg>]
“””
from docopt import docopt
if __name__ == ‘__main__’:
args = docopt(__doc__)
print(args[‘<arg>’], args[‘<optionalarg>’] or ‘foo’)
</code></pre>
[0]: <a href="https://github.com/docopt/docopt" rel="nofollow">https://github.com/docopt/docopt</a>
I’ve only ever found click to be a waste of time compared to just using argparse. The extra concision you get from decorator syntax just doesn’t matter and you introduce another dependency and need to go into the relatively poor click documentation (especially if you end up with a longterm dependency on old versions of click). Just not worth it.
I'm not sure why click is often recommended. I've used argh for years and find it easier:<p><pre><code> def hello(name):
return "hello {}".format(name)
def ping():
return "pong"
if __name__ == "__main__":
import argh
parser = argh.ArghParser()
parser.add_commands([hello, ping])
parser.dispatch()</code></pre>
I have 3 live projects using click. I like it.<p>Caveat: Click's docs were out of date and had open bugs for what feels like years until it finally updated to 7. version 6 was "unstable" <a href="https://github.com/pallets/click/issues/503" rel="nofollow">https://github.com/pallets/click/issues/503</a>. Hopefully it's over now.<p>Good things:<p>- More concise than standard library argparse<p>- Testing via CliRunner (<a href="http://click.palletsprojects.com/en/7.x/testing/" rel="nofollow">http://click.palletsprojects.com/en/7.x/testing/</a>), example: <a href="https://github.com/tmux-python/tmuxp/blob/v1.5.3/tests/test_cli.py" rel="nofollow">https://github.com/tmux-python/tmuxp/blob/v1.5.3/tests/test_...</a><p>Feel free to study / copy from mine if you like (license MIT):<p>- <a href="https://cihai-cli.git-pull.com/" rel="nofollow">https://cihai-cli.git-pull.com/</a>: <a href="https://github.com/cihai/cihai-cli/blob/v0.5.0/cihai_cli/cli.py" rel="nofollow">https://github.com/cihai/cihai-cli/blob/v0.5.0/cihai_cli/cli...</a><p>- <a href="https://tmuxp.git-pull.com/" rel="nofollow">https://tmuxp.git-pull.com/</a>: <a href="https://github.com/tmux-python/tmuxp/blob/v1.5.3/tmuxp/cli.py" rel="nofollow">https://github.com/tmux-python/tmuxp/blob/v1.5.3/tmuxp/cli.p...</a><p>- <a href="https://vcspull-git-pull.com/" rel="nofollow">https://vcspull-git-pull.com/</a>: <a href="https://github.com/vcs-python/vcspull/blob/v1.2.0/vcspull/cli.py" rel="nofollow">https://github.com/vcs-python/vcspull/blob/v1.2.0/vcspull/cl...</a>
Does this do anything that the builtin argparse library <a href="https://docs.python.org/3/library/argparse.html" rel="nofollow">https://docs.python.org/3/library/argparse.html</a> doesn't? Seems it just turns it into decorator syntax.
For those of you using argparse I recommend looking at configargparse. A drop in replacement which adds environment variable support (similar to click) and config file support
I've used Fire[0] for a few projects and have been meaning to give Click a try, but I'm not sure I like the syntax after reading through the article (holy moly decorators). Seems unwieldy to me, but does anyone have experience with both?<p>[0] <a href="https://github.com/google/python-fire" rel="nofollow">https://github.com/google/python-fire</a>
Click unfortunately doesn't pass the smell test of a non-awkward implementation of 'sudo'. It gets some partial credit because an implementation is <i>possible</i> but only because you can subclass the click.Command object and change the parsing behavior arbitrarily.<p>So many of these parsing libraries seem to forget that any positional parameter can stop options parsing not just '--' and that that parameter might appear arbitrarily deep in subcommands.<p>This is not only needed for commands like sudo and ssh but BSD style CLIs as well which all OSX users are probably intimately familiar and annoyed with.
Click and argparse end up with a lot of boilerplate. `defopt` (<a href="https://defopt.readthedocs.io/en/stable/" rel="nofollow">https://defopt.readthedocs.io/en/stable/</a>) reduces it to almost nothing (granted only in Python 3), is only a single file and even turns your function docstring into command help text.<p>With every other tool I’ve tried, I end up writing a python API and a CLI and documenting it twice.<p>Defopt eliminates that almost completely (but with *args still allows for elegant interfaces)
The decorators aren't the main thing, you dont need to use them. The overall interface is just so much better than argparse. It's way more featureful.
i'm sorry but this is not how decorators are supposed to be used. I also doubt that click does decorators correctly too. I couldn't get them working on a instance method for example. Just stick to the standard library on this. A dependency is not worth it here.
I use click for <a href="https://github.com/rcarmo/piku" rel="nofollow">https://github.com/rcarmo/piku</a> (in fact it’s the only dependency) and often wish something like it was part of the standard library...
Here is a fairly detailed overview in the form of slides what is Click and what it can do: <a href="https://slides.com/kissgyorgy/deck-10#/" rel="nofollow">https://slides.com/kissgyorgy/deck-10#/</a>
I wish python had something similar to OCaml's Cmdliner[0]<p>[0] <a href="https://erratique.ch/software/cmdliner/doc/Cmdliner" rel="nofollow">https://erratique.ch/software/cmdliner/doc/Cmdliner</a>