On Jan 16, 2009, at 8:37 AM, Stephen C. Gilardi wrote:

I like this. I've also seen several requests for a way to know when code is running in a script which could be satisfied by this as well.

For clarity, here's what I'm proposing based on recent discussion in this thread:

[1] When clojure.main runs a script, it will set *command-line-script* to the script name as it appears on the command line.

Example:

java -cp clojure.jar clojure.main -i dir-utils.clj list- dir.clj :verbose :depth 3

will run with these values set:

        *command-line-script* "list-dir.clj"
        *command-line-args* (":verbose" ":depth" "3")

Note that dir-utils.clj is an "init file" here (there can be any number of them, each introduced by a "-i"), while list-dir.clj is the main script.

[2] When clojure.lang.Script runs a script, the name immediately before either the end of line or the "--" will be the script name. Here's the equivalent to above:

java -cp clojure.jar clojure.lang.Script dir-utils.clj list-dir.clj -- :verbose :depth 3

(same values set):

        *command-line-script* "list-dir.clj"
        *command-line-args* (":verbose" ":depth" "3")

Note that in this case, files are still loaded in order, but "init" files are not "marked". list-dir.clj is considered the main script because it's the last to appear before the "--" which marks the beginning of args.

[3] *command-line-script* will be nil in other cases: when clojure.main launches a repl, when using clojure.lang.Repl, or when clojure.main reads a script from *in*.

Comments welcome.

--Steve

Attachment: smime.p7s
Description: S/MIME cryptographic signature

Reply via email to