diff --git a/CHANGELOG.md b/CHANGELOG.md index f27d982..f0c1ff0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,10 +3,7 @@ ## Added - Added `:required` for `:flags` - -## Fixed - -## Changed +- Support commands with arguments and subcommands at the same time # 0.7.33 (2024-02-27 / cb19704) diff --git a/repl_sessions/test_scratch.clj b/repl_sessions/test_scratch.clj index 513066d..52357d4 100644 --- a/repl_sessions/test_scratch.clj +++ b/repl_sessions/test_scratch.clj @@ -1,5 +1,6 @@ -(ns test-scratch - (:require [clojure.test :as t])) +(ns lambdaisland.cli.test-scratch + (:require + [lambdaisland.cli :as cli])) @@ -226,3 +227,8 @@ ;; :flags ;; ["-v,--verbose" {:description "Increase verbosity"}]}) + +(def s + {:commands ["foo ARG" {:commands ["baz" prn]}]}) + +(cli/dispatch* s ["foo" "XXX" "baz"]) diff --git a/src/lambdaisland/cli.clj b/src/lambdaisland/cli.clj index f5796c2..e29beee 100644 --- a/src/lambdaisland/cli.clj +++ b/src/lambdaisland/cli.clj @@ -338,7 +338,7 @@ (print-help program-name doc [] flagpairs) (binding [*opts* (-> opts (dissoc ::middleware) - (assoc ::argv pos-args) + (update ::argv (fnil into []) pos-args) (merge (zipmap argnames pos-args)))] (if-let [missing (missing-flags flagmap opts)] (parse-error! "Missing required flags:" (->> missing (map #(str/join " " %)) (str/join ", "))) @@ -350,13 +350,22 @@ cmd (when cmd (first (str/split cmd #"[ =]"))) opts (if cmd (update opts ::command (fnil conj []) cmd) opts) command-pairs (prepare-cmdpairs commands) - command-map (into {} command-pairs) - command-match (get command-map cmd)] - + command-map (update-keys (into {} command-pairs) + #(first (str/split % #"[ =]"))) + command-match (get command-map cmd) + argnames (:argnames command-match) + arg-count (count argnames)] (cond command-match - (dispatch* (assoc (merge (dissoc cmdspec :command :commands) command-match) - :name (str program-name " " cmd)) pos-args opts) + (dispatch* + (-> cmdspec + (dissoc :command :commands) + (merge command-match) + (assoc :name (str program-name " " cmd))) + (drop arg-count pos-args) + (-> opts + (update ::argv (fnil into []) (take arg-count pos-args)) + (merge (zipmap argnames pos-args)))) (or (nil? command-match) (nil? commands)