Skip to content

Instantly share code, notes, and snippets.

@viksit
Created April 15, 2010 21:24
Show Gist options
  • Save viksit/367681 to your computer and use it in GitHub Desktop.
Save viksit/367681 to your computer and use it in GitHub Desktop.
;; Thanks to user alanlcode on StackOverflow
;; http://stackoverflow.com/questions/1341154/building-a-clojure-app-with-a-command-line-interface
(ns cmd-line-demo
(:gen-class)
(:use clojure.contrib.command-line))
(defn -main [& args]
(with-command-line args
"Command line demo"
[[foo "This is the description for foo" 1]
[bar "This is the description for bar" 2]
[boolean? b? "This is a boolean flag."]
remaining]
(println "foo: " foo)
(println "bar: " bar)
(println "boolean?: " boolean?)
(println "remaining: " remaining)))
; Compile
;;user> (compile 'cmd-line-demo)
;; cmd-line-demo
; Running it
$ java -classpath . cmd_line_demo
Command line demo
Options
--foo <arg> This is the description for foo [default 1]
--bar <arg> This is the description for bar [default 2]
--boolean, -b This is a boolean flag.
$ java -classpath . cmd_line_demo --foo "changed value"
foo: changed value
bar: 2
boolean?: nil
remaining: []
$ java -classpath . cmd_line_demo -boolean
foo: 1
bar: 2
boolean?: true
remaining: []
$ java -classpath . cmd_line_demo -foo test file1 file2 file3
foo: test
bar: 2
boolean?: nil
remaining: [file1 file2 file3]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment