|
82 | 82 | (desc cmdopts)])))) |
83 | 83 |
|
84 | 84 | (defn parse-error! [& msg] |
85 | | - (throw (ex-info (str/join " " msg) {:type ::parse-error}))) |
| 85 | + (println "[FATAL]" (str/join " " msg)) |
| 86 | + (System/exit 1) |
| 87 | + #_(throw (ex-info (str/join " " msg) {:type ::parse-error}))) |
86 | 88 |
|
87 | 89 | (defn add-middleware [opts {mw :middleware}] |
88 | 90 | (let [mw (if (or (nil? mw) (sequential? mw)) mw [mw])] |
|
100 | 102 | (assoc flags (:key flagspec) (:value flagspec))) |
101 | 103 | flagspec)) |
102 | 104 | ([flags flagspec & args] |
| 105 | + (prn flags flagspec args) |
103 | 106 | (add-middleware |
104 | 107 | (if-let [handler (:handler flagspec)] |
105 | 108 | (apply call-handler handler flags args) |
|
129 | 132 | (reduce |
130 | 133 | (fn [[cli-args args flags] f] |
131 | 134 | (let [[f arg] (str/split f #"=")] |
132 | | - (if-let [{:keys [argcnt value handler middleware parse] :as flagspec |
| 135 | + (if-let [{:keys [argcnt flagstr value handler middleware parse] :as flagspec |
133 | 136 | :or {parse default-parse}} (get flagmap f)] |
134 | 137 | (cond |
135 | 138 | (or (nil? argcnt) |
|
138 | 141 | (assoc-flag flags flagspec) |
139 | 142 | (update-flag flags flagspec (fnil inc 0)))] |
140 | 143 | (= 1 argcnt) |
141 | | - (if arg |
| 144 | + (cond |
| 145 | + arg |
142 | 146 | [cli-args args (assoc-flag flags flagspec (parse arg))] |
143 | | - [(next cli-args) args (assoc-flag flags flagspec (parse (first cli-args)))]) |
| 147 | + (first cli-args) |
| 148 | + [(next cli-args) args (assoc-flag flags flagspec (parse (first cli-args)))] |
| 149 | + :else |
| 150 | + (parse-error! flagstr "expects an argument, but no more positional arguments available.")) |
144 | 151 | :else |
145 | 152 | [(drop argcnt cli-args) args (assoc-flag flags flagspec (map parse (take argcnt cli-args)))]) |
146 | 153 | (if strict? |
|
231 | 238 | (merge flagopts))))) |
232 | 239 |
|
233 | 240 | (defn parse-flagstrs [flagpairs] |
234 | | - (into {"--help" {:key :help :value true}} |
| 241 | + (into {"-h" {:key :help :value true} |
| 242 | + "--help" {:key :help :value true}} |
235 | 243 | (comp |
236 | 244 | (mapcat build-flagmap-entries) |
237 | 245 | (map (juxt :flag identity))) |
|
0 commit comments