Liking cljdoc? Tell your friends :D

Cliopatra

Cliopatra provides functionality to easily define and dispatch to multiple command-line utilities from single or multiple namespace entry points.

Some advantages of Cliopatra:

  • Better than having to write a case expression or multimethod from one namespace on the one hand, or putting each command in a different module entry point on the other.
  • 'Required opts' functionality.
  • Concise and DRY syntax for accessing and dispatching on user-provided values.

Dependency Coordinates

[org.clojars.runa/cliopatra "1.1.0"]

Usage

...

(:require [cliopatra.command :as command :refer [defcommand]])

...

Defining a Command

(defcommand name-of-command
  "documentation string for the command definition"
  {;; usage-heading defaults to command's docstring if not provided
   :usage-heading "documentation for command, prints above usage banner"

   ;; opts-spec = tools.cli opts spec + added required opts feature
   ;; The :required true option in the opt flags is a feature we've
   ;; layered ontop of tools.cli's option semantics to enable options that
   ;; are both named and required.
   :opts-spec [["-f" "--foo" ... :required true]
               ["-b" "--bar"]]

   ;; bind-args-to is for positional non-option args. defaults to: args if
   ;; none is provided. In this case they will be bound to [baz quux].
   :bind-args-to [baz quux]}
  ... body ...)

In the body of defcommand, some useful lexical bindings are made available:

  • usage (the usage heading and banner generated by tools.cli),
  • opts (a map of options and their values),
  • a lexical binding for each sym in bind-args-to (or args if unspecified),
  • and a lexical binding for each opt in opts-spec

Example Usage

(defcommand make-csv
  "Create a file filled with come calculated data"
  {:opts-spec [["--append" "Append to file?" :default true]]
   :bind-args-to [output-file]}
  (spit output-file (calculate-csv) :append append))

(defn -main [& args]
  (command/dispatch 'my-lib.main args))
lein run make-csv my.csv --append true

Can you improve this documentation? These fine people already did:
Robert Levy, Alex Baranosky & Ram Krishnan
Edit on GitHub

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close