Skip to content

Commit 3c1b328

Browse files
committed
Add :coll? flagopts, print multiline flag docs
1 parent 096f2f3 commit 3c1b328

File tree

3 files changed

+41
-17
lines changed

3 files changed

+41
-17
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## Added
44

5+
- Added `:coll?` flagopt for flags that can be specified multiple times
6+
- Correctly print multiline flag docstrings
7+
58
## Fixed
69

710
## Changed

README.md

+13
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ At this point a few things are worth calling out.
188188
as a positional argument rather than a flag. Note that the shell does its own
189189
backspace (character escape) handling, so in practice this means prefixing
190190
with two backslashes, e.g. `\\--foo`.
191+
- For flags that are expected to be passed multiple times, e.g. `-u foo -u bar`,
192+
set `:coll? true`, in that case you will always receive them as a vector, even
193+
if there is only one.
191194

192195
You can also explicitly set which key to use with `:key`, as well as setting a
193196
specific `:value`, for instance:
@@ -269,6 +272,16 @@ must always be passed:
269272
:required true}] })
270273
```
271274

275+
#### Flag option reference
276+
277+
- `:doc` docstring
278+
- `:default` default value
279+
- `:value` value to be associated with the key (for flags that don't take arguments)
280+
- `:handler` handler function, `(fn [opts & flag-args] ,,,)`
281+
- `:middelware` functions which wrap the final command handler
282+
- `:coll?` flag can be specified multiple times, will result in a vector
283+
- `:parse` function used to coerce the flag argument
284+
272285
### Commands
273286

274287
`lambdaisland/cli` is specifically meant for CLI tools with multiple subcommands

src/lambdaisland/cli.clj

+25-17
Original file line numberDiff line numberDiff line change
@@ -71,21 +71,25 @@
7171
has-long? (some long? (mapcat (comp :flags second) flagpairs))]
7272
(println "\nFLAGS")
7373
(print-table
74-
(for [[_ {:keys [flags argdoc required] :as flagopts}] flagpairs]
75-
(let [short (some short? flags)
76-
long (some long? flags)]
77-
[(str (cond
78-
short
79-
(str short ", ")
80-
has-short?
81-
" "
82-
:else
83-
"")
84-
long
85-
argdoc)
86-
(desc flagopts)
87-
(if required "(required)" "")
88-
])))))
74+
(apply
75+
concat
76+
(for [[_ {:keys [flags argdoc doc required] :as flagopts}] flagpairs]
77+
(let [short (some short? flags)
78+
long (some long? flags)]
79+
(into [[(str (cond
80+
short
81+
(str short ", ")
82+
has-short?
83+
" "
84+
:else
85+
"")
86+
long
87+
argdoc)
88+
(desc flagopts)
89+
(if required "(required)" "")]]
90+
(map (fn [l]
91+
["" l ""]))
92+
(next (str/split doc #"\R")))))))))
8993
(when (seq command-pairs)
9094
(println "\nSUBCOMMANDS")
9195
(print-table
@@ -135,9 +139,13 @@
135139
(:value flagspec)
136140
((fnil inc 0) (get opts (:key flagspec))))
137141
(= 1 (count args))
138-
(first args)
142+
(if (:coll? flagspec)
143+
((fnil conj []) (get opts (:key flagspec)) (first args))
144+
(first args))
139145
:else
140-
(vec args))))))))))))
146+
(if (:coll? flagspec)
147+
((fnil into []) (get opts (:key flagspec)) args)
148+
(vec args)))))))))))))
141149

142150
(defn default-parse [s]
143151
(cond

0 commit comments

Comments
 (0)