When a user supplies no command line arguments, I want Hello World to print usage information instead of an error trace.
":";exec clj -m `basename $0 .clj` ${1+"$@"}
":";exit
(ns hello
(:gen-class))
(defn -main
[greetee]
(println (str "Hello " greetee "!")))
$ ./hello.clj Fred
Hello Fred!
$ ./hello.clj
Exception in thread "main" clojure.lang.ArityException: Wrong number of args (0) passed to: hello$-main
at clojure.lang.AFn.throwArity(AFn.java:439)
at clojure.lang.AFn.invoke(AFn.java:35)
at clojure.lang.Var.invoke(Var.java:397)
at clojure.lang.AFn.applyToHelper(AFn.java:159)
at clojure.lang.Var.applyTo(Var.java:518)
at clojure.core$apply.invoke(core.clj:600)
at clojure.main$main_opt.invoke(main.clj:323)
at clojure.lang.FnLoaderThunk.invoke(FnLoaderThunk.java:36)
at clojure.main$main.doInvoke(main.clj:426)
at clojure.lang.RestFn.invoke(RestFn.java:422)
at clojure.lang.FnLoaderThunk.invoke(FnLoaderThunk.java:36)
at clojure.lang.Var.invoke(Var.java:405)
at clojure.lang.AFn.applyToHelper(AFn.java:165)
at clojure.lang.Var.applyTo(Var.java:518)
at clojure.main.main(main.java:37)
(defn -main [& args]
(if-not (first args)
(usage)
(println "Hello" (first args))))
You can improve on this a fair bit, but that's a reasonable outline.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With