关于clojure:如何在tools.cli中接受选项的其他参数?

How to accept additional arguments for an option in tools.cli?

我是Clojure的新手。

我的cli应用程序的选项-a需要多个参数,例如:

1
java -jar app.jar -a 12 abc xyz

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

我的代码是:

1
2
3
["-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]

其他参数作为parse-opts返回的映射的键:arguments的值列出

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


我认为您不能一次解析一个选项的空格分隔值。
通常,您可以这样操作:-a opt1 -a opt2 -a opt3,但是由于opt1具有不同的类型,因此无法使用。

用逗号分隔它们呢?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
(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