Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use command line arguments in Clojure?

I'm working my way through one of my first Clojure programs. The one thing I have left to do is have my program accept command line arguments. The number of args can vary (but needs to be at least one), and then each command line arg needs to be provided as an argument to a function in my main, one at a time. I've been reading online and it seems that clojure/tools.cli is the way to do it, (using parse-opts maybe?). But I can't figure it out for the life of me. There's no validation, really that needs to happen -- whatever the user would provide would be valid. (The only thing that needs to be checked is that there is at least one argument provided). Any suggestions on how to go about this?

All the examples I run into seem very complicated and go over my head very easily.

An easy example of what I'm trying to do is after a user provides any number of command line arguments, then have clojure print each string in a new line of the terminal.

I use leiningen to run my programs.

like image 580
orangeorangepeel Avatar asked Apr 27 '18 23:04

orangeorangepeel


1 Answers

Since your entire question seems to boil down to:

An easy example of what I'm trying to do is after a user provides any number of command line arguments, then have clojure print each string in a new line of the terminal.

I'll answer that. That can be done fairly succinctly:

(defn -main [& args] ; & creates a list of var-args
  (if (seq args)
    ; Foreach arg, print the arg...
    (doseq [arg args]
      (println arg))

    ; Handle failure however here
    (throw (Exception. "Must have at least one argument!"))))

Note, unless you absolutely want to fail outright when 0 arguments are passed, you can skip the seq check. doseq won't run if args is empty since there isn't anything to iterate over.

You can also replace the whole doseq with:

(mapv println args)

But that's arguably an abuse of mapv.

like image 94
Carcigenicate Avatar answered Nov 15 '22 08:11

Carcigenicate