Skip to content

Commit cb678de

Browse files
authored
Merge pull request #59 from hlship/hls/20251121-tool-options
Ensure that extra tool options are written to completions
2 parents 4fa4482 + e3c8f82 commit cb678de

14 files changed

Lines changed: 96 additions & 44 deletions

File tree

.github/workflows/clojure.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515

1616
steps:
1717
- name: Checkout
18-
uses: actions/checkout@v5
18+
uses: actions/checkout@v6
1919

2020
- name: Setup Java
2121
uses: actions/setup-java@v5

CHANGES.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
* You may now enter `-h` or `--help` after a group to get help for just that group
2626
* Tool help output has been reordered, with top-level tool commands first (previously, those were in a "Builtin" group and listed last)
2727
* Tool help now displays just root-level commands by default (add `--commands all` to list nested commands)
28-
* When extracting the first sentence as the single-line index, embedded periods are no longer considered the end of the sentence
28+
* When extracting the first sentence as the single-line title, embedded periods are no longer considered the end of the sentence
2929
* `net.lewisship.cli-tools`:
3030
* Added function `tool-name`
3131
* Added function `command-root`
@@ -38,7 +38,7 @@
3838
* :source-dirs specifies extra directories to consider when caching
3939
* :pre-dispatch - callback function invoked before dispatch
4040
* :pre-invoke - callback function invoked before the dispatched command function is invoked
41-
* Can now handle "messy" case where a command has the same name as a group
41+
* Can now handle the case where a command has the same name as a group
4242
* Cache files are now stored in `~/.cache/net.lewisship.cli-tools` by default
4343
* Added initial support for commands defined as Babashka CLI functions
4444
* Added `net.lewiship.cli-tools.test` namespace

deps.edn

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
{:extra-deps {org.clojure/tools.cli {:mvn/version "1.2.245" :optional true}
2828
babashka/fs {:mvn/version "0.5.27" :optional true}
2929
babashka/process {:mvn/version "0.6.23" :optional true}
30-
selmer/selmer {:mvn/version "1.12.65" :optional true}
31-
org.babashka/cli {:mvn/version "0.8.66" :optional true}}}
30+
selmer/selmer {:mvn/version "1.12.69" :optional true}
31+
org.babashka/cli {:mvn/version "0.8.67" :optional true}}}
3232

3333
:1.11
3434
{:override-deps {org.clojure/clojure ^:antq/exclude {:mvn/version "1.11.4"}}}

resources/net/lewisship/cli_tools/group.tpl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
case "$state" in
99
cmds)
1010
_values "{{tool}} {{group.name}} subcommands" {% for sub in group.subs %} \
11-
"{{sub.name}}[{{sub.summary}}]" {% endfor %}
11+
"{{sub.name}}[{{sub.title}}]" {% endfor %}
1212
;;
1313
args)
1414
case $line[1] in {% for sub in group.subs %}
@@ -17,5 +17,3 @@
1717
;;
1818
esac
1919
}
20-
21-

resources/net/lewisship/cli_tools/top-level.tpl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
_{{tool}}() {
44
local line state
55

6-
_arguments -C \
6+
_arguments -C {% for opt in options %} \
7+
{{ opt }} {% endfor %} \
78
"1: :->cmds" \
8-
"*::arg:->args"
9+
"*::args:->args"
910

1011
case "$state" in
1112
cmds)
1213
_values "{{tool}} command" {% for cmd in commands %} \
13-
"{{cmd.name}}[{{cmd.summary}}]" {% endfor %}
14+
"{{cmd.name}}[{{cmd.title}}]" {% endfor %}
1415
;;
1516
args)
1617
case $line[1] in {% for cmd in commands %}
@@ -20,4 +21,3 @@ _{{tool}}() {
2021
;;
2122
esac
2223
}
23-

src/net/lewisship/cli_tools.clj

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -190,14 +190,6 @@
190190
~validations)
191191
~@body))))))
192192

193-
194-
(def ^:private
195-
default-tool-options
196-
"Default tool command line options."
197-
[["-C" "--color" "Enable ANSI color output"]
198-
["-N" "--no-color" "Disable ANSI color output"]
199-
["-h" "--help" "This command summary"]])
200-
201193
(defn- expand-tool-options
202194
"Expand dispatch options into tool options, leveraging a cache."
203195
[options]
@@ -221,7 +213,7 @@
221213
(merge {:tool-name tool-name'
222214
:cache-digest digest
223215
:command-root command-root}
224-
(select-keys options [:doc :arguments :tool-summary :pre-dispatch :pre-invoke]))))
216+
(select-keys options [:doc :arguments :tool-summary :pre-dispatch :pre-invoke :extra-tool-options]))))
225217

226218
(defn- dispatch*
227219
"Called (indirectly/anonymously) from a tool handler to process remaining command line arguments."
@@ -308,7 +300,7 @@
308300
default-dispatch-options
309301
dispatch-options)
310302
{:keys [extra-tool-options tool-options-handler]} merged-options
311-
full-options (concat extra-tool-options default-tool-options)
303+
full-options (concat extra-tool-options impl/default-tool-options)
312304
{:keys [options arguments summary errors]}
313305
(cli/parse-opts (:arguments merged-options)
314306
full-options
@@ -334,7 +326,7 @@
334326

335327
(if tool-options-handler
336328
(tool-options-handler options dispatch-options' callback)
337-
(dispatch* dispatch-options'))))))
329+
(callback))))))
338330

339331
(defn select-option
340332
"Builds a standard option spec for selecting from a list of possible values.

src/net/lewisship/cli_tools/completions.clj

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@
6565
(if fn
6666
{:name command-name
6767
:fn-name fn-name
68-
:summary title
68+
:title title
6969
:options (options command-map)}
7070
{:name (->> command-map
7171
:command-path
7272
(string/join " "))
73-
:summary title
73+
:title title
7474
:fn-name fn-name
7575
:subs (map #(extract-command fn-name %) (:subs command-map))})))
7676

@@ -86,12 +86,14 @@
8686
:command command}))))
8787

8888
(defn- print-tool
89-
[tool-name command-root _groups]
89+
[tool-name command-root extra-options]
9090
(let [prefix (str "_" tool-name)
91+
options (map #(apply to-opt %) (concat extra-options impl/default-tool-options))
9192
commands (->> command-root
9293
(keep #(extract-command prefix %)))]
9394
(selmer.util/without-escaping
9495
(render "top-level" {:tool tool-name
96+
:options options
9597
:commands commands})
9698
(render-commands tool-name commands))))
9799

@@ -102,23 +104,23 @@
102104
output-path ["PATH" "File to write completions to."
103105
:optional true]]
104106
(binding [impl/*introspection-mode* true]
105-
(let [{:keys [command-root tool-name groups]} impl/*tool-options*]
107+
(let [{:keys [command-root tool-name extra-tool-options]} impl/*tool-options*
108+
generator #(binding [ansi/*color-enabled* false]
109+
(print-tool tool-name command-root extra-tool-options))]
106110
(if output-path
107111
(do
108112
(with-open [w (-> output-path
109113
fs/file
110114
io/output-stream
111115
io/writer)]
112116
(try
113-
(binding [*out* w
114-
ansi/*color-enabled* false]
115-
(print-tool tool-name command-root groups))
117+
(binding [*out* w]
118+
(generator))
116119
(catch Throwable t
117120
(abort 1 [:red
118121
(command-path) ": "
119122
(or (ex-message t)
120123
(class t))]))))
121124
(perr [:cyan "Wrote " output-path]))
122125
;; Just write to standard output
123-
(binding [ansi/*color-enabled* false]
124-
(print-tool tool-name command-root groups))))))
126+
(generator)))))

src/net/lewisship/cli_tools/impl.clj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,3 +1061,9 @@
10611061
:subs)]
10621062
(cond->> root
10631063
transformer (transformer dispatch-options))))
1064+
1065+
(def default-tool-options
1066+
"Default tool command line options."
1067+
[["-C" "--color" "Enable ANSI color output"]
1068+
["-N" "--no-color" "Disable ANSI color output"]
1069+
["-h" "--help" "This command summary"]])

test-resources/expected/messy-completions.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
_messy() {
44
local line state
55

6-
_arguments -C \
6+
_arguments -C \
7+
'(-C --color)'{-C,--color}$'[Enable ANSI color output]' \
8+
'(-N --no-color)'{-N,--no-color}$'[Disable ANSI color output]' \
9+
'(-h --help)'{-h,--help}$'[This command summary]' \
710
"1: :->cmds" \
8-
"*::arg:->args"
11+
"*::args:->args"
912

1013
case "$state" in
1114
cmds)
@@ -29,7 +32,6 @@ _messy() {
2932
;;
3033
esac
3134
}
32-
3335
_messy_completions() {
3436
_arguments -s \
3537
'(-h --help)'{-h,--help}$'[This command summary]'

test-resources/expected/simple-completions.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
_simple() {
44
local line state
55

6-
_arguments -C \
6+
_arguments -C \
7+
'(-C --color)'{-C,--color}$'[Enable ANSI color output]' \
8+
'(-N --no-color)'{-N,--no-color}$'[Disable ANSI color output]' \
9+
'(-h --help)'{-h,--help}$'[This command summary]' \
710
"1: :->cmds" \
8-
"*::arg:->args"
11+
"*::args:->args"
912

1013
case "$state" in
1114
cmds)
@@ -26,7 +29,6 @@ _simple() {
2629
;;
2730
esac
2831
}
29-
3032
_simple_colors() {
3133
_arguments -s \
3234
'(-h --help)'{-h,--help}$'[This command summary]'

0 commit comments

Comments
 (0)