1.2.0 permalink Arrow_down_16x16

with-command-line

clojure.contrib.command-line

  • (with-command-line args desc cmdspec & body)
Bind locals to command-line args.

1 Example top

  • ;; with-command-line takes:
    
    ;; * args – the list of incoming args we want to process, such as from a main()
    ;;          call
    ;; * desc – the usage description to print with –help
    ;; * cmdspec – a list of options and for each a help description and default value
    ;; * body – the body to execute in the context of the command-line values
    
    ;; Here's a simple example:
    
    (defn -main [& args]
      (with-command-line
        args
        "Usage: wc [-l|-w|-c] [-out out.txt] ..."
        [[lines? l? "Count lines" false]
         [words? w? "Count words" false]
         [chars? c? "Count chars" false]
         [out "The output file"]
         files]
        ...implementation...   ))
    
    
    ;; Here, lines?, words?, chars? specify boolean options.  l?, w?, and c? are 
    ;; alternate short forms for them.  The string is a help description and the last
    ;; value is the default.  out is a text option.  file is a seq of additional
    ;; strings read at the end of the command-line.  
    
    ;; All of the named cmdspec options (lines?, l?, out, etc) are given the value
    ;; specified by reading from the command-line, so you can see this kind of like
    ;; a let form.
Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/contrib/command_line.clj:90 top

(defmacro with-command-line 
  "Bind locals to command-line args."
  [args desc cmdspec & body]
  (let [locals (vec (for [spec cmdspec]
                      (if (vector? spec)
                        (first spec)
                        spec)))]
    `(let [{:strs ~locals :as cmdmap#} (make-map ~args '~cmdspec)]
       (if (cmdmap# "help?")
         (print-help ~desc cmdmap#)
         (do ~@body)))))
Vars in clojure.contrib.command-line/with-command-line: defmacro let apply concat hash-map list seq vector
Used in 0 other vars

Comments top

No comments for with-command-line. Log in to add a comment.