Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dispatch improvements #77

Merged
merged 51 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
ee6e6ba
wip: dispatch-tree
Sohalt Nov 14, 2023
731ea76
wip
Sohalt Nov 15, 2023
7cb4c47
2-arity versions of dispatch-tree(')
Sohalt Nov 15, 2023
07c87be
wip
borkdude Dec 18, 2023
9f4d0f8
assert
borkdude Dec 18, 2023
a13e714
test case
borkdude Dec 18, 2023
5d5f594
fix windows
borkdude Dec 18, 2023
1aa536f
Add test deps to cljs
borkdude Dec 18, 2023
c2e2e2b
wip
borkdude Dec 18, 2023
40989aa
wip
borkdude Dec 18, 2023
344aefc
Fix CLJS
borkdude Dec 19, 2023
c49bd07
wip
borkdude Dec 19, 2023
a7cc39b
wip
borkdude Dec 19, 2023
b61622a
Add todo
borkdude Dec 20, 2023
0393d56
wip
borkdude Dec 21, 2023
db4f846
yooo
borkdude Dec 21, 2023
c9a0e19
faaaaaail
borkdude Dec 21, 2023
b4cad6d
dude
borkdude Jan 2, 2024
9d6578f
dude
borkdude Jan 2, 2024
8d4a12c
rid prn
borkdude Jan 2, 2024
3f0bb0f
dude
borkdude Jan 2, 2024
a44ef3d
dude
borkdude Jan 2, 2024
a2efd94
yolo
borkdude Jan 2, 2024
6cf4bab
yo
borkdude Jan 2, 2024
4005e56
opts
borkdude Jan 2, 2024
054b72c
wip
borkdude Jan 2, 2024
6c1340c
wip
borkdude Jan 2, 2024
e672086
nodoc
borkdude Jan 2, 2024
d7cda2b
wip
borkdude Jan 2, 2024
dc89558
yeah
borkdude Jan 2, 2024
94cb514
yah
borkdude Jan 2, 2024
758eaa3
wip
borkdude Jan 2, 2024
282425b
wip
borkdude Jan 2, 2024
92c4263
dude
borkdude Jan 2, 2024
a9f8f81
verify option parsing
borkdude Jan 2, 2024
47198a4
dude
borkdude Jan 2, 2024
6815638
edge case, todo add test
borkdude Jan 2, 2024
a01d4b5
failing test
borkdude Jan 3, 2024
1d52de2
fixiefix
borkdude Jan 3, 2024
e856547
failing test
borkdude Jan 3, 2024
dd03f3a
wip
borkdude Jan 3, 2024
12f38e5
delay parsing opts in subcommand parsing
borkdude Jan 3, 2024
aff6474
yeah
borkdude Jan 3, 2024
915120e
dude
borkdude Jan 3, 2024
5d3f43e
dude
borkdude Jan 4, 2024
a672686
prioritize subcommands
borkdude Jan 4, 2024
988ae48
yeah
borkdude Jan 4, 2024
5c48333
dude
borkdude Jan 4, 2024
d6f1a6c
doc
borkdude Jan 4, 2024
3c772be
doc
borkdude Jan 4, 2024
20953bf
yeah
borkdude Jan 4, 2024
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
26 changes: 26 additions & 0 deletions .clj-kondo/borkdude/deflet/borkdude/deflet.clj_kondo
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
(ns borkdude.deflet
(:require [clj-kondo.hooks-api :as hooks-api]))

(defn deflet* [children]
(let [f (first children)
r (next children)]
(if (and (hooks-api/list-node? f)
(#{'def 'defp} (hooks-api/sexpr (first (:children f)))))
(let [def-children (:children f)]
(with-meta (hooks-api/list-node
[(hooks-api/coerce 'clojure.core/let)
(hooks-api/vector-node [(second def-children)
(nth def-children 2)])
(deflet* r)])
(meta f)))
(if-not r (or f (hooks-api/coerce nil))
(with-meta
(hooks-api/list-node (list (hooks-api/coerce 'do)
f
(deflet* r)))
(meta f))))))

(defn deflet [{:keys [node]}]
(let [children (:children node)
new-node (deflet* children)]
{:node new-node}))
3 changes: 3 additions & 0 deletions .clj-kondo/borkdude/deflet/config.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{:lint-as {borkdude.deflet/defp clojure.core/def}
:hooks {:analyze-call {borkdude.deflet/deflet borkdude.deflet/deflet
borkdude.deflet/defletp borkdude.deflet/deflet}}}
2 changes: 1 addition & 1 deletion .dir-locals.el
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
((clojure-mode
(cider-clojure-cli-aliases . ":repl")))
(cider-clojure-cli-aliases . ":repl:test")))
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ target
.clj-kondo/rewrite-clj
.clj-kondo/funcool
cljs-test-runner-out
src/scratch.clj
13 changes: 7 additions & 6 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,8 @@ Coerce string `s` using `f`. Does not coerce when `s` is not a string.

Subcommand dispatcher.

Dispatches on first matching command entry in `table`. A match is
determines by whether `:cmds`, a vector of strings, is a subsequence
(matching from the start) of the invoked commands.
Dispatches on longest matching command entry in `table` by matching
subcommands to the `:cmds` vector and invoking the correspondig `:fn`.

Table is in the form:

Expand All @@ -79,12 +78,14 @@ Subcommand dispatcher.
* `:args` - concatenation of unparsed commands and args
* `:rest-cmds`: DEPRECATED, this will be removed in a future version

This function does not throw. Use an empty `:cmds` vector to always match.
Use an empty `:cmds` vector to always match or to provide global options.

Provide an `:error-fn` to deal with non-matches.

Each entry in the table may have additional [`parse-args`](#parse-args) options.

Examples: see [README.md](README.md#subcommands).
<br><sub>[source](https://github.com/babashka/cli/blob/main/src/babashka/cli.cljc#L567-L611)</sub>
For more information and examples, see [README.md](README.md#subcommands).
<br><sub>[source](https://github.com/babashka/cli/blob/main/src/babashka/cli.cljc#L650-L682)</sub>
## `format-opts`
``` clojure

Expand Down
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,52 @@ Additional `parse-arg` options may be passed in each table entry:
{:cmds [] :fn help}])
```

Since cli 0.8.54 the order of `:cmds` in the table doesn't matter.

### Shared options

Since cli 0.8.54, babashka.cli supports parsing shared options in between and before the subcommands.

E.g.:

``` clojure
(def global-spec {:foo {:coerce :keyword}})
(def sub1-spec {:bar {:coerce :keyword}})
(def sub2-spec {:baz {:coerce :keyword}})

(def table
[{:cmds [] :spec global-spec}
{:cmds ["sub1"] :fn identity :spec sub1-spec}
{:cmds ["sub1" "sub2"] :fn identity :spec sub2-spec}])

(cli/dispatch table ["--foo" "a" "sub1" "--bar" "b" "sub2" "--baz" "c" "arg"])

;;=>

{:dispatch ["sub1" "sub2"],
:opts {:foo :a, :bar :b, :baz :c},
:args ["arg"]}
```

Note that specs are not merged, such that:

``` clojure
(cli/dispatch table ["sub1" "--foo" "bar"])
```

returns `{:dispatch ["sub1"], :opts {:foo "bar"}}` (`"bar"` is not coerced as a keyword).

Note that it is possible to use `:args->opts` but subcommands are always prioritized over arguments:

``` clojure
(def table
[{:cmds ["sub1"] :fn identity :spec sub1-spec :args->opts [:some-opt]}
{:cmds ["sub1" "sub2"] :fn identity :spec sub2-spec}])

(cli/dispatch table ["sub1" "dude"]) ;;=> {:dispatch ["sub1"], :opts {:some-opt "dude"}}
(cli/dispatch table ["sub1" "sub2"]) ;;=> {:dispatch ["sub1" "sub2"], :opts {}}
```

## Babashka tasks

For documentation on babashka tasks, go
Expand Down
2 changes: 1 addition & 1 deletion bb.edn
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
:task (apply clojure "-M:test" *command-line-args*)}

cljs-test {:doc "Run CLJS tests"
:task (apply clojure "-M:cljs-test" *command-line-args*)}
:task (apply clojure "-M:test:cljs-test" *command-line-args*)}

quickdoc {:doc "Invoke quickdoc"
:requires ([quickdoc.api :as api])
Expand Down
3 changes: 2 additions & 1 deletion deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
{:extra-paths ["test"]
:extra-deps {io.github.cognitect-labs/test-runner
{:git/tag "v0.5.1" :git/sha "dfb30dd"}
babashka/fs {:mvn/version "0.1.11"}}
babashka/fs {:mvn/version "0.1.11"}
io.github.borkdude/deflet {:mvn/version "0.1.0"}}
:exec-args {:cmd "bb test"}
:main-opts ["-m" "babashka.cli.exec"]
:exec-fn babashka.test-runner/test #_cognitect.test-runner.api/test}
Expand Down
Loading
Loading