如何在Clojure中将CLI参数向量分成几对?

时间:2019-07-01 22:38:59

标签: clojure command-line-interface

我有ORA-22813,并希望以-m 4 -y 2016或地图结尾。如何将它们分成几对?

2 个答案:

答案 0 :(得分:2)

要将它们分成几对,请使用partition

val

要制作地图,请apply hash-map对其进行映射:

dev=> (partition 2 ["-m" 4 "-y" 2016])
(("-m" 4) ("-y" 2016))

答案 1 :(得分:2)

有一个special library for that

(let [cli-options [["-m" "--max MAX" "M description"
                        :parse-fn #(Integer/parseInt %)]
                       ["-y" "--year YEAR" "Year"
                        :parse-fn #(Integer/parseInt %)]]]
        (-> "-m 4 -y 2016"
            (str/split #"\s")
            (parse-opts cli-options)))
=>
{:options {:max 4, :year 2016},
 :arguments [],
 :summary "  -m, --max MAX    M description\n-y, --year YEAR  Year",
 :errors nil}