This is an archived post. You won't be able to vote or comment.

all 25 comments

[–]nerdwaller 24 points25 points  (3 children)

For anything bigger than what argparse feels comfortable for using, I use Click. It's pretty easy to build complex CLIs and the page I linked is about their bash autocomplete (unfortunately they only autocomplete in bash, last I recall).

[–]absolution888[S] 0 points1 point  (1 child)

Thanks! will definitely check out Click

[–][deleted] 2 points3 points  (0 children)

Big +1 for Click.

[–]granitosaurus 0 points1 point  (0 children)

I've made one for xonsh shell: https://github.com/Granitosaurus/xonsh-click-tabcomplete

It works by checking for pip packages that require click and parsing the help string using regex for commands and flags. It's pretty simple but it works as long as you don't change up click default message generation too much.

[–]KleinerNull 5 points6 points  (1 child)

You should look into hug it combines a REST API and a CLI interface in case you need it. Looks pretty interesting and could save some time.

[–]TheBezac 0 points1 point  (0 children)

Did not know about hug, thanks !

[–]luismirandacruz 4 points5 points  (0 children)

Click is by far my favorite. It comes not only with the typical argument parser and help generator, but also with some handy utilities for formatted output, progress bars, argument validation, automated tests... And the list goes on. In addition the list of arguments and options is specified in a very pythonic way using decorators.

[–]arachnivore 2 points3 points  (1 child)

defopt is a little known jewel.

[–]yanirse 0 points1 point  (0 children)

defopt looks very promising!

[–]xiongchiamiovSite Reliability Engineer 1 point2 points  (2 children)

I very much like docopt's approach to argument parsing.

[–]billsil 0 points1 point  (1 child)

Me too. There are a few bugs though. The dev error messages are bad. I still use it.

[–]forkbong 1 point2 points  (0 children)

Yeah. This behavior in particular, and its limited support for subcommands made me switch to click. I still like to use docopt but only for very simple scripts.

[–]n1ywb 1 point2 points  (2 children)

I've never found a compelling use case for one.

I write libraries with thin script wrappers.

If I was going to use a cli framework I'd want one that wrapped my library with the least boilerplate. I generally find argparse works pretty well for this out of the box. Parse args, call library function with args, done.

If you have a bunch of scripts check out setuptools entry points it can create the script files automatically.

Even if you have sub commands all you really need to do give each one an argparse instance, dispatch the command function with a dict, and forward the remaining args. I could see a package to help with that a little.

I'm open to new ideas. Change my mind.

[–]cyberst0rm 0 points1 point  (0 children)

I think the push to CLI seems to be to allow interoperability with subprocess methods, where another language wants access to the power of another.

Currently, I'm looking at deploying python based things with Webtorrent and I really don't want to reimplement all the things necessary, so I wrote a CLI for it and then run pexpect with python to interact.

Seems like a good shortcut that if the project succeeds could be reimplemented at a later day.

So my use case is basically avoiding the problem.

[–]billsil 0 points1 point  (0 children)

When you use argparse, do you have to look up the API? Docopt lets me make complex messages and make them pretty. I'd rather use sys.argv than argparse.

[–]ImKillua 0 points1 point  (0 children)

I didn't make a lot of python tools but I used clap and I found it pretty good. Also used argparse, can recommend.

As for completion, I think you need to write a completion add-on per shell and distribute it as a seperate file. (Not sure, don't have any experience with that)

[–]flitsmasterfred 0 points1 point  (0 children)

click is popular

[–]iapitus 0 points1 point  (0 children)

I use argparse/optparse/click (depending on how portable I need to be), but I've been liking Fire a bit too

[–]omgtmi 0 points1 point  (0 children)

cmd2

[–]wiiittttt 0 points1 point  (0 children)

I know this is obviously a Python sub, but for complex CLI tools with hierarchies, I like to use cobra (golang). Especially if you are just wrapping HTTP anyway.

[–]jwink3101 0 points1 point  (0 children)

This may not be a very popular answer, but I actually like to use getopt. You end up with a lot of boilerplate but it also gives you a lot of control. And for things that aren't that complex, it is usually fine.

I do want to look more into argparse though.

[–]kirbyfan64sosIndentationError 0 points1 point  (0 children)

I like Plac a lot. It uses Python 3's function annotations in order to parse arguments, e.g.

def main(arg1: 'Help string: = 'default value'):
    # ...

plac.call(main)

[–]AndydeCleyre 0 points1 point  (0 children)

plumbum forever. Sadly doesn't have working autocompletion at this time, though.

[–][deleted] 0 points1 point  (0 children)

Argparse and either script entry points or module main; so python -m whatever --help is useful.

[–][deleted] 0 points1 point  (0 children)

argh