Parse command line options in Clojure via Apache Commons CLI.
Usage
(use 'commandline.core) (def command-line-arguments ["-a" "--almost-all" "--block-size" "10" "-c" "-t" "2011-09-25T16:45" "-I" "1,2,3" "FILE"]) (with-commandline [[options arguments] command-line-arguments] [[a all "do not hide entries starting with ."] [A almost-all "do not list implied . and .."] [b escape "print octal escapes for nongraphic characters"] [t time "the time" :time] [nil block-size "use SIZE-byte blocks" :integer "SIZE"] [B ignore-backups "do not list implied entried ending with ~"] [c nil (str "with -lt: sort by, and show, ctime (time of last modification of file status information)\n" "with -l: show ctime and sort by name otherwise: sort by ctime")] [C nil "list entries by columns"] [I ids "list of integers" :integers "IDS"]] ;; The parsed command line options are bound to `options` as a map. (assert (= options {:block-size 10 :escape false :ignore-backups false :almost-all true :I [1 2 3] :A true :time #inst "2011-09-25T16:45:00.000Z" :ids [1 2 3] :B false :C false :all true :c true :b false :t #inst "2011-09-25T16:45:00.000Z" :a true})) ;; All pending arguments are bound to `arguments`. (assert (= arguments ["FILE"])) (print-usage "ls") ;;=> usage: ls [-a] [-A] [-b] [-B] [--block-size <SIZE>] [-c] [-C] [-I <IDS>] [-t <arg>] (print-help "ls") ;;=> usage: ls ;;=> -A,--almost-all do not list implied . and .. ;;=> -a,--all do not hide entries starting with . ;;=> -b,--escape print octal escapes for nongraphic characters ;;=> -B,--ignore-backups do not list implied entried ending with ~ ;;=> --block-size <SIZE> use SIZE-byte blocks ;;=> -c with -lt: sort by, and show, ctime (time of last modification of file status information) ;;=> with -l: show ctime and sort by name otherwise: sort by ctime ;;=> ctime and sort by name otherwise: sort by ctime ;;=> -C list entries by columns )
License
Copyright (C) 2011-2016 r0man
Distributed under the Eclipse Public License, the same as Clojure.