CLI

is a simple CLI library for Carp.

(load "https://veitheller.de/git/carpentry/cli@0.0.6")

(defn main []
  (let [p (=> (CLI.new @"My super cool tool!")
              (CLI.add &(CLI.int "flag" "f" "my flag" true))
              (CLI.add &(CLI.str "thing" "t" "my thing" false @"hi" &[@"a" @"b" @"hi"])))]
    (match (CLI.parse &p)
      (Result.Success flags)
        (println* &(str &(Map.get &flags "flag")) " " &(str &(Map.get &flags "thing")))
      (Result.Error msg) (do (IO.errorln &msg) (CLI.usage &p)))))

Installation

(load "https://veitheller.de/git/carpentry/cli@0.0.7")

Usage

CLI should be built using combinators, as in the example above. It has, as of now, three option types: integrals (longs), floating point numbers (doubles), and strings. They can be built using CLI.int, CLI.float, CLI.bool, and CLI.str, respectively. Their structure is always the same, except for booleans:

(CLI.int <long> <short> <description> <required?>)
; or
(CLI.int <long> <short> <description> <required?> <default>)
; or
(CLI.int <long> <short> <description> <required?> <default> <options-array>)

You’ll have to set a default if you want to specify options, although you can set it to (Maybe.Nothing) if you want to make sure that it has to be set manually.

Booleans neither take defaults nor options. If a boolean flag receives a value, it will be read as true unless it’s the string false.

Once you’re done building your flag structure, you can run CLI.parse. It will not abort the program on error, instead it will tell you what went wrong in a Result.Error. If it succeeds, the Result.Success contains a Map from the long flag name to the value. The values are not in the map if they are unset.

CmdMap

module

Module

Option

module

Module

is the option type. To construct an Option, please use int, float, or str.

Parser

module

Module

is the parser type. To construct a Parser, please use new.

add

defn

(λ [Parser, (Ref Option a)] Parser)

                    (add p opt)
                

adds an Option opt to the Parser p.

bool

macro

Macro

                    (bool long short description)
                

creates a boolean option.

float

macro

Macro

                    (float long short description required :rest default-options)
                

creates a integer option. The actual type is a Double.

int

macro

Macro

                    (int long short description required :rest default-options)
                

creates a integer option. The actual type is a Long.

new

defn

(λ [String] Parser)

                    (new descr)
                

creates a new Parser with a program description descr.

parse

defn

(λ [(Ref Parser a)] (Result (Map String Type) String))

                    (parse p)
                

parses the arguments as specified by the parser p.

If everything goes right, it will return a Success containing a map from the long arguments to their values. Because values can be optional, they are returned as Maybe.

Otherwise it will return an Error containing an error message. If that error mesage is empty, --help was requested. If you don’t want to provide a --help feature, you can override that flag.

str

macro

Macro

                    (str long short description required :rest default-options)
                

creates a string option.

usage

defn

(λ [(Ref Parser a)] ())

                    (usage p)
                

takes a Parser p and prints its usage information.