繁体   English   中英

在Clojure中从命令行设置调试功能

[英]Setting a debug function from the command line in Clojure

我有这样的命名空间:

(ns foo.core)

(def ^:dynamic *debug-fn*
  "A function taking arguments [bar baz]"
  nil)

(defn bar-info
  [bar _]
  (println bar))

(defn baz-info
  [_ baz]
  (println baz))

(defn do-stuff
  [bar baz]
  (when *debug-fn* (*debug-fn* bar baz)))

(defn -main
  [& {:keys [debug-fn]}]
  (binding [*debug-fn* (symbol debug-fn)]  ;; THIS WON'T WORK!
    (do-stuff 27 42)))

我想做的是允许从命令行指定调试函数,如下所示: lein run bar-infolein run baz-info

我不确定如何将指定的字符串作为命令行参数并将其转换为要绑定的名称空间限定函数。 我需要一个宏来做这个吗?

使用ns-resolve ,您需要指定定义函数的命名空间。

user=> (defn f [n] (* n n n))
#'user/f
user=> ((ns-resolve *ns* (symbol "f")) 10)
1000

使用alter-var-root

user=> (doc alter-var-root)
-------------------------
clojure.core/alter-var-root
([v f & args])
  Atomically alters the root binding of var v by applying f to its
  current value plus any args
nil
user=> (alter-var-root #'*debug-fn* (fn [v] (fn [x] (println x) x)))
#<user$eval171$fn__172$fn__173 user$eval171$fn__172$fn__173@7c93d88e>
user=> (*debug-fn* 1)
1
1

虽然我已经接受了上面的Guillermo的答案 ,但我认为添加我最终使用的解决方案可能也很有用:

(def debug-fns
  {:bar-info (fn [bar _] (println bar))
   :baz-info (fn [_ baz] (println baz))

(def active-debug-fns (atom []))

(defn activate-debug-fn!
  [fn-key]
  (let [f (debug-fns fn-key)]
    (if f
      (swap! active-debug-fns conj f)
      (warn (str "Debug function " fn-key " not found! Available functions are: "
                 (join " " (map name (keys debug-fns))))))))

(defn debug-fn-keys
  [args]
  (if (args "--debug")
    (split (or (args "--debug") "") #",")
    []))

(defn do-stuff
  [bar baz]
  (doseq [f @active-debug-fns]
    (f bar baz)))

(defn -main
  [& args]
  (let [args (apply hash-map args)]
    (doseq [f (debug-fn-keys args)]
      (activate-debug-fn! (keyword k)))
    (do-stuff 27 42)))

所以现在你可以说像lein run --debug bar-info来获取关于酒吧的信息,或者lein run --debug bar,baz来获取关于酒吧和bazes的信息。

任何使这更加惯用的建议都会被愉快地接受和编辑。:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM