For example, this should run the application:
clj myapp.clj
and this should not:
clj
user=> (load-file "myapp.clj")
Ruby has something like this. You surround the code that runs the app like this:
if $PROGRAM_NAME == __FILE__
# code to run application goes here
end
--
R. Mark Volkmann
Object Computing, Inc.
Good idea! Unfortunately it seems that *command-line-args* is nil
regardless of whether I load the code in a REPL or run it as a script.
It doesn't include the name of the script, just arguments that follow
it.
Am 02.01.2009 um 17:34 schrieb Mark Volkmann:
> For example, this should run the application:
>
> clj myapp.clj
>
> and this should not:
>
> clj
> user=> (load-file "myapp.clj")
You should probably pack everything in a namespace
with a main function.
(ns my.app)
...
(defn main
[& args]
...)
Then you can run the script via:
java -cp <appropriate classpath here> clojure.main my.app
Our you can load the file via load-file or require, without
running it.
Downside: the file must be named according to the
namespace rules.
Sincerely
Meikel
Sounds good. I can't find the function that returns the current
namespace though. Do you remember what it is?
I just learned that the current namespace is stored in the special
variable *ns*.
It's not clear though that this will help. When I add (println "*ns*
=" *ns*) to the top of my .clj file, I get the same output:
*ns* = #<Namespace user>
whether I run it as a script or use (load-file "demo.clj") from a REPL.
You can get the current namespace from *ns*.
k
Am 04.01.2009 um 11:15 schrieb Timothy Pratley:
> I can't seem to get this approach working:
>
> http://clojure.org/compilation seems to imply that this approach can
> only be taken using :gen-class and pre-compiling before calling in
> this way (which makes sense as my.app is not a class yet).
Yes. I found this out after sending the mail. But still clojure.main
does this somehow....
Sincerely
Meikel
Thanks! I've got it working this way now. My script defines a "main"
function. At the bottom of the script I do this:
; Only run the application automatically if run as a script,
; not if loaded in a REPL with load-file.
(if *command-line-args* (main))
There is clojure.contrib.command-line
I don't know if it's elegant, and it's certainly not (yet) as powerful
as many optparse tools in some other languages, but I've found it to
be useful.
--Chouser
I thought this was the go-to library for command line parsing:
https://github.com/clojure/tools.cli
I believe it is, yes. Some history:
Sean Corfield -- (970) FOR-SEAN -- (904) 302-SEAN
An Architect's View -- http://corfield.org/
"If you're not annoying somebody, you're not really alive."
-- Margaret Atwood