Cli-matic - a concise stub for command-line scripts


#1

Hi all,
I find writing more and more scripts since I started using clj, but one thing that I really did not like was all the boilerplate required for command-line parsing. The library tools.cli is very powerful, but it requires writing way too much for my average script.
Plus, I generally use the subcommands in scripts, like e.g. git log, and wanted a way to automate help and description.

So I wrote this little library cli-matic https://github.com/l3nz/cli-matic that basically requires a configuration struct and some functions to actually implement functionality, and takes care of the rest.

You can see it in action in this very little toy calculator https://github.com/l3nz/cli-matic/blob/master/src/cli_matic/toycalc.clj - as you can see, there is basically nothing but the functions that do the computing plus a declarative configuration structure.

I am sharing it as I’m finding it useful - comments and hints are very welcome. Would love to make it work with both clj/cljs, if anyone is interested.


#2

Nice way to make tools.cli usage a lot more concise!

Stylistic comment: it’s very strange to see closing } and ) on their own lines – that’s counter to the community style guide for Clojure.


#3

The library still has a long way to go, but I think it’s useful enough to be used profitably by somebody else.

As per your code comments - I agree, and my feeling was “maybe one or two cases, I don’t leave dangling parentheses”. Boy I was wrong - and I did not notice (on my defense, I rely on Cursive to do the formatting, and I just did not notice)! Any suggestions on tools that may simpllify formatting / spot weirdnesses?


#4

You could always run your code through clj-format – I think there’s a Leiningen plugin for it.


#5

Hello,
Seems that you are having fun writing cli scripts.
I don’t want to underestimate your work, but have you considered using docopt? Docopt is an standard description for command line parsing that has implementations for many different languages,including Clojure. The cool part is that the help message is also the api description and the parser.
It’s a shame that it does not have a ClojureScript implementation


#6

I was not aware of docopt http://docopt.org/ - it looks very powerful, though it seems to treat everything as one single command (in terms of help), while what I had in mind was more similar to the way you do it in Go http://snowsyn.net/2016/08/11/parsing-command-line-options-in-go/ that I always found very pleasant to use.


#7

This looks useful! Thank you for sharing your work.

On a side note, in your example I was surprised to see snake casing for a function. That is quite unusual in Clojure. :smile:


#8

I really need to use clj-format :slight_smile:


#9

What do you mean ? If you input any command or subcommand wrong it will display the help, but I think it also displays sub-command only help if you input a sub-command wrong.


#10

I have been looking for something like this!!. great work!.


#11

…or you can wake up one morning and discover somebody else did it for you and sent you a PR while you were sleeping. :wink: