TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Easy Python CLI with Click

56 pointsby ribabalmost 6 years ago

15 comments

Areading314almost 6 years ago
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.
评论 #20732313 未加载
评论 #20732728 未加载
评论 #20733030 未加载
rjelialmost 6 years ago
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> #!&#x2F;usr&#x2F;bin&#x2F;env python3 “”” Usage: .&#x2F;myscript.py &lt;arg&gt; [&lt;optionalarg&gt;] “”” from docopt import docopt if __name__ == ‘__main__’: args = docopt(__doc__) print(args[‘&lt;arg&gt;’], args[‘&lt;optionalarg&gt;’] or ‘foo’) </code></pre> [0]: <a href="https:&#x2F;&#x2F;github.com&#x2F;docopt&#x2F;docopt" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;docopt&#x2F;docopt</a>
评论 #20732451 未加载
评论 #20733370 未加载
评论 #20733194 未加载
评论 #20733079 未加载
mlthoughts2018almost 6 years ago
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.
gugglealmost 6 years ago
I&#x27;m not sure why click is often recommended. I&#x27;ve used argh for years and find it easier:<p><pre><code> def hello(name): return &quot;hello {}&quot;.format(name) def ping(): return &quot;pong&quot; if __name__ == &quot;__main__&quot;: import argh parser = argh.ArghParser() parser.add_commands([hello, ping]) parser.dispatch()</code></pre>
评论 #20735684 未加载
评论 #20732495 未加载
评论 #20732877 未加载
评论 #20733122 未加载
评论 #20732799 未加载
tonyalmost 6 years ago
I have 3 live projects using click. I like it.<p>Caveat: Click&#x27;s docs were out of date and had open bugs for what feels like years until it finally updated to 7. version 6 was &quot;unstable&quot; <a href="https:&#x2F;&#x2F;github.com&#x2F;pallets&#x2F;click&#x2F;issues&#x2F;503" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;pallets&#x2F;click&#x2F;issues&#x2F;503</a>. Hopefully it&#x27;s over now.<p>Good things:<p>- More concise than standard library argparse<p>- Testing via CliRunner (<a href="http:&#x2F;&#x2F;click.palletsprojects.com&#x2F;en&#x2F;7.x&#x2F;testing&#x2F;" rel="nofollow">http:&#x2F;&#x2F;click.palletsprojects.com&#x2F;en&#x2F;7.x&#x2F;testing&#x2F;</a>), example: <a href="https:&#x2F;&#x2F;github.com&#x2F;tmux-python&#x2F;tmuxp&#x2F;blob&#x2F;v1.5.3&#x2F;tests&#x2F;test_cli.py" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;tmux-python&#x2F;tmuxp&#x2F;blob&#x2F;v1.5.3&#x2F;tests&#x2F;test_...</a><p>Feel free to study &#x2F; copy from mine if you like (license MIT):<p>- <a href="https:&#x2F;&#x2F;cihai-cli.git-pull.com&#x2F;" rel="nofollow">https:&#x2F;&#x2F;cihai-cli.git-pull.com&#x2F;</a>: <a href="https:&#x2F;&#x2F;github.com&#x2F;cihai&#x2F;cihai-cli&#x2F;blob&#x2F;v0.5.0&#x2F;cihai_cli&#x2F;cli.py" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;cihai&#x2F;cihai-cli&#x2F;blob&#x2F;v0.5.0&#x2F;cihai_cli&#x2F;cli...</a><p>- <a href="https:&#x2F;&#x2F;tmuxp.git-pull.com&#x2F;" rel="nofollow">https:&#x2F;&#x2F;tmuxp.git-pull.com&#x2F;</a>: <a href="https:&#x2F;&#x2F;github.com&#x2F;tmux-python&#x2F;tmuxp&#x2F;blob&#x2F;v1.5.3&#x2F;tmuxp&#x2F;cli.py" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;tmux-python&#x2F;tmuxp&#x2F;blob&#x2F;v1.5.3&#x2F;tmuxp&#x2F;cli.p...</a><p>- <a href="https:&#x2F;&#x2F;vcspull-git-pull.com&#x2F;" rel="nofollow">https:&#x2F;&#x2F;vcspull-git-pull.com&#x2F;</a>: <a href="https:&#x2F;&#x2F;github.com&#x2F;vcs-python&#x2F;vcspull&#x2F;blob&#x2F;v1.2.0&#x2F;vcspull&#x2F;cli.py" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;vcs-python&#x2F;vcspull&#x2F;blob&#x2F;v1.2.0&#x2F;vcspull&#x2F;cl...</a>
btownalmost 6 years ago
Does this do anything that the builtin argparse library <a href="https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;library&#x2F;argparse.html" rel="nofollow">https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;library&#x2F;argparse.html</a> doesn&#x27;t? Seems it just turns it into decorator syntax.
评论 #20732149 未加载
jabworkalmost 6 years ago
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
kylekalmost 6 years ago
I&#x27;ve used Fire[0] for a few projects and have been meaning to give Click a try, but I&#x27;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:&#x2F;&#x2F;github.com&#x2F;google&#x2F;python-fire" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;google&#x2F;python-fire</a>
评论 #20732062 未加载
评论 #20735098 未加载
Spivakalmost 6 years ago
Click unfortunately doesn&#x27;t pass the smell test of a non-awkward implementation of &#x27;sudo&#x27;. 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 &#x27;--&#x27; 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.
评论 #20736275 未加载
jblarneyforwardalmost 6 years ago
Click and argparse end up with a lot of boilerplate. `defopt` (<a href="https:&#x2F;&#x2F;defopt.readthedocs.io&#x2F;en&#x2F;stable&#x2F;" rel="nofollow">https:&#x2F;&#x2F;defopt.readthedocs.io&#x2F;en&#x2F;stable&#x2F;</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)
j88439h84almost 6 years ago
The decorators aren&#x27;t the main thing, you dont need to use them. The overall interface is just so much better than argparse. It&#x27;s way more featureful.
sigmonsaysalmost 6 years ago
i&#x27;m sorry but this is not how decorators are supposed to be used. I also doubt that click does decorators correctly too. I couldn&#x27;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.
评论 #20732468 未加载
评论 #20733096 未加载
rcarmoalmost 6 years ago
I use click for <a href="https:&#x2F;&#x2F;github.com&#x2F;rcarmo&#x2F;piku" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;rcarmo&#x2F;piku</a> (in fact it’s the only dependency) and often wish something like it was part of the standard library...
kissgyorgyalmost 6 years ago
Here is a fairly detailed overview in the form of slides what is Click and what it can do: <a href="https:&#x2F;&#x2F;slides.com&#x2F;kissgyorgy&#x2F;deck-10#&#x2F;" rel="nofollow">https:&#x2F;&#x2F;slides.com&#x2F;kissgyorgy&#x2F;deck-10#&#x2F;</a>
dwdzalmost 6 years ago
I wish python had something similar to OCaml&#x27;s Cmdliner[0]<p>[0] <a href="https:&#x2F;&#x2F;erratique.ch&#x2F;software&#x2F;cmdliner&#x2F;doc&#x2F;Cmdliner" rel="nofollow">https:&#x2F;&#x2F;erratique.ch&#x2F;software&#x2F;cmdliner&#x2F;doc&#x2F;Cmdliner</a>