我想使用clojure/tools.cli和lein bin生成clojure庫的命令行版本。這工作正常,除了我運行腳本時輸出到標準錯誤。這個特定的庫具有重寫某些基本函數的功能,所以編譯clojure代碼時自然會出現警告。我知道這通常是一個糟糕的主意,但經過仔細考慮後,我相信這是最好的選擇。每次運行腳本時,如何阻止這些消息出現?如何在clojure程序中將輸出抑制爲stderr
我添加了slf4j-nop到monger文檔中建議的依賴關係,以禁止來自monger的不需要的消息,並且這可以工作,但對clojure編譯器的警告沒有影響。
我也嘗試使用slf4j日誌詳細在這裏:suppress output from `clojure.tools.logging`但沒有成功。
下面是一些代碼:
project.clj
(defproject image-search "0.1.0-SNAPSHOT"
:description "Search for images containing specific metadata"
:url "http://github.com/soulflyer/image-search"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.7.0"]
[org.clojure/tools.cli "0.3.3"]
[org.slf4j/slf4j-nop "1.7.21"]
[image-lib "0.1.1-SNAPSHOT"]]
:main image-search.command-line
:bin {:name "image-search"
:bin-path "~/bin"})
和command_line.clj:
(ns image-search.command-line
(:require [image-search.core :refer [open all-images ifeq ifin ]]
[clojure.tools.cli :refer :all])
(:gen-class))
(def cli-options
[["-c" "--count" "Counts the results"]
["-D" "--database DATABASE" "specifies database to use"
:default "photos"]
["-I" "--image-collection IMAGE-COLLECTION" "specifies the image collection"
:default "images"]
["-K" "--keyword-collection KEYWORD-COLLECTION" "specifies the keyword collection"
:default "keywords"]
["-h" "--help"]
["-i" "--iso ISO" "Search on ISO value"]
["-s" "--shutter SHUTTER-SPEED" "search on SHUTTER-SPEED"]
["-f" "--aperture APERTURE" "search on APERTURE"]
["-y" "--year YEAR" "search on YEAR"]
["-m" "--month MONTH" "search on MONTH"]
["-M" "--model MODEL" "search by camera model"]
["-p" "--project PROJECT" "search photos in PROJECT"]
["-k" "--keyword KEYWORD" "search for KEYWORD"]])
(defn print-count [pics]
(println (count pics)))
(defn -main
"I don't do a whole lot ... yet."
[& args]
(let [{:keys [options arguments errors summary]} (parse-opts args cli-options)
output-function (if (:count options) print-count open)]
(-> all-images
(ifeq :ISO-Speed-Ratings (:iso options))
(ifeq :Year (:year options))
(ifeq :Month (:month options))
(ifin :Project (:project options))
(ifeq :F-Number (:aperture options))
(ifin :Keywords (:keyword options))
(ifin :Model (:model options))
(output-function))))
我跑雷音垃圾桶,然後運行它產生的可執行文件,並得到像這樣:
(master) image-search: image-search -i 640 -c
WARNING: or already refers to: #'clojure.core/or in namespace: image-search.core, being replaced by: #'image-search.core/or
WARNING: and already refers to: #'clojure.core/and in namespace: image-search.core, being replaced by: #'image-search.core/and
請注意,2警告指的是我在命令行版本中不使用的功能。我甚至不包括他們。當我需要圖書館時,他們不在提供的名單中。但是,當我從複製品或蘋果酒中使用它時,它們對圖書館來說很重要。所以我真的不想重命名它們。