2016-06-09 60 views
1

我想使用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警告指的是我在命令行版本中不使用的功能。我甚至不包括他们。当我需要图书馆时,他们不在提供的名单中。但是,当我从复制品或苹果酒中使用它时,它们对图书馆来说很重要。所以我真的不想重命名它们。

回答

2

您可以通过添加删除警告以下到您的(ns image-search.core)声明:

(ns image-search.core 
    (:refer-clojure :exclude [or and]) 

随着这种变化,你仍然可以使用原来的Clojure的orand通过完全合格他们:

`clojure.core/or` 
`clojure.core/and` 

无改变编译器会警告你重写你的名字空间绑定orand,因为它们默认绑定到的函数和clojure/and变量。