Command line parsing in Emacs

commander.el is a command line parser for Emacs. It avoids messing with command-switch-alist (and friends) and instead defines the schema in an elegant API.

Example schema

Here is a (silly) example where numbers can be added and subtracted.

(require 'commander)

(defvar calc-fn nil)

(defun calc (&rest args)
  (if calc-fn
      (message "%s" (apply calc-fn (mapcar 'string-to-int args)))
    0))

(defun add ()
  (setq calc-fn '+))

(defun sub ()
  (setq calc-fn '-))

(commander
 (option "--help, -h" "Show usage information" commander-print-usage)
 (option "--add" "Add values" add)
 (option "--sub" "Subtract values" sub)
 (command "calc [*]" "Calculate these values" calc))

Example usage

Add list of values

$ carton exec emacs --script math.el -- calc 1 2 3 4 5 --add

15

Subtract list of values

$ carton exec emacs --script math.el -- calc 1 2 3 4 5 --sub

-13

Show usage information

$ carton exec emacs --script math.el -- --help

USAGE: math.el COMMAND [OPTIONS]

COMMANDS:
 calc <*>            Calculate these values

OPTIONS:
 --sub               Subtract values
 --add               Add values
 -h                  Show usage information
 --help              Show usage information

More information is available at Github: https://github.com/rejeep/commander.el


blog comments powered by Disqus Back to Top

Fork me on GitHub