Jean Carlo Machado
Jean Carlo Machado

Reputation: 1616

Building CLI scripts in Clojure

What are the common/standard ways to build CLI scripts in Clojure?

In my view such a method should include the following characteristics:

Ideally, providing a simple example of the solution usage would be much appreciated. Somewhat equivalent to:

#!/bin/bash
    echo "$@"
    cat /dev/stdin

Note: I'm aware that this question was somewhat questioned previously here. But the question is incomplete and the answers don't reach a consensus neither a significant proportion of the solutions that seems to exist.

Upvotes: 4

Views: 1741

Answers (6)

Mars
Mars

Reputation: 8854

One should also consider babashska: https://github.com/babashka/babashka. Rather than using a version of Clojurescript, like planck, it implements a subset of JVM-based Clojure, but has fast startup becauses it's been compiled to native binaries using GraalVM.

Note that lumo, mentioned in some other answers, seems to be no longer under development, although for all I know it's still a good option.

Upvotes: 0

Rachel K. Westmacott
Rachel K. Westmacott

Reputation: 2301

Now that there is new CLI tooling it is possible to create a standalone Clojure script without using third party tools. Once you've got the clj command line tool installed, a script like the one below should just work.

In terms of the original question, this can be as good as any Clojure/JVM CLI program at dealing with command line arguments and system input/output depending on what libraries you :require. I've haven't benchmarked it, so I won't comment on performance but if it worries you then please experiment yourself to see if startup time is acceptable to you. I would say this scores highly on dependency management though, as the script is entirely standalone (apart from the clj tool which is now the recommended way to run Clojure anyway).

File: ~/bin/script.sh

#!/bin/sh

"exec" "clj" "-Sdeps" "{:deps,{hiccup,{:mvn/version,\"1.0.5\"}}}" "$0" "$@"

(ns my-script
  (:require
    [hiccup.core :as hiccup]))

(println
  (hiccup/html
    [:div
      [:span "Command line args: " (clojure.string/join ", " *command-line-args*)]
      [:span "Stdin: " (read-line)]]))

Then ensure it is executable:

$ chmod +x ~/bin/script.sh

And run it:

$ echo "stdin" | script.sh command line args
<div><span>Command line args: command, line, args</span><span>Stdin: stdin</span></div>

NB. This is primarily a shell script which treats the strings on line three as commands to execute. That subsequent execution will run the clj command line tool with the given arguments, which will evaluate those strings as strings (without side effects) and then proceed to evaluate the Clojure code below.

Note also that dependencies are specified as a map passed to clj on line three. You can read more about how that works on the Clojure website. The tokens in the dependency map are separated by commas, which Clojure treats as whitespace but which most shells do not.

Thanks to the good folk on the #tools-deps channel of the "clojurians" Slack group whence this solution came.

Upvotes: 4

kmell
kmell

Reputation: 1

I write a number of Clojure (JVM) scripts, and use a the CLI-matic library https://github.com/l3nz/cli-matic/ to abstract most of the boilerplate that goes with command-line parsing, creation and maintenance of help, errors, etc.

Upvotes: 0

Matias Bjarland
Matias Bjarland

Reputation: 4482

I know you asked for non project creating methods to accomplish this but as this specific issue has been on my mind for quite some time I figured I would throw in another alternative.

TLDR: jump to the "Creating an Executable CLI Command" section below

Background

I had pretty much the same list of requirements as you do a while back and landed on creating executable jar files. I'm not talking about executable via java -jar myfile.jar, but rather self-contained uber-jars which you can execute directly as you would with any other binary file.

If you read the zip file specification (which jar files adher to as a jar file is a zip file), it turns out this is actually possible. The short version is that you need to:

  • build a fat jar with the stuff you need
  • insert a bash / bat / shell script into the binary jar content at the beginning of your file
  • chmod +x the uber jar file (or if on windows, check the executable box)
  • rewrite the jar file meta data records so that the inserted script text does not invalidate the zip file internal offsets

It should be noted that this is actually supported by the zip file specification. This is how self extracting zip files etc work and the resulting fat jar (after the above process) is still a valid jar file and a valid zip archive. All relevant commands such as java -jar still work and the file is now also executable directly from the command line.

In addition, following the above pattern it is also possible to add support for things like the drip jvm launcher which greatly accelerates the startup times of your cli scripts.

As it turns out when I started looking into this about a year ago, a library for the last point of rewriting the jar file meta data did not exist. Not just in clojure but on the JVM as a whole. This still blows my mind: the central deployment unit of all languages on the jvm is the jar file and there was no library out there that actually read the internals of jar files. Internals as in the actual zip file structure, not just what java's ZipFile and friends does.

Furthermore, I could not find a library for clojure which dealt with the kind of binary structure the zip file specification required in a clean way.

Solution:

  • octet has what I consider the cleanest interface of the available binary libraries for clojure, so I wrote a pull request for octet adding support for the features required by the zip file specification.
  • I then created a new library clj-zip-meta which reads and interprets the zip file meta data and is capable of the offset rewriting described in the last point above.
  • I then created a pull request to an existing clojure lib lein-binplus to add support for the zip meta rewriting implemented by clj-zip-meta and also add support for custom preamble scripts to be able to create real executable jars without the need for java -jar.
  • After all this I created a leiningen template cli-cmd to support creating cli command projects which support all the above bells and whistles and has a well structured command line parsing setup...or what I considered well structured : ). Comments welcomed.

Creating an Executable CLI Command

So with all that, you can create a new command line clojure app with leiningen and run it using:

~> lein new cli-cmd mycmd

~> cd mycmd

~> lein bin 

Compiling mycmd.core
Compiling mycmd.core
Created /home/mbjarland/tmp/clj-cmd/mycmd/target/mycmd-0.1.0-SNAPSHOT.jar
Created /home/mbjarland/tmp/clj-cmd/mycmd/target/mycmd-0.1.0-SNAPSHOT-standalone.jar
Creating standalone executable: /home/mbjarland/tmp/clj-cmd/mycmd/target/mycmd
Re-aligning zip offsets

~> target/mycmd 
---- debug output, remove for production code ----
options    {:port 80, :hostname "localhost", :verbosity 0}
arguments  []
errors     nil
summary    
   -p, --port PORT      80         Port number
  -H, --hostname HOST  localhost  Remote host
      --detach                    Detach from controlling process
  -v                              Verbosity level; may be specified multiple times to increase value
  -h, --help
--------------------------------------------------
This is my program. There are many like it, but this one is mine.

Usage: mycmd [options] action

Options:
  -p, --port PORT      80         Port number
  -H, --hostname HOST  localhost  Remote host
      --detach                    Detach from controlling process
  -v                              Verbosity level; may be specified multiple times to increase value
  -h, --help

Actions:
  start    Start a new server
  stop     Stop an existing server
  status   Print a server's status

Please refer to the manual page for more information.

Error: invalid action '' specified!   

Where the output from the command is just the boilerplate sample command line parsing I've added to the leiningen template.

The custom preamble script is located at boot/jar-preamble.sh and it has support for drip. In other words, if you have drip on your path, the generated executable will use it, otherwise it will fall back to standard java -jar way of launching the uber jar internally.

The source for the command line parsing and the code for the cli app live under the src directory as per normal.

If you feel like hacking, it is possible to change the preamble script and re-run lein bin and the new preamble will be inserted into your executable by the build process.

Also it should be noted that this method still does java -jar under the covers so you do need java on your path.

Ayway, long-winded explanation, but hopefully it will be of some use for somebody with this problem.

Upvotes: 3

Michiel Borkent
Michiel Borkent

Reputation: 34870

An option would be Planck which runs on MacOS and Linux. It uses self-hosted ClojureScript, has fast startup and targets JavaScriptCore.

It has a nice SDK and mimics some things from Clojure which you do not have in ClojureScript, e.g. planck.io resembles clojure.java.io. It supports loading dependencies via tools.deps.alpha/deps.edn.

Echoing stdin is as easy as:

(require '[planck.core :refer [*in* slurp]])
(print (slurp *in*))

and printing the command line arguments:

(println *command-line-args*)

...

$ echo "foo" | planck stdin.cljs 1 2 3
foo
(1 2 3)

An example of a standalone script, i.e. not a project, with dependencies: the tree command line tool in Planck.

One caveat is that Planck doesn't support using npm dependencies. So if you need those, go for Lumo which targets NodeJS.

A third option would be joker which is a Clojure interpreter written in Go.

Upvotes: 4

Aleph Aleph
Aleph Aleph

Reputation: 5395

Consider Lumo, a ClojureScript environment which was specially designed for scripting.

Note that while it supports both ClojureScript (JAR) and NPM dependencies, the dependency support is still under development.

Upvotes: 2

Related Questions