Skip to content

Commit

Permalink
Fix #104: allow extra arguments to be passed in exec fn (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude authored Nov 15, 2024
1 parent 6ee198e commit 19c0353
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ For breaking changes, check [here](#breaking-changes).

- Fix [#102](https://github.com/babashka/cli/issues/102): `format-table` correctly pads cells containing ANSI escape codes
- Fix [#106](https://github.com/babashka/cli/issues/106): Multiple options before subcommand conflict with subcommand
- Fix [#104](https://github.com/babashka/cli/issues/104): Allow extra arguments to be passed before options in exec function

## v0.8.60 (2024-07-23)

Expand Down
23 changes: 12 additions & 11 deletions src/babashka/cli/exec.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

(set! *warn-on-reflection* true)

#_:clj-kondo/ignore
(def ^:private ^:dynamic *basis* "For testing" nil)

(defmacro ^:private req-resolve [f]
Expand Down Expand Up @@ -54,16 +53,18 @@
(str/starts-with? f "{")
[(edn/read-string f) cmds]
:else [nil (cons f cmds)])
f (case (count cmds)
0 (resolve-exec-fn ns-default exec-fn)
1 (let [f (first cmds)]
(if (str/includes? f "/")
(symbol f)
(resolve-exec-fn ns-default (symbol f))))
2 (let [[ns-default f] cmds]
(if (str/includes? f "/")
(symbol f)
(resolve-exec-fn (symbol ns-default) (symbol f)))))
[f unconsumed-args] (case (count cmds)
0 [(resolve-exec-fn ns-default exec-fn)]
1 [(let [f (first cmds)]
(if (str/includes? f "/")
(symbol f)
(resolve-exec-fn ns-default (symbol f))))]
(let [[ns-default f & unconsumed-args] cmds]
[(if (str/includes? f "/")
(symbol f)
(resolve-exec-fn (symbol ns-default) (symbol f)))
unconsumed-args]))
args (concat unconsumed-args args)
f* f
f (req-resolve f)
_ (assert (ifn? f) (str "Could not resolve function: " f*))
Expand Down
27 changes: 17 additions & 10 deletions test/babashka/cli/exec_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
(is (submap? {:a 1 :b 2} (main "babashka.cli.exec-test/foo" ":a" "1" ":b" "2")))
(is (submap? {:b 1} (main "babashka.cli.exec-test" "foo" ":b" "1")))
(is (submap? {:a 1 :b 2} (main "babashka.cli.exec-test" "foo" ":a" "1" ":b" "2")))
(is (submap? {:args ["arg"]
:exec true}
(-> (main "babashka.cli.exec-test" "foo" "arg" ":a" "1" ":b" "2")
meta
:org.babashka/cli)))
(is (submap? {:a 1 :b 2} (main
"{:coerce {:a :long}}"
"babashka.cli.exec-test" "foo" ":a" "1" ":b" "2")))
Expand All @@ -35,20 +40,20 @@
":a" "1" ":b" "2"))))
(is (submap? {:a 1 :b 2}
(binding [babashka.cli.exec/*basis* '{:argmap {:org.babashka/cli {:coerce {:a :long}}
:exec-fn babashka.cli.exec-test/foo}}]
:exec-fn babashka.cli.exec-test/foo}}]
(main
":a" "1" ":b" "2"))))
(is (submap? {:a 1 :b 2}
(binding [babashka.cli.exec/*basis*
'{:argmap {:org.babashka/cli {:coerce {:a :long}}
:ns-default babashka.cli.exec-test}}]
:ns-default babashka.cli.exec-test}}]
(main
"foo" ":a" "1" ":b" "2"))))
(is (submap? {:a 1 :b 2}
(binding [babashka.cli.exec/*basis*
'{:argmap {:org.babashka/cli {:coerce {:a :long}}
:ns-default babashka.cli.exec-test
:exec-fn foo}}]
:ns-default babashka.cli.exec-test
:exec-fn foo}}]
(main ":a" "1" ":b" "2"))))
(let [basis "{:argmap {:org.babashka/cli {:coerce {:a :long}}
:ns-default babashka.cli.exec-test
Expand All @@ -63,18 +68,20 @@
(edn/read-string
(with-out-str (binding [babashka.cli.exec/*basis*
'{:argmap {:org.babashka/cli {:coerce {:a :long}}
:ns-default babashka.cli.exec-test
:exec-fn foo}}]
:ns-default babashka.cli.exec-test
:exec-fn foo}}]
(main "clojure.core/prn" ":a" "1" ":b" "2"))))))
(is (submap? {:a 1 :b 2}
(edn/read-string
(with-out-str (binding [babashka.cli.exec/*basis*
'{:argmap {:ns-default clojure.pprint
:exec-fn foo}}]
:exec-fn foo}}]
(main "pprint" ":a" "1" ":b" "2"))))))
(is (:exec (:org.babashka/cli
(meta (binding [babashka.cli.exec/*basis*
'{:argmap {:org.babashka/cli {:coerce {:a :long}}
:ns-default babashka.cli.exec-test
:exec-fn foo}}]
(main ":a" "1" ":b" "2")))))))
:ns-default babashka.cli.exec-test
:exec-fn foo}}]
(main ":a" "1" ":b" "2"))))))

)

0 comments on commit 19c0353

Please sign in to comment.