Created
April 15, 2010 21:24
-
-
Save viksit/367681 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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