Hacker News new | ask | show | jobs
by JulianWasTaken 4789 days ago
This kind of "hey I will parse your CLI out of some callables' argspecs" has been done many times before:

- https://github.com/fritzo/parsable.py

- https://github.com/gissehel/cltools

- https://github.com/piranha/opster

- https://github.com/kennethreitz-archive/argue

- https://github.com/pdubroy/simpleopt

(all of those found from a 30 second GitHub search, so apologies if I've left one out or if one of those isn't exactly alike).

The thing is, the fact that you cut out some code does not make your thing more Pythonic, or simpler, or easier to use by itself. It's definitely a start, but CLIs are hard. And while argparse has its issues, if you try to supplant it, I don't doubt that you can get the simple cases right, but it's the more complex composition of components that is hard to do, and that's why I find anything in this realm hard to latch onto.

docopt is a nice step in a different direction, and I use it occasionally, but it's hard to do this -- for the simple cases argparse really isn't that bad, and I really really don't think that magically introspecting callables is a good approach. You will at some point want something more complicated, or to prevent some magic from doing somethings, and that kind of interface will become awkward, inextensible, and painful. Or at least that has been the case so far for anyone that's tried this.

So, applaud any code that anyone's sharing, apologies for coming down hard, but I don't think this is something anyone should use for anything but a trivial "hey here's my CLI it's a one off, and I don't want to learn argparse [which takes an hour or two]". And even for that, docopt is probably a better option.

3 comments

I think the most popular is https://pypi.python.org/pypi/Baker/. And I like automatic introspection of callables very much - no more argument parsing, just a decorator under a function. Sure this is not suitable for polished CLI interfaces, but my use cases are almost only in-house scripts for internal usage.
+1 for Baker, it is the first script I import in my git repo when writing a python tool.

A big plus is that it is a single file (baker.py) with no dependencies, which is very useful when I have to use my scripts on machines I don't have root access on. (I know about virtualenv, but it is definitely overkill for most of the things I do)

Similar task solved in Java, though for interactive CLI. There it's much more needed, for we don't have argparse and the language's just too verbose.

https://code.google.com/p/cliche/

I'm working on one that distinguishes itself by attempting to be language-agnostic, meaning the subcommands are just executables, implemented in any language: https://github.com/datagrok/subcommander

It's not meant to replace argparse; it's meant to provide a namespace mechanism for a system with lots of sub-commands.

I've been using it at work for over a year. Needs some more polish and some bugs fixed before I'd call it "releasable" though.

Another project with similar goals: https://github.com/37signals/sub