I'd like to create Clojure class that can be run both via main
method and as a script. I need no command line arguments in both cases. Currently, I have something like this:
(ns my-namespace.core
(:gen-class))
(defn -main
[& args]
(println "Hi"))
; I'd like to have something like (when (in-script?) (-main))
(-main)
In this form it is runnable as script but running it as a class shows Hi
twice, not surprisingly.
I expected to find some way to distinguish runs similar to Python's idiom if __name__ == "__main__":
. But I've only found old discussion mentioning this. Unfortunately, approach with *command-line-args*
described there seems to be not working. I have it nil
regardless of running as class or as script.
So is there a way to find out in runtime if Clojure file is running as a script or via main
method?