|
| 1 | +(ns test-scratch |
| 2 | + (:require [clojure.test :as t])) |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | +(defmacro def-are [name & pairs] |
| 7 | + `(deftest ~name |
| 8 | + ~@(for [[a b] (partition 2 pairs)] |
| 9 | + (is (= ~b ~a))))) |
| 10 | + |
| 11 | +(set! *print-namespace-maps* false) |
| 12 | + |
| 13 | +(def last-flags (atom nil)) |
| 14 | + |
| 15 | +(defn capture-cmd [flags] |
| 16 | + (reset! last-flags flags)) |
| 17 | + |
| 18 | +(defn cli [cli-args cmdspec] |
| 19 | + (reset! last-flags :not-called) |
| 20 | + (let [out (with-out-str |
| 21 | + (dispatch cmdspec cli-args))] |
| 22 | + (if (seq out) |
| 23 | + [@last-flags out] |
| 24 | + @last-flags))) |
| 25 | + |
| 26 | +(defn lines [& args] |
| 27 | + (str/join "\n" args)) |
| 28 | + |
| 29 | +(def cmds-run ["run" {:description "Do the thing" |
| 30 | + :command capture-cmd}]) |
| 31 | + |
| 32 | +(def flags-input ["-i,--input FILE" "Specify input file"]) |
| 33 | + |
| 34 | +(def-are base-case |
| 35 | + (cli ["run"] {:commands cmds-run}) |
| 36 | + {:lambdaisland.cli/command ["run"], :lambdaisland.cli/argv []}) |
| 37 | + |
| 38 | +(def-are help-rendering |
| 39 | + (cli [] {:commands cmds-run}) |
| 40 | + [:not-called |
| 41 | + (lines "Usage: cli [command...] [flags-or-args...]" |
| 42 | + "" |
| 43 | + " run Do the thing" |
| 44 | + "")] |
| 45 | + |
| 46 | + (cli ["help"] {:commands cmds-run}) |
| 47 | + [:not-called |
| 48 | + (lines "Usage: cli [command...] [flags-or-args...]" |
| 49 | + "" |
| 50 | + " run Do the thing" |
| 51 | + "")] |
| 52 | + |
| 53 | + (cli ["help"] {:commands cmds-run}) |
| 54 | + [:not-called |
| 55 | + (lines "Usage: cli [command...] [flags-or-args...]" |
| 56 | + "" |
| 57 | + " run Do the thing" |
| 58 | + "")] |
| 59 | + (cli ["--help"] {:commands cmds-run |
| 60 | + :flags flags-input}) |
| 61 | + [:not-called |
| 62 | + (lines "Usage: cli [command...] [flags-or-args...]" |
| 63 | + "" |
| 64 | + " -i,--input FILE Specify input file" |
| 65 | + "" |
| 66 | + " run Do the thing" |
| 67 | + "")]) |
| 68 | + |
| 69 | +(def-are arguments |
| 70 | + (cli ["run" "hello"] {:commands cmds-run}) |
| 71 | + {:lambdaisland.cli/command ["run"] |
| 72 | + :lambdaisland.cli/argv ["hello"]}) |
| 73 | + |
| 74 | +(deftest subcommands |
| 75 | + (let [cmdspec {:commands {"run" {:command capture-cmd |
| 76 | + :description "Do something"} |
| 77 | + "widget" {:description "Work with widgets" |
| 78 | + :commands |
| 79 | + ["ls" {:description "List widgets" |
| 80 | + :command capture-cmd} |
| 81 | + "add" {:description "Add widget" |
| 82 | + :command capture-cmd}]}}}] |
| 83 | + (is (= [["x"] {::cli/command ["widget" "ls"]}] |
| 84 | + (cli ["widget" "ls" "x"] cmdspec))))) |
| 85 | + |
| 86 | +(deftest flags |
| 87 | + (is (= [nil {:input "INPUT" |
| 88 | + :output "OUTPUT" |
| 89 | + ::cli/command ["run"]}] |
| 90 | + (cli |
| 91 | + ["run" "--input" "INPUT" "--output" "OUTPUT"] |
| 92 | + {:commands {"run" {:command capture-cmd |
| 93 | + :description "Do something"}} |
| 94 | + :flags ["-i,--input FILE" {:desc "Specify input file"} |
| 95 | + "--output FILE" "Specify output file"]})))) |
| 96 | + |
| 97 | +(deftest parse-flagstr-test |
| 98 | + (testing "single short flag" |
| 99 | + (is (= {:flagstr "-i" |
| 100 | + :flags ["-i"] |
| 101 | + :description "Specify input file" |
| 102 | + :argdoc "" |
| 103 | + :key :i |
| 104 | + :argcnt 0 |
| 105 | + :args []} |
| 106 | + (parse-flagstr "-i" {:description "Specify input file"})))) |
| 107 | + (testing "single long flag" |
| 108 | + (is (= {:flagstr "--input" |
| 109 | + :flags ["--input"] |
| 110 | + :description "Specify input file" |
| 111 | + :argdoc "" |
| 112 | + :key :input |
| 113 | + :argcnt 0 |
| 114 | + :args []} |
| 115 | + (parse-flagstr "--input" {:description "Specify input file"})))) |
| 116 | + |
| 117 | + (testing "short and long - with argument" |
| 118 | + (is (= [ |
| 119 | + {:flag "-i" |
| 120 | + :short? true |
| 121 | + :args '(:file) |
| 122 | + :key :input |
| 123 | + :argcnt 1 |
| 124 | + :description "Specify input file"} |
| 125 | + {:flag "--input" |
| 126 | + :short? false |
| 127 | + :args '(:file) |
| 128 | + :key :input |
| 129 | + :argcnt 1 |
| 130 | + :description "Specify input file"}] |
| 131 | + (build-flagmap-entries ["-i,--input FILE" {:description "Specify input file"}])))) |
| 132 | + |
| 133 | + (testing "override key, value" |
| 134 | + (is (= [{:flag "--foo" |
| 135 | + :short? false |
| 136 | + :args () |
| 137 | + :key :bar |
| 138 | + :argcnt 0 |
| 139 | + :description "Do a foo" |
| 140 | + :value 123}] |
| 141 | + (build-flagmap-entries |
| 142 | + ["--foo" {:description "Do a foo" |
| 143 | + :key :bar |
| 144 | + :value 123}])))) |
| 145 | + |
| 146 | + (testing "--[no-]" |
| 147 | + (is (= [{:flag "--no-foo" |
| 148 | + :short? false |
| 149 | + :args () |
| 150 | + :key :foo |
| 151 | + :argcnt 0 |
| 152 | + :value false |
| 153 | + :description "Enable/disable foo"} |
| 154 | + {:flag "--foo" |
| 155 | + :short? false |
| 156 | + :args () |
| 157 | + :key :foo |
| 158 | + :argcnt 0 |
| 159 | + :value true |
| 160 | + :description "Enable/disable foo"}] |
| 161 | + (build-flagmap-entries ["--[no-]foo" {:description "Enable/disable foo"}]))))) |
| 162 | + |
| 163 | +(deftest parse-arg-names-test |
| 164 | + (is (= '[["-i"] "" ()] (parse-arg-names "-i"))) |
| 165 | + (is (= '[["-i"] " FOO BAR" (:foo :bar)] (parse-arg-names "-i FOO BAR"))) |
| 166 | + (is (= '[["-i" "--input"] "=<foo>" (:foo)] (parse-arg-names "-i, --input=<foo>"))) |
| 167 | + (is (= '[["cmd"] " FILE" (:file)] (parse-arg-names "cmd FILE"))) |
| 168 | + (is (= '[["cmd"] " <file>" (:file)] (parse-arg-names "cmd <file>")))) |
| 169 | + |
| 170 | +(mapcat build-flagmap-entries |
| 171 | + (coerce-to-pairs ["-i, --input ARG" {:description "foo"}])) |
| 172 | +(parse-flagstrs ["-i, --input, --[no-]foo ARG" {:description "foo"}]) |
| 173 | +(parse-flagstrs ["-i, --input, --[no-]foo=<arg>" {:description "foo"}]) |
| 174 | +(build-flagmap-entries ["-i, --input ARG" {:description "foo"}]) |
| 175 | + |
| 176 | + |
| 177 | +;; (let [cmdspec |
| 178 | +;; {:commands {"run" {:command (show-args "run") |
| 179 | +;; :description "Do something"} |
| 180 | +;; "widget" {:description "Work with widgets" |
| 181 | +;; :commands |
| 182 | +;; ["ls" {:description "List widgets" |
| 183 | +;; :command (show-args "widget ls")} |
| 184 | +;; "add" {:description "Add widget" |
| 185 | +;; :command (show-args "widget add")}]}} |
| 186 | +;; :flags ["-i,--input FILE" {:desc "Specify input file" |
| 187 | +;; :key "XXX"} |
| 188 | +;; "--output FILE" "Specify output file"]}] |
| 189 | +;; (split-flags |
| 190 | +;; (assoc cmdspec :flagspecs (parse-flagstrs (:flags cmdspec))) |
| 191 | +;; ["widget" "ls" "--help" "--input" "INPUT" "--output" "OUTPUT"])) |
| 192 | + |
| 193 | +;; (dispatch |
| 194 | +;; {:commands {"run" {:command (show-args "run") |
| 195 | +;; :description "Do something"} |
| 196 | +;; "widget" {:description "Work with widgets" |
| 197 | +;; :commands |
| 198 | +;; ["ls" {:description "List widgets" |
| 199 | +;; :command (show-args "widget ls") |
| 200 | +;; :help "List widgets in the order they exist."} |
| 201 | +;; "add" {:description "Add widget" |
| 202 | +;; :command (show-args "widget add")}]}} |
| 203 | +;; :flags ["-i,--input FILE" {:desc "Specify input file"} |
| 204 | +;; "--output FILE" "Specify output file"]} |
| 205 | +;; ["widget" "ls" "--help"]) |
| 206 | + |
| 207 | +;; (cli/dispatch |
| 208 | +;; {:commands |
| 209 | +;; ["run" |
| 210 | +;; {:description "Run the thing" |
| 211 | +;; :command (fn [args flags] |
| 212 | +;; ,,,)} |
| 213 | + |
| 214 | +;; "widgets" |
| 215 | +;; {:description "Work with widgets" |
| 216 | +;; :subcommands |
| 217 | +;; ["ls" |
| 218 | +;; {:description "List widgets" |
| 219 | +;; :command (fn [args flags] ,,,)} |
| 220 | +;; "create NAME" |
| 221 | +;; {:description "Create a new widget" |
| 222 | +;; :command (fn [args flags] ,,,)} |
| 223 | +;; "delete ID" |
| 224 | +;; {:description "Delete the widget with the given ID" |
| 225 | +;; :command (fn [args flags] ,,,)}]}] |
| 226 | + |
| 227 | +;; :flags |
| 228 | +;; ["-v,--verbose" {:description "Increase verbosity"}]}) |
0 commit comments