Add --verbose to global opts

This commit is contained in:
Michiel Borkent 2021-05-02 11:00:44 +02:00
parent cde72ebc45
commit d1bbf844de
2 changed files with 29 additions and 19 deletions

View file

@ -427,12 +427,6 @@ When no eval opts or subcommand is provided, the implicit subcommand is repl.")
(assoc opts-map :run fst :command-line-args (next args)))) (assoc opts-map :run fst :command-line-args (next args))))
opts-map))) opts-map)))
(defn parse-global-opts [options]
(when-let [f (first options)]
(case f
("--classpath" "-cp") [(nnext options) (second options)]
[options nil])))
(defn parse-args [args opts-map] (defn parse-args [args opts-map]
(loop [options args (loop [options args
opts-map opts-map] opts-map opts-map]
@ -567,12 +561,22 @@ When no eval opts or subcommand is provided, the implicit subcommand is repl.")
:command-line-args (next options))))))) :command-line-args (next options)))))))
opts-map))) opts-map)))
(defn parse-global-opts [options]
(loop [options (seq options)
opts-map {}]
(if options
(case (first options)
("--classpath" "-cp") (recur (nnext options) (assoc opts-map :classpath (second options)))
("--verbose") (recur (next options) (assoc opts-map :verbose? true))
[options opts-map])
[options opts-map])))
(defn parse-opts (defn parse-opts
([options] (parse-opts options nil)) ([options] (parse-opts options nil))
([options opts-map] ([options opts-map]
(let [[options classpath] (parse-global-opts options) (let [[options opts-map] (if opts-map
opts-map (if classpath (assoc opts-map :classpath classpath) [options opts-map]
opts-map) (parse-global-opts options))
opt (first options) opt (first options)
tasks (into #{} (map str) (keys (:tasks @common/bb-edn)))] tasks (into #{} (map str) (keys (:tasks @common/bb-edn)))]
(if-not opt opts-map (if-not opt opts-map
@ -580,16 +584,16 @@ When no eval opts or subcommand is provided, the implicit subcommand is repl.")
(cond (cond
(fs/regular-file? opt) (fs/regular-file? opt)
(if (str/ends-with? opt ".jar") (if (str/ends-with? opt ".jar")
{:classpath classpath (assoc opts-map
:jar opt :jar opt
:command-line-args (next options)} :command-line-args (next options))
{:classpath classpath (assoc opts-map
:file opt :file opt
:command-line-args (next options)}) :command-line-args (next options)))
(contains? tasks opt) (contains? tasks opt)
{:run opt (assoc opts-map
:classpath classpath :run opt
:command-line-args (rest options)} :command-line-args (next options))
(command? opt) (command? opt)
(recur (cons (str "--" opt) (next options)) opts-map) (recur (cons (str "--" opt) (next options)) opts-map)
:else :else

View file

@ -35,6 +35,12 @@
(is (= {:classpath "src" (is (= {:classpath "src"
:uberjar "foo.jar"} :uberjar "foo.jar"}
(main/parse-opts ["--classpath" "src" "uberjar" "foo.jar"]))) (main/parse-opts ["--classpath" "src" "uberjar" "foo.jar"])))
(is (= {:classpath "src"
:uberjar "foo.jar"
:verbose? true}
(main/parse-opts ["--verbose" "--classpath" "src" "uberjar" "foo.jar"])))
(is (= "src" (:classpath (main/parse-opts ["--classpath" "src"]))))
(is (:verbose? (main/parse-opts ["--verbose"])))
(is (= 123 (bb nil "(println 123)"))) (is (= 123 (bb nil "(println 123)")))
(is (= 123 (bb nil "-e" "(println 123)"))) (is (= 123 (bb nil "-e" "(println 123)")))
(is (= 123 (bb nil "--eval" "(println 123)"))) (is (= 123 (bb nil "--eval" "(println 123)")))
@ -47,7 +53,7 @@
(let [v (bb nil "--describe")] (let [v (bb nil "--describe")]
(is (:babashka/version v)) (is (:babashka/version v))
(is (:feature/xml v))) (is (:feature/xml v)))
(is (= "src" (:classpath (main/parse-opts ["--classpath" "src"]))))) )
(deftest version-test (deftest version-test
(is (= [1 0 0] (main/parse-version "1.0.0-SNAPSHOT"))) (is (= [1 0 0] (main/parse-version "1.0.0-SNAPSHOT")))