Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

*BREAKING CHANGES*

* JDK 17 is now required when using Clojure
* Groups are now defined in the options passed to `net.lewisship.cli-tools/dispatch`, not in
namespace metadata
* Command names are matched as prefixes (not substrings)
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ Documentation starts with [the overview](doc/overview.md).
## Compatibility

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

## License

Expand Down
10 changes: 3 additions & 7 deletions src/net/lewisship/cli_tools.clj
Original file line number Diff line number Diff line change
Expand Up @@ -170,16 +170,12 @@
;; it can also be a map with key :options
test-mode?# (impl/command-map? args#)
command-spec# ~(select-keys parsed-interface parse-cli-keys)]
;; introspection mode is used primarily by completions; it invokes the function with no arguments
;; (but with the flag on) and the command-spec is returned, from which completions are generated.
(if impl/*introspection-mode*
command-spec#
(let [~command-map-symbol (cond
test-mode?#
(let [~command-map-symbol (if test-mode?#
{:options (first args#)}

impl/*introspection-mode*
command-spec#

:else
(impl/parse-cli ~command-name'
~docstring
args#
Expand Down
9 changes: 4 additions & 5 deletions src/net/lewisship/cli_tools/cache.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
[clj-commons.ansi :refer [perr]]
#?(:bb [babashka.classpath :as cp]))
(:import (java.io File)
(java.util HexFormat)
(java.nio ByteBuffer)
(java.security MessageDigest)))

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

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

(defn classpath-digest
"Passed the tool options, return a hex string of the SHA-1 digest of the files from the classpath and
Expand Down
13 changes: 8 additions & 5 deletions src/net/lewisship/cli_tools/impl.clj
Original file line number Diff line number Diff line change
Expand Up @@ -906,11 +906,14 @@
invoke-last-command-fn nil]
(cond-let
(#{"-h" "--help"} term)
(if invoke-last-command-fn
(invoke-last-command-fn)
(do
(print-commands nil container-map commands-map false)
(exit 0)))
(do
(print-commands nil container-map commands-map false)
;; This is the ugly mixed case where its a group and a command; we present
;; the group first then the command.
(when invoke-last-command-fn
(println)
(invoke-last-command-fn))
(exit 0))

:let [possible-commands commands-map
matchable-terms (keys possible-commands)]
Expand Down
20 changes: 15 additions & 5 deletions test/net/lewisship/cli_tools/messy_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,21 @@
(dispatch "mess" "kiwi"))))

(deftest help-for-messy-command-shows-command-help
(let [{:keys [status out]} (dispatch "mess" "-h")]
(is (= 0 status))
(is (string/includes? out "Usage:"))
(is (string/includes? out "NAME"))
(is (not (string/includes? out "nested")))))
(is (match? {:status 0
:out-lines ["bigmess messy - Messy command and group at same time"
""
"Commands:"
" nested: Command nested under messy group/command"
""
"Usage: bigmess messy [OPTIONS] NAME"
"Messy command."
""
"Options:"
" -h, --help This command summary"
""
"Arguments:"
" NAME: Name to print"]}
(dispatch "mess" "-h"))))

(comment

Expand Down