新手 Clojure 和 leiningen 问题:
鉴于下面我项目中的代码片段,这适用于 lein repl :
==> (-main "something")
产生预期的“命令:某事......运行......完成”,但不能从命令行工作:
me pallet1]lein run "something"
产生“命令:某事......错误:未作为命令解析”
为什么?/ 我如何解决它?
重现:
lein new eg
然后编辑生成的工程文件,添加:main eg.core
定义main函数,编辑生成的src/eg/core.clj文件,粘贴进去:
核心.clj
(ns eg.core)
(defn something [] (println "Something!"))
(defn run-command-if-any [^String commandname]
(printf "Command: %s ..." commandname)
(if-let [cmd (ns-resolve *ns* (symbol commandname))]
(
(println "running ...") (cmd) (println "done.")
)
(println "error: not resolved as a command.")
))
(defn -main [ commandname ] (run-command-if-any commandname))
然后
lein repl
eg.core=> (-main "something")
工作(即打印“某事!),但 lein run something 没有(即打印“错误:未解决”消息)