Command line arguments and 'perl -e' kind of usage.

113 views
Skip to first unread message

pj

unread,
Jul 10, 2008, 12:43:21 PM7/10/08
to Clojure
Greetings

I couldn't figure out how to pass command line arguments to a clojure
script. (the usual argc, argv stuff).
Also it would be really useful if we can have things similar to "perl
-e '(snippet of code)'".

Are there any plans to implement such things ?

Appreciate your time.
Thanks
pj

Allen Rohner

unread,
Jul 10, 2008, 1:49:55 PM7/10/08
to Clojure
From this thread, it looks like there have been attempts at it:
http://groups.google.com/group/clojure/browse_thread/thread/9824e327a70d0528/a846295760e22496?lnk=gst&q=script#a846295760e22496

I expect that there will be a standard way to do it at some point. Of
course, the best way to predict the future is to create it. :-)

Allen

Craig McDaniel

unread,
Jul 10, 2008, 5:06:21 PM7/10/08
to Clojure
If you're on a unix-like system, this example script supports command
line arguments:

http://en.wikibooks.org/wiki/Clojure_Programming#Shebang_Scripting_in_Clojure

It works for me on Linux, but I'd couldn't find any documentation on
how they did that trick of having a script file not start with "#!".

-Craig

On Jul 10, 1:49 pm, Allen Rohner <aroh...@gmail.com> wrote:
> On Jul 10, 11:43 am, pj <pjdtech2...@gmail.com> wrote:
>
> > Greetings
>
> > I couldn't figure out how to pass command line arguments to a clojure
> > script. (the usual argc, argv stuff).
> > Also it would be really useful if we can have things similar to  "perl
> > -e '(snippet of code)'".
>
> > Are there any plans to implement such things ?
>
> > Appreciate your time.
> > Thanks
> > pj
>
> From this thread, it looks like there have been attempts at it:http://groups.google.com/group/clojure/browse_thread/thread/9824e327a...

Chouser

unread,
Jul 10, 2008, 5:46:41 PM7/10/08
to clo...@googlegroups.com
On Thu, Jul 10, 2008 at 5:06 PM, Craig McDaniel <crai...@gmail.com> wrote:
>
> If you're on a unix-like system, this example script supports command
> line arguments:
>
> http://en.wikibooks.org/wiki/Clojure_Programming#Shebang_Scripting_in_Clojure
>
> It works for me on Linux, but I'd couldn't find any documentation on
> how they did that trick of having a script file not start with "#!".

That's deeply clever and a little scary. I had to call in my
bash-guru friend to pick it apart.

Because it does not start with #!, the whole file gets passed to /bin/sh
/bin/sh sees the first line starts with # and skips it as a comment,
and then goes on to run the "exec java" line. Since it's "exec",
/bin/sh never gets any farther.

The exec line sends the path of your script to Clojure, which loads
and runs the whole thing from the top. In Clojure, # does not mean a
comment, but #^ is used for metadata. So the ":shebang" part doesn't
really matter except that it starts with : so it's a Clojure keyword.
The #^ metadata form wants to be followed by something, and it this
case its a quoted vector '[...] The exec line that meant something to
/bin/sh is now seen by Clojure as a series of symbols and strings to
be stored in the vector and tagged with the :shebang metadata. ...but
nothing is done with this value, so the rest of your Clojure code gets
run unhindered.

This was apparently added to the wiki by "Digash" -- impressive!

--Chouser

Monsieur Pinocchio

unread,
Jul 11, 2008, 2:53:50 AM7/11/08
to clo...@googlegroups.com
How about using 'here' documents in bash? A shebang script now looks like

#!/bin/bash
exec java -cp path_to/clojure.jar java.lang.Repl << END

<your clojure program>

END

--
Pinocchio

Randall R Schulz

unread,
Jul 11, 2008, 8:57:16 AM7/11/08
to clo...@googlegroups.com

Be aware that as shown, variable references, including positional ($1,
$2 etc.) local and environment will be expanded in the here-document.
If you quote the terminating symbol immediately following the "<<"
operator, the body of the here document is taken literally.


Randall Schulz

Dimitry Gashinsky

unread,
Jul 11, 2008, 12:54:30 PM7/11/08
to clo...@googlegroups.com
"C" == Chouser <cho...@gmail.com> writes:

C> This was apparently added to the wiki by "Digash" -- impressive!

Thank you for explaining it so well!

Regards,
DiGash
--
,`,`,`,`,`,`,`,`,`,`,`,`,
,`,`,`,` @ `,`,`,` .com ,
, Dimitry Gashinsky ,`,`,
,`,`,`,`,`,`,`,`,`,`,`,`,

Dudley Flanders

unread,
Jul 11, 2008, 1:33:31 PM7/11/08
to clo...@googlegroups.com

On Jul 10, 2008, at 11:43 AM, pj wrote:

>
> Greetings
>
> I couldn't figure out how to pass command line arguments to a clojure
> script. (the usual argc, argv stuff).
> Also it would be really useful if we can have things similar to "perl
> -e '(snippet of code)'".
>
> Are there any plans to implement such things ?


Any arguments you pass to Clojure after a '--' will be available as
*command-line-args*, for example:

dudley@hiro :> java -cp clojure.jar clojure.lang.Repl -- a b c
Clojure
user=> (prn *command-line-args*)
("a" "b" "c")

Rich has said on IRC that he's not averse to having a richer command-
line interface in Clojure, but it's not something he needs or uses, so
he's not planning on adding it himself (Rich, please correct me if
I've misunderstood). If we want more advanced stuff (like "-e") we'll
have to write it.

:dudley

PJ Durai

unread,
Jul 11, 2008, 4:47:05 PM7/11/08
to clo...@googlegroups.com
On 7/11/08, Dudley Flanders <dud...@misnomer.us> wrote:
>
>
> On Jul 10, 2008, at 11:43 AM, pj wrote:
>
> >
> > Greetings
> >
> > I couldn't figure out how to pass command line arguments to a clojure
> > script. (the usual argc, argv stuff).
> > Also it would be really useful if we can have things similar to "perl
> > -e '(snippet of code)'".
> >
> > Are there any plans to implement such things ?
>
>
> Any arguments you pass to Clojure after a '--' will be available as
> *command-line-args*, for example:
>
> dudley@hiro :> java -cp clojure.jar clojure.lang.Repl -- a b c
> Clojure
> user=> (prn *command-line-args*)
> ("a" "b" "c")

Yep. That was what I was looking for. That will do for the moment.

>
> Rich has said on IRC that he's not averse to having a richer command-
> line interface in Clojure, but it's not something he needs or uses, so
> he's not planning on adding it himself (Rich, please correct me if
> I've misunderstood). If we want more advanced stuff (like "-e") we'll
> have to write it.

Understood. I havn't taken a look at the implementation details at all
yet. Right now I am having too much fun rewriting my JRuby/Jython
scripts in Clojure. (I have tried ABCL with JFLI, but somehow Clojure
seems really right.)

thanks
pj

Reply all
Reply to author
Forward
0 new messages