0
votes

I've done some coding in clojure inside core.clj which have a -main method that can take 0 or more arguments:

(defn -main
  "dequeue function"
   [& args]

I'm loading this clj file with:

(load-file "src/exercise/core.clj")

And then i'm trying to learn how to call this within repl in order to develop core_test.clj (if there is any tips about how to develop this auto tests, please give me some hints as well). What I'm trying to do now is:

(-main "resources\\sample-input.json" "a")

But this is printing "Arguments 0" which is a message that I told the code to print just to see how many arguments are being passed with

(println "Arguments" (count *command-line-args*))

How am I suposed to do this? Thanks!

1

1 Answers

2
votes

i'm trying to learn how to call this within repl in order to develop core_test.clj

Usually you'd write other functions that are called from -main and test those rather than the app's entry point -main.

But you should be able to call -main like any other function. I have a src/sandbox/main.clj file:

(ns sandbox.main)
(defn -main [& args]
  (prn args))

Starting a REPL in the project folder, I can call -main like this:

(use 'sandbox.main) ;; (load-file "src/sandbox/main.clj") also works
=> nil
(in-ns 'sandbox.main)
=> #object[clojure.lang.Namespace 0x67ccce04 "sandbox.main"]
(-main "some" "args")
;; ("some" "args")
=> nil

There's a key difference in my example though: it's printing -main's args binding; *command-line-args* is nil because you're not running the code from the command line with args, you're running it from a REPL.

Regardless, it's probably a better idea to use an existing library to work with CLI args rather than *command-line-args* directly.