Specifying files with spaces in the name to clojure.contrib.command-line

17 views
Skip to first unread message

Kevin Albrecht

unread,
Feb 14, 2009, 3:56:44 PM2/14/09
to Clojure
This utility is very useful, but it seems unable to handle passing in
file names with spaces in them.

Example program:

;--------- begin args.clj
(ns args
(:use clojure.contrib.command-line))

(with-command-line *command-line-args*
"args -- test of args"
[filenames]
(doseq [filename filenames]
(prn filename)))
;---------- end args.clj

I run the program like this:
> clj args.clj ~/src/no_spaces.txt "~/file with spaces.txt"

And the results are this:

"/Users/smith/src/no_spaces.txt"
"~/file"
"with"
"spaces.txt"

When they should be this:

"/Users/smith/src/no_spaces.txt"
"~/file with spaces.txt"

--Kevin Albrecht

P.S. Also, unrelated to this problem, the following line in the
example code in command_line.clj is missing the vector surrounding the
bindings of the doseq:

:else (doseq filename filenames

Michael Wood

unread,
Feb 14, 2009, 4:28:27 PM2/14/09
to clo...@googlegroups.com
On Sat, Feb 14, 2009 at 10:56 PM, Kevin Albrecht <only...@gmail.com> wrote:
>
> This utility is very useful, but it seems unable to handle passing in
> file names with spaces in them.
>
> Example program:
>
> ;--------- begin args.clj
> (ns args
> (:use clojure.contrib.command-line))
>
> (with-command-line *command-line-args*
> "args -- test of args"
> [filenames]
> (doseq [filename filenames]
> (prn filename)))
> ;---------- end args.clj
>
> I run the program like this:
>> clj args.clj ~/src/no_spaces.txt "~/file with spaces.txt"

This works for me. Maybe there's a problem with your "clj" script.

> And the results are this:
>
> "/Users/smith/src/no_spaces.txt"
> "~/file"
> "with"
> "spaces.txt"
>
> When they should be this:
>
> "/Users/smith/src/no_spaces.txt"
> "~/file with spaces.txt"

I get this:

$ clj /tmp/args.clj one two "three three three" four
"one"
"two"
"three three three"
"four"

Does this work for you?

(doseq [arg *command-line-args*]
(prn arg))

And this?

(ns args
(:use clojure.contrib.command-line))

(with-command-line ["one" "two two" "three"]


"args -- test of args"
[filenames]
(doseq [filename filenames]
(prn filename)))

My clj script basically does this:

java -server -cp "${CLASSPATH}" clojure.main "${script}" "$@"

The quotes are necessary around the $@, otherwise you will get the
symptoms you are seeing.

> P.S. Also, unrelated to this problem, the following line in the
> example code in command_line.clj is missing the vector surrounding the
> bindings of the doseq:
>
> :else (doseq filename filenames

That's the old syntax for doseq, so I suspect nobody remembered to
update the example.

--
Michael Wood <esio...@gmail.com>

Kevin Albrecht

unread,
Feb 14, 2009, 8:51:38 PM2/14/09
to Clojure
Ah, this was the problem, thanks.

Chouser

unread,
Feb 14, 2009, 11:19:01 PM2/14/09
to clo...@googlegroups.com
On Sat, Feb 14, 2009 at 3:56 PM, Kevin Albrecht <only...@gmail.com> wrote:
>
> P.S. Also, unrelated to this problem, the following line in the
> example code in command_line.clj is missing the vector surrounding the
> bindings of the doseq:
>
> :else (doseq filename filenames

Fixed, thanks.

--Chouser

Reply all
Reply to author
Forward
0 new messages