如何为tools.cli中的选项接受其他参数?

时间:2017-12-26 12:47:38

标签: clojure clojure-contrib

我是一个Clojure新手。

我的cli app的选项// Convert nested arrays to a simple array $array = array(); array_walk_recursive($input, function ($a) use (&$array) { $array[] = $a; }); sort($array); $hash = md5(json_encode($array)); ---- These arrays have the same hash: $arr1 = array(0 => array(1, 2, 3), 1, 2); $arr2 = array(0 => array(1, 3, 2), 1, 2); 需要多个参数,例如:

-a

第一个是数字,其他两个必须是字符串。

我的代码是:

java -jar app.jar -a 12 abc xyz

但是我发现传递给["-a" "--add LINE TYPE ENTRY" "Add entry to specified line number of the menu" :parse-fn #(split % #" ") :validate [#(number? (Integer/parseInt (first %))) "ERROR: Invalid position"] 函数的%是一个只包含第一个参数的向量,即:parse-fn

其他参数列为[12]

返回的地图的键:arguments的值

现在,
(1)有没有办法验证那些未经处理的参数?
(2)如何检索和使用这些参数?

1 个答案:

答案 0 :(得分:1)

我认为你不能一次解析一个选项的空格分隔值。 通常你会这样做:-a opt1 -a opt2 -a opt3,但由于你有opt1的不同类型,这将无效。

用逗号分隔它们怎么样?

(require '[clojure.tools.cli :refer [parse-opts]])

(def cli-opts
  [["-a" "--add LINE TYPE ENTRY" "Add entry to specified line number of the menu"
    :parse-fn (fn [a-args]
                (-> a-args
                    (str/split #",")
                    (update 0 #(Integer/parseInt %))))
    :validate [(fn [[num s1 s2]]
                 (and (number? num)
                      (string? s1)
                      (string? s2)))]]])

(parse-opts ["-a" "12,abc,xyz"] cli-opts)

;;=> {:options {:add [12 "abc" "xyz"]}, :arguments [], :summary "  -a, --add LINE TYPE ENTRY  Add entry to specified line number of the menu", :errors nil}

另一种选择是为a引入两个或三个不同的选项:--line--type--entry

相关问题