Scope of Command Line Args

20 views
Skip to first unread message

octopusgrabbus

unread,
Jun 20, 2011, 3:26:25 PM6/20/11
to Clojure
In the following program, is the scope of the command line arguments
-- args -- local to with-command-line, or can they be accessed
outside with-command-line?

I think they are local to with-command-line.

(ns test-csv
(:gen-class)
(:use clojure.contrib.command-line)
(:use clojure-csv.core))

(defn process-file
"Process csv file and prints first item in every row"
[file-name]
(let [data (slurp file-name)
rows (parse-csv data)]
(dorun (map #(println (first %)) rows))))

(defn -main [& args]
(with-command-line args
"Get csv file name"
[[file-name ".csv file name" 1]]
(println "file-name:", file-name)
(if file-name
(process-file "resultset.csv")
(process-file file-name))));

Sean Corfield

unread,
Jun 20, 2011, 6:37:28 PM6/20/11
to clo...@googlegroups.com
On Mon, Jun 20, 2011 at 12:26 PM, octopusgrabbus
<octopus...@gmail.com> wrote:
> In the following program, is the scope of the command line arguments
> -- args --  local to with-command-line, or can they be accessed
> outside with-command-line?

args itself is available in the scope of your -main function. The
command line argument you specify, file-name, is available only inside
with-command-line.

If you want the default to be "resultset.csv", why not specify that in
the cmdspec:

(defn -main [& args]
(with-command-line args
"Get csv file name"

[[file-name ".csv file name" "resultset.csv"]]
(println "filename:" file-name)
(process-file file-name))
--
Sean A Corfield -- (904) 302-SEAN
An Architect's View -- http://corfield.org/
World Singles, LLC. -- http://worldsingles.com/
Railo Technologies, Inc. -- http://www.getrailo.com/

"Perfection is the enemy of the good."
-- Gustave Flaubert, French realist novelist (1821-1880)

octopusgrabbus

unread,
Jun 20, 2011, 7:34:27 PM6/20/11
to Clojure
Thanks. Your way of doing it is cleaner.

On Jun 20, 6:37 pm, Sean Corfield <seancorfi...@gmail.com> wrote:
> On Mon, Jun 20, 2011 at 12:26 PM, octopusgrabbus
>
> <octopusgrab...@gmail.com> wrote:
> > In the following program, is the scope of the command line arguments
> > -- args --  local to with-command-line, or can they be accessed
> > outside with-command-line?
>
> args itself is available in the scope of your -main function. The
> command line argument you specify, file-name, is available only inside
> with-command-line.
>
> If you want the default to be "resultset.csv", why not specify that in
> the cmdspec:
>
> (defn -main [& args]
>   (with-command-line args
>     "Get csv file name"
>     [[file-name ".csv file name" "resultset.csv"]]
>     (println "filename:" file-name)
>     (process-file file-name))
> --
> Sean A Corfield -- (904) 302-SEAN
> An Architect's View --http://corfield.org/
> World Singles, LLC. --http://worldsingles.com/
> Railo Technologies, Inc. --http://www.getrailo.com/
Reply all
Reply to author
Forward
0 new messages