|
109 | 109 | (apply update flags (:key flagspec) f args)) |
110 | 110 | flagspec)) |
111 | 111 |
|
| 112 | +(defn default-parse [s] |
| 113 | + (cond |
| 114 | + (re-find #"^-?\d+$" s) |
| 115 | + (parse-long s) |
| 116 | + (re-find #"^-?\d+\.\d+$" s) |
| 117 | + (parse-double s) |
| 118 | + :else |
| 119 | + s)) |
| 120 | + |
112 | 121 | (defn handle-flag [{:keys [flagmap strict?] :as cmdspec} flag cli-args args flags] |
113 | 122 | (reduce |
114 | 123 | (fn [[cli-args args flags] f] |
115 | 124 | (let [[f arg] (str/split f #"=")] |
116 | | - (if-let [{:keys [argcnt value handler middleware] :as flagspec} (get flagmap f)] |
| 125 | + (if-let [{:keys [argcnt value handler middleware parse] :as flagspec |
| 126 | + :or {parse default-parse}} (get flagmap f)] |
117 | 127 | (cond |
118 | 128 | (or (nil? argcnt) |
119 | 129 | (= 0 argcnt)) |
|
122 | 132 | (update-flag flags flagspec (fnil inc 0)))] |
123 | 133 | (= 1 argcnt) |
124 | 134 | (if arg |
125 | | - [cli-args args (assoc-flag flags flagspec arg)] |
126 | | - [(next cli-args) args (assoc-flag flags flagspec (first cli-args))]) |
| 135 | + [cli-args args (assoc-flag flags flagspec (parse arg))] |
| 136 | + [(next cli-args) args (assoc-flag flags flagspec (parse (first cli-args)))]) |
127 | 137 | :else |
128 | | - [(drop argcnt cli-args) args (assoc-flag flags flagspec (take argcnt cli-args))]) |
| 138 | + [(drop argcnt cli-args) args (assoc-flag flags flagspec (map parse (take argcnt cli-args)))]) |
129 | 139 | (if strict? |
130 | 140 | (parse-error! "Unknown flag: " f) |
131 | 141 | [cli-args args (update-flag flags {:key (keyword (str/replace f #"^-+" ""))} #(or arg ((fnil inc 0) %)))])))) |
|
238 | 248 | cmdspec (assoc cmdspec :flagpairs flagpairs :flagmap flagmap) |
239 | 249 | [cmdspec pos-args flags] (split-flags cmdspec cli-args) |
240 | 250 | flagpairs (get cmdspec :flagpairs)] |
241 | | - (dispatch (merge (meta (:command cmdspec)) cmdspec) pos-args flags))) |
| 251 | + (dispatch (merge (meta (:command cmdspec)) cmdspec) pos-args (merge (reduce #(if-let [d (:default %2)] |
| 252 | + (assoc %1 (:key %2) d) |
| 253 | + %1) {} (map second flagpairs)) |
| 254 | + flags)))) |
242 | 255 | ([{:keys [commands doc argnames command flags flagpairs flagmap] |
243 | 256 | :as cmdspec |
244 | 257 | program-name :name |
|
0 commit comments