Skip to content

Commit 5c3eae7

Browse files
committed
Handle default/parse
1 parent db018a9 commit 5c3eae7

4 files changed

Lines changed: 35 additions & 15 deletions

File tree

CHANGELOG.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
# Unreleased
22

3-
## Added
4-
5-
## Fixed
3+
Much expanded and improved version, see README for all details. This is
4+
approaching the envisioned scope for this library.
65

7-
## Changed
6+
- take docstring/command from var
7+
- `:strict?` mode
8+
- `:handler` and `:middleware` on flags
9+
- Much improved help text rendering
10+
- More lenient flag parsing
11+
- Add `:default` and `:parse`
812

913
# 0.2.11-alpha (2024-02-08 / 0a352bc)
1014

@@ -24,4 +28,4 @@
2428

2529
- subcommand handling
2630
- rudimentary flag handling
27-
- help text generation
31+
- help text generation

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@ At this point a few things are worth calling out.
161161
- You can use the `--[no-]foo` syntax for adding both a `--foo` and a `--no-foo`
162162
flag, in this case the resulting value in the opts map will be `:foo true` or
163163
`:foo false`
164+
- You can add a `:default` to a flag, like `["--port PORT" {:default 8080}]`
165+
- You can set a `:parse` function which will be used to parse/coerce the
166+
argument. The default will parse numbers (basic longs and doubles, no special
167+
formats), and nothing else.
164168

165169
You can also explicitly set which key to use with `:key`, as well as setting a
166170
specific `:value`, for instance:

bin/cli-test

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,11 @@
4545
{:commands
4646
["ls" {:command #'list-widgets
4747
:flags
48-
["-l, --long"
48+
["--port PORT" {:doc "Port number"
49+
:default 8080}
50+
"-l, --long"
4951
{:doc "Use long format"
50-
:middleware [(fn [cmd]
51-
(fn [opts]
52-
(binding [*format* :long]
53-
(cmd opts))))]}]}]})
52+
}]}]})
5453
#_
5554
(cli/dispatch
5655
{:name "cli-test"

src/lambdaisland/cli.clj

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,21 @@
109109
(apply update flags (:key flagspec) f args))
110110
flagspec))
111111

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+
112121
(defn handle-flag [{:keys [flagmap strict?] :as cmdspec} flag cli-args args flags]
113122
(reduce
114123
(fn [[cli-args args flags] f]
115124
(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)]
117127
(cond
118128
(or (nil? argcnt)
119129
(= 0 argcnt))
@@ -122,10 +132,10 @@
122132
(update-flag flags flagspec (fnil inc 0)))]
123133
(= 1 argcnt)
124134
(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)))])
127137
: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)))])
129139
(if strict?
130140
(parse-error! "Unknown flag: " f)
131141
[cli-args args (update-flag flags {:key (keyword (str/replace f #"^-+" ""))} #(or arg ((fnil inc 0) %)))]))))
@@ -238,7 +248,10 @@
238248
cmdspec (assoc cmdspec :flagpairs flagpairs :flagmap flagmap)
239249
[cmdspec pos-args flags] (split-flags cmdspec cli-args)
240250
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))))
242255
([{:keys [commands doc argnames command flags flagpairs flagmap]
243256
:as cmdspec
244257
program-name :name

0 commit comments

Comments
 (0)