Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ways to avoid Exception in thread "main" clojure.lang.ArityException?

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)
like image 233
mcandre Avatar asked Mar 08 '11 02:03

mcandre


1 Answers

(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.

like image 91
amalloy Avatar answered Oct 04 '22 15:10

amalloy