octopusgrabbus
octopusgrabbus

Reputation: 10685

How do access default parameters in Clojure cli?

I am wondering how I can access the default parameters in my opts, after they've been parased by cli args. I am invoking my program

./rexfer --ifn1 --ifm1 --ofn1 --ofn3

(defn parse-args
  "Using the newer cli library, parses command line args."
  [args]
  (cli args
       ["-r" "--ifn1" ".csv input file"  :default "standfiletrans_acre.csv"]
       ["-p" "--ifn2" ".csv input file"  :default "pp_report_to_change.csv"]
       ["-m" "--ifm1" ".csv input file"  :default "columns.csv"]
       ["-v" "--ofn1" ".csv output file" :default "re_values.csv"]
       ["-x" "--ofn2" ".csv output file" :default "re_pp.csv"]
       ["-u" "--ofn3" ".csv output file" :default "re_mixed_use_ratio.csv"]
       ["-t" "--rpt"  ".csv pipe delimited output file" :default "xfer.csv"]))

(defn -main 
  [& args]
  (let [[opts args banner] (parse-args args)

This is the output of opts after being parsed

{:rpt xfer.csv, :ofn3 re_mixed_use_ratio.csv, :ofn2 re_pp.csv, :ofn1 --ofn3, :ifm1 --ifn1, :ifn2 pp_report_to_change.csv, :ifn1 standfiletrans_acre.csv}

(:ifn1 opts) returns --ifn1, not standfiletrans_acre.csv.

Upvotes: 1

Views: 162

Answers (1)

Arthur Ulfeldt
Arthur Ulfeldt

Reputation: 91534

The :default keyword is used when the argument is not given at all. If you add the argument, don't supply a value, and then supply the next argument it will read the next argument as the value supplied to the first argument:

arthur@a:~/args/src/args$ lein run --ifn1 --FOO --ifn2 --ifm1  
All namespaces already :aot compiled.
options are:                                                
{:rpt "xfer.csv"
 :ofn3 "re_mixed_use_ratio.csv",
 :ofn2 "re_pp.csv", 
 :ofn1 "re_values.csv",
 :ifm1 "columns.csv", 
 :ifn2 "--ifm1",   
 :ifn1 "--FOO"} 
arguments are: 
[] 
the banner text is: 
 Usage:                             

 Switches    Default                  Desc           
 --------    -------                  ----   
 -r, --ifn1  standfiletrans_acre.csv  .csv input file 
 -p, --ifn2  pp_report_to_change.csv  .csv input file 
 -m, --ifm1  columns.csv              .csv input file   
 -v, --ofn1  re_values.csv            .csv output file 
 -x, --ofn2  re_pp.csv                .csv output file 
 -u, --ofn3  re_mixed_use_ratio.csv   .csv output file                      
 -t, --rpt   xfer.csv                 .csv pipe delimited output file   

In this example the :default value is only used for the values not on the list.

Upvotes: 2

Related Questions