Skip to content

Commit 025991c

Browse files
committed
Improved help, explain why we are showing help (no such command, missing args)
1 parent 4ac1d68 commit 025991c

2 files changed

Lines changed: 30 additions & 14 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
## Added
44

5-
## Fixed
6-
7-
## Changed
5+
- Better help messages: say why we are showing the help (no such command,
6+
missing positional args)
87

98
# 0.9.42 (2024-05-27 / 3b81e73)
109

@@ -100,4 +99,4 @@ approaching the envisioned scope for this library.
10099

101100
- subcommand handling
102101
- rudimentary flag handling
103-
- help text generation
102+
- help text generation

src/lambdaisland/cli.clj

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,11 @@
306306
(recur (dissoc cmdspec :flags) cli-args args (conj seen-prefixes args) flags))
307307

308308
:else
309-
(recur (dissoc cmdspec :flags) cli-args (conj args (str/replace arg #"^\\(.)" (fn [[_ o]] o))) (conj seen-prefixes args) flags)))))
309+
(recur (dissoc cmdspec :flags)
310+
cli-args
311+
(conj args (str/replace arg #"^\\(.)" (fn [[_ o]] o)))
312+
(conj seen-prefixes args)
313+
flags)))))
310314

311315
(defn missing-flags
312316
"Return a set of required flags in `flagmap` not present in `opts`, or `nil` if
@@ -337,7 +341,7 @@
337341

338342
(cond
339343
command
340-
(if (or (:help opts) (< (count pos-args) (count argnames)))
344+
(if (:help opts)
341345
(print-help program-name doc [] argnames flagpairs)
342346
(binding [*opts* (-> opts
343347
(dissoc ::middleware)
@@ -359,7 +363,8 @@
359363
argnames (:argnames command-match)
360364
arg-count (count argnames)]
361365
(cond
362-
command-match
366+
(and command-match
367+
(<= arg-count (count pos-args)))
363368
(dispatch*
364369
(-> cmdspec
365370
(dissoc :command :commands)
@@ -372,13 +377,25 @@
372377

373378
(or (nil? command-match)
374379
(nil? commands)
375-
(:help opts))
376-
(print-help program-name doc (for [[k v] command-pairs]
377-
[k (if (:commands v)
378-
(update v :commands prepare-cmdpairs)
379-
v)])
380-
argnames
381-
flagpairs)
380+
(:help opts)
381+
(< (count pos-args) arg-count))
382+
(do
383+
(cond
384+
(or (nil? command-match) (nil? commands))
385+
(println "No matching command found:" cmd "\n")
386+
(< (count pos-args) arg-count)
387+
(println "Positional arguments missing:"
388+
(->> argnames
389+
(drop (count pos-args))
390+
(map #(str "<" (name %) ">"))
391+
(str/join " "))
392+
"\n"))
393+
(print-help program-name doc (for [[k v] command-pairs]
394+
[k (if (:commands v)
395+
(update v :commands prepare-cmdpairs)
396+
v)])
397+
argnames
398+
flagpairs))
382399

383400
:else
384401
(parse-error! "Expected either :command or :commands key in" cmdspec))))))

0 commit comments

Comments
 (0)