Skip to content

Commit e55f751

Browse files
authored
Merge pull request #62 from hlship/hls/20260303-command-and-group-help
For combined command/group: show command usage after group help
2 parents d4c6bc5 + 4c6a991 commit e55f751

File tree

6 files changed

+30
-24
lines changed

6 files changed

+30
-24
lines changed

CHANGES.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
*BREAKING CHANGES*
44

5-
* JDK 17 is now required when using Clojure
65
* Groups are now defined in the options passed to `net.lewisship.cli-tools/dispatch`, not in
76
namespace metadata
87
* Command names are matched as prefixes (not substrings)

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ Documentation starts with [the overview](doc/overview.md).
5757
## Compatibility
5858

5959
`cli-tools` is compatible with Clojure 1.11 and above, and w/ Babashka.
60-
For Clojure, it requires JDK 17 or above.
6160

6261
## License
6362

src/net/lewisship/cli_tools.clj

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,16 +170,12 @@
170170
;; it can also be a map with key :options
171171
test-mode?# (impl/command-map? args#)
172172
command-spec# ~(select-keys parsed-interface parse-cli-keys)]
173+
;; introspection mode is used primarily by completions; it invokes the function with no arguments
174+
;; (but with the flag on) and the command-spec is returned, from which completions are generated.
173175
(if impl/*introspection-mode*
174176
command-spec#
175-
(let [~command-map-symbol (cond
176-
test-mode?#
177+
(let [~command-map-symbol (if test-mode?#
177178
{:options (first args#)}
178-
179-
impl/*introspection-mode*
180-
command-spec#
181-
182-
:else
183179
(impl/parse-cli ~command-name'
184180
~docstring
185181
args#

src/net/lewisship/cli_tools/cache.cljc

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
[clj-commons.ansi :refer [perr]]
1010
#?(:bb [babashka.classpath :as cp]))
1111
(:import (java.io File)
12-
(java.util HexFormat)
1312
(java.nio ByteBuffer)
1413
(java.security MessageDigest)))
1514

@@ -59,10 +58,10 @@
5958
;; Adding or removing a file (even if no other files are touched) will change the digest.
6059
(update-digest-recursively digest f))))
6160

62-
(defn- hex-string
63-
[^bytes input]
64-
(-> (HexFormat/of)
65-
(.formatHex input)))
61+
(defn- hex-string [^bytes input]
62+
(let [sb (StringBuilder.)]
63+
(run! #(.append sb (format "%X" %)) input)
64+
(str sb)))
6665

6766
(defn classpath-digest
6867
"Passed the tool options, return a hex string of the SHA-1 digest of the files from the classpath and

src/net/lewisship/cli_tools/impl.clj

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -906,11 +906,14 @@
906906
invoke-last-command-fn nil]
907907
(cond-let
908908
(#{"-h" "--help"} term)
909-
(if invoke-last-command-fn
910-
(invoke-last-command-fn)
911-
(do
912-
(print-commands nil container-map commands-map false)
913-
(exit 0)))
909+
(do
910+
(print-commands nil container-map commands-map false)
911+
;; This is the ugly mixed case where its a group and a command; we present
912+
;; the group first then the command.
913+
(when invoke-last-command-fn
914+
(println)
915+
(invoke-last-command-fn))
916+
(exit 0))
914917

915918
:let [possible-commands commands-map
916919
matchable-terms (keys possible-commands)]

test/net/lewisship/cli_tools/messy_test.clj

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,21 @@
3939
(dispatch "mess" "kiwi"))))
4040

4141
(deftest help-for-messy-command-shows-command-help
42-
(let [{:keys [status out]} (dispatch "mess" "-h")]
43-
(is (= 0 status))
44-
(is (string/includes? out "Usage:"))
45-
(is (string/includes? out "NAME"))
46-
(is (not (string/includes? out "nested")))))
42+
(is (match? {:status 0
43+
:out-lines ["bigmess messy - Messy command and group at same time"
44+
""
45+
"Commands:"
46+
" nested: Command nested under messy group/command"
47+
""
48+
"Usage: bigmess messy [OPTIONS] NAME"
49+
"Messy command."
50+
""
51+
"Options:"
52+
" -h, --help This command summary"
53+
""
54+
"Arguments:"
55+
" NAME: Name to print"]}
56+
(dispatch "mess" "-h"))))
4757

4858
(comment
4959

0 commit comments

Comments
 (0)