Live data from Hacker News

Easy Python CLI with Click

codingwithricky.com

11–20 of 64 posts

Re: Easy Python CLI with Click

#11
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:

  #!/usr/bin/env python3
  “””
  Usage: ./myscript.py  []
  “””
  from docopt import docopt

  if __name__ == ‘__main__’:
      args = docopt(__doc__)
      print(args[‘’], args[‘’] or ‘foo’)
[0]: https://github.com/docopt/docopt

Re: Easy Python CLI with Click

#13
post #8
post #2

Does this do anything that the builtin argparse library https://docs.python.org/3/library/argparse.html doesn't? Seems it just turns it into decorator syntax.

It really comes handy IMHO when you have a lot of commands with nesting and context to share. I've used it on a CLI tool with many sub-commands implemented in imported modules and it made things really easy while I don't think argpase would have "scaled" without becoming a mess. Plus it comes with some useful utilities you might need in a cli to show a progressbar, display color, open test in a pager/an editor etc. T…

Ah - makes a lot more sense when they describe Git, with its nested interfaces and argument parsers (and reused logic therein) as a motivating example!

Re: Easy Python CLI with Click

#14

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.

This is more of a packaging problem. There are various ways to package python code with it's dependencies into a single executable. In any case, comparing a stdlib library to a 3rd party one is a bit apples to oranges. Most people first decide whether or not they want to use pypi packages, and then start evaluating which ones are appropriate.

sure, if you're building a CLI to do management commands for an application that already has dependency management in place, that's all fine

but if you just have some basic script to scrape some logs or zip up files, its nice to have it be self contained

Re: Easy Python CLI with Click

#15
post #11

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: #!/usr/bin/env python3 “”” Usage: ./myscript.py [ ] “”” from docopt import docopt if __name__ == ‘__main__’: args = docopt(__doc__) print(args[‘ ’], args[‘ ’]…

For really, really basic things docopt is good. But you tend to re-invent the wheel when you need to do something other than the basic parsing it supports. The click docs have a good section[1] on this:

> On top of that docopt is restricted to basic parsing. It does not handle argument dispatching and callback invocation or types. This means there is a lot of code that needs to be written in addition to the basic help page to handle the parsing results.

1. http://click.palletsprojects.com/en/7.x/why/#why-not-docopt-...

Re: Easy Python CLI with Click

#16

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.

Decorators can be used in any way you want. This is actually an absolutely fine use of decorators to add metadata/annotations to a given function.

Critiques of any library are great, but you need to expand on vague points: "I also doubt that click does decorators correctly too", or how you would ever expect them (or even want them??) to work on an instance method. It might be that you're misunderstanding the library and how it should be used rather than it being not worth using.

I find it absolutely perfect for a lot of situations.

Re: Easy Python CLI with Click

#17

Earlier quoted context omitted.

This is more of a packaging problem. There are various ways to package python code with it's dependencies into a single executable. In any case, comparing a stdlib library to a 3rd party one is a bit apples to oranges. Most people first decide whether or not they want to use pypi packages, and then start evaluating which ones are appropriate.

sure, if you're building a CLI to do management commands for an application that already has dependency management in place, that's all fine but if you just have some basic script to scrape some logs or zip up files, its nice to have it be self contained

Perhaps a non-starter for you if you don't want to involve Docker but I have had good experiences wrapping a docker invocation in a shell script shim with the same name as the program and forwarding everything I need into the container. Then folks just grab the shell script and they're off... added bonus it's really easy to add update functionality to your tools.

Re: Easy Python CLI with Click

#19
post #9

I'm not sure why click is often recommended. I've used argh for years and find it easier: 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()

If people in your organization tend to write Flask apps, then you might find click in standalone scrips as well.
Post reply on HN