vdj4y
vdj4y

Reputation: 2669

How to make Clojure command line

I am new to Clojure, I wish to create a command line in clojure.

I am using lein, The app is simply waiting for user to type something and when press enter, it will print the line.

I cannot seems to make Clojure wait forever with lein run Is there any other way?

Here is my code.

(defn -main [& args] 
   (read-line)
)

so when I type something and press enter, the whole code stops, I want to take the input of user typing and process it continuously. I mean each time user press enter, he/she should be able to continue to next line and program will run forever.

Upvotes: 1

Views: 257

Answers (1)

cfrick
cfrick

Reputation: 37008

You need to loop for the user inputs then and provide some means to break the loop (yet, ctrl-c also works). E.g.

(loop []
  (let [input (read-line)]
    (if (= input "quit")
      (println "bye")
      (do
        (println "You said: " input)
        (recur)))))

Upvotes: 2

Related Questions