我想使用clojure / tools.cli和lein bin生成一个clojure库的命令行版本。这工作正常,除了我在运行脚本时输出到stderr。这个特殊的库具有覆盖一些基本功能的功能,所以当编译clojure代码时自然会有警告。我知道这通常是一个坏主意,但经过仔细考虑后,我相信这是最好的方式。如何在每次运行脚本时停止显示这些消息?
我按照monger文档中的建议将slf4j-nop添加到依赖项中,以禁止来自monger的不需要的消息,这样做有效,但对clojure编译器的警告没有影响。
我也尝试过使用slf4j-log,如下所示: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))))
我运行lein bin,然后运行它生成的可执行文件并得到类似的内容:
(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警告是指我在命令行版本中不使用的功能。我甚至不包括他们。当我:需要库时,它们不在列表中。但是,当我从repl或者苹果酒中使用它们时,它们对于库来说很重要。所以我真的不想重命名它们。
答案 0 :(得分:3)
您可以通过在(ns image-search.core)
声明中添加以下内容来删除警告:
(ns image-search.core
(:refer-clojure :exclude [or and])
通过完全符合条件,您仍然可以使用原始Clojure or
和and
:
`clojure.core/or`
`clojure.core/and`
如果没有这种更改,编译器会警告您,您正在覆盖or
和and
的命名空间绑定,因为它们默认绑定到clojure.core/or
和clojure/and
vars的函数