Live data from Hacker News

Easy Python CLI with Click

codingwithricky.com

1–10 of 64 posts

Re: Easy Python CLI with Click

#3
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.

Re: Easy Python CLI with Click

#5
post #4

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? [0] https://github.com/google/python-fire

Based on the examples in the article, explicitly writing a line for each CLI option seems like excessive boilerplate and verbosity that Fire manages to sidestep completely. To me, this is the principal point of using a Python library to create CLIs. Am I missing something?

Re: Easy Python CLI with Click

#6
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.

Re: Easy Python CLI with Click

#7
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.

Re: Easy Python CLI with Click

#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.

The official doc is really nice and gives it more justice than this blog post: https://click.palletsprojects.com/en/7.x.

Re: Easy Python CLI with Click

#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()

Re: Easy Python CLI with Click

#10

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.

Post reply on HN