Skip to content

Commit c9696a0

Browse files
committed
Make -h handle help, nicer error message+exit handling
1 parent f5676ff commit c9696a0

2 files changed

Lines changed: 18 additions & 7 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
## Added
44

5-
## Fixed
5+
- `-h` can now be used to get help, in addition to `--help`
66

77
## Changed
88

9+
- When encountering parse errors (invalid arguments), print a message and exit,
10+
rather than throwing (which looks quite ugly from a bb script)
11+
912
# 0.5.27 (2024-02-26 / 28f559d)
1013

1114
## Added
@@ -70,4 +73,4 @@ approaching the envisioned scope for this library.
7073

7174
- subcommand handling
7275
- rudimentary flag handling
73-
- help text generation
76+
- help text generation

src/lambdaisland/cli.clj

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@
8282
(desc cmdopts)]))))
8383

8484
(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})))
8688

8789
(defn add-middleware [opts {mw :middleware}]
8890
(let [mw (if (or (nil? mw) (sequential? mw)) mw [mw])]
@@ -100,6 +102,7 @@
100102
(assoc flags (:key flagspec) (:value flagspec)))
101103
flagspec))
102104
([flags flagspec & args]
105+
(prn flags flagspec args)
103106
(add-middleware
104107
(if-let [handler (:handler flagspec)]
105108
(apply call-handler handler flags args)
@@ -129,7 +132,7 @@
129132
(reduce
130133
(fn [[cli-args args flags] f]
131134
(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
133136
:or {parse default-parse}} (get flagmap f)]
134137
(cond
135138
(or (nil? argcnt)
@@ -138,9 +141,13 @@
138141
(assoc-flag flags flagspec)
139142
(update-flag flags flagspec (fnil inc 0)))]
140143
(= 1 argcnt)
141-
(if arg
144+
(cond
145+
arg
142146
[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."))
144151
:else
145152
[(drop argcnt cli-args) args (assoc-flag flags flagspec (map parse (take argcnt cli-args)))])
146153
(if strict?
@@ -231,7 +238,8 @@
231238
(merge flagopts)))))
232239

233240
(defn parse-flagstrs [flagpairs]
234-
(into {"--help" {:key :help :value true}}
241+
(into {"-h" {:key :help :value true}
242+
"--help" {:key :help :value true}}
235243
(comp
236244
(mapcat build-flagmap-entries)
237245
(map (juxt :flag identity)))

0 commit comments

Comments
 (0)