Skip to content

Commit a206f00

Browse files
committed
add cljr support per @dmiller
1 parent fca05e3 commit a206f00

4 files changed

Lines changed: 57 additions & 16 deletions

File tree

.gitignore

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,18 @@
33
/.cpcache
44
/.lein-failures
55
/.lein-repl-history
6-
/.lsp
6+
/.lsp/.cache
77
/.nrepl-port
8-
/.portal
8+
/.portal/vs-code.edn
99
/.socket-repl-port
1010
/cljs-test-runner-out
1111
target
12+
13+
14+
#Visual Studio artifacts
15+
bin/
16+
obj/
17+
.vs/
18+
*.user
19+
*.suo
20+
*.nupkg

src/main/clojure/clojure/tools/cli.cljc

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@
143143
(recur options (into extra-args (vec (rest args))) nil)
144144

145145
(and (opt? opt) (nil? spec))
146-
(throw #?(:clj (Exception. (str "'" opt "' is not a valid argument"))
146+
(throw #?(:clj (Exception. (str "'" opt "' is not a valid argument"))
147+
:cljr (Exception. (str "'" opt "' is not a valid argument"))
147148
:cljs (js/Error. (str "'" opt "' is not a valid argument"))))
148149

149150
(and (opt? opt) (spec :flag))
@@ -251,6 +252,7 @@
251252
(let [msg (str "Warning: The following options to parse-opts are unrecognized: "
252253
(s/join ", " unknown-keys))]
253254
#?(:clj (binding [*out* *err*] (println msg))
255+
:cljr (binding [*out* *err*] (println msg))
254256
:cljs (binding [*print-fn* *print-err-fn*] (println msg)))))))
255257

256258
(select-keys map spec-keys))
@@ -262,6 +264,7 @@
262264
long-opt (or long-opt (:long-opt spec-map))
263265
[long-opt req] (when long-opt
264266
(rest (re-find #"^(--[^ =]+)(?:[ =](.*))?" long-opt)))
267+
#?@(:cljr (req (if (= req "") nil req))) ;;; Regular expression variation
265268
id (when long-opt
266269
(keyword (nth (re-find #"^--(\[no-\])?(.*)" long-opt) 2)))
267270
validate (:validate spec-map)
@@ -393,7 +396,7 @@
393396
(let [{:keys [validate-fn validate-msg]} spec]
394397
(or (loop [[vfn & vfns] validate-fn [msg & msgs] validate-msg]
395398
(when vfn
396-
(if (try (vfn value) (catch #?(:clj Throwable :cljs :default) _))
399+
(if (try (vfn value) (catch #?(:clj Throwable :cljr Exception :cljs :default) _))
397400
(recur vfns msgs)
398401
[::error (validation-error value opt optarg msg)])))
399402
[value nil])))
@@ -403,7 +406,7 @@
403406
[value error] (if parse-fn
404407
(try
405408
[(parse-fn value) nil]
406-
(catch #?(:clj Throwable :cljs :default) e
409+
(catch #?(:clj Throwable :cljr Exception :cljs :default) e
407410
[nil (parse-error opt optarg (str e))]))
408411
[value nil])]
409412
(cond error
@@ -507,7 +510,7 @@
507510
(or default-desc
508511
(when (contains? spec :default)
509512
(if (some? default)
510-
(str default)
513+
#?(:cljr (pr-str default) :default (str default)) ;; in order to get the proper case for booleans
511514
"nil"))
512515
(when default-fn
513516
"<computed>")
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
5+
</PropertyGroup>
6+
7+
8+
<PropertyGroup>
9+
<PackageId>clojure.tools.cli</PackageId>
10+
<RootNamespace>clojure.tools</RootNamespace>
11+
<Title>clojure.tools.cli</Title>
12+
<Product>clojure.tools.cli</Product>
13+
<AssemblyTitle>clojure.tools.cli</AssemblyTitle>
14+
<Authors>Gareth Jones, Sung Pae, Sean Corfield</Authors>
15+
<Description>Something appropriate.</Description>
16+
<Copyright>Copyright © Rich Hickey and contributors 2023</Copyright>
17+
<PackageLicenseExpression>EPL-1.0</PackageLicenseExpression>
18+
<RepositoryUrl>https://github.com/clojure/tools.cku</RepositoryUrl>
19+
<Company>Clojure contributors</Company>
20+
<PackageTags>Clojure;ClojureCLR</PackageTags>
21+
<Version>1.0.214</Version>
22+
</PropertyGroup>
23+
24+
<ItemGroup>
25+
<EmbeddedResource CopyToOutputDirectory="Never" Include="..\..\clojure\clojure\tools\**\*" />
26+
</ItemGroup>
27+
28+
</Project>

src/test/clojure/clojure/tools/cli_test.cljc

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,18 @@
5959
:long-opt "--[no-]foo"})))
6060
(testing "throws AssertionError on unset :id, duplicate :short-opt or :long-opt,
6161
multiple :default(-fn) entries per :id, or both :assoc-fn/:update-fn present"
62-
(is (thrown? #?(:clj AssertionError :cljs :default)
62+
(is (thrown? #?(:clj AssertionError :cljr Exception :cljs :default)
6363
(compile-option-specs [["-a" :id nil]])))
64-
(is (thrown? #?(:clj AssertionError :cljs :default)
64+
(is (thrown? #?(:clj AssertionError :cljr Exception :cljs :default)
6565
(compile-option-specs [{:id :a :short-opt "-a"} {:id :b :short-opt "-a"}])))
66-
(is (thrown? #?(:clj AssertionError :cljs :default)
66+
(is (thrown? #?(:clj AssertionError :cljr Exception :cljs :default)
6767
(compile-option-specs [{:id :alpha :long-opt "--alpha"} {:id :beta :long-opt "--alpha"}])))
68-
(is (thrown? #?(:clj AssertionError :cljs :default)
68+
(is (thrown? #?(:clj AssertionError :cljr Exception :cljs :default)
6969
(compile-option-specs [{:id :alpha :default 0} {:id :alpha :default 1}])))
70-
(is (thrown? #?(:clj AssertionError :cljs :default)
70+
(is (thrown? #?(:clj AssertionError :cljr Exception :cljs :default)
7171
(compile-option-specs [{:id :alpha :default-fn (constantly 0)}
7272
{:id :alpha :default-fn (constantly 1)}])))
73-
(is (thrown? #?(:clj AssertionError :cljs :default)
73+
(is (thrown? #?(:clj AssertionError :cljr Exception :cljs :default)
7474
(compile-option-specs [{:id :alpha :assoc-fn assoc :update-fn identity}]))))
7575
(testing "desugars `--long-opt=value`"
7676
(is (= (map (juxt :id :long-opt :required)
@@ -99,12 +99,16 @@
9999
(with-out-str
100100
#?(:clj (binding [*err* *out*]
101101
(compile-option-specs [[nil "--alpha" :validate nil :flag true]]))
102+
:cljr (binding [*err* *out*]
103+
(compile-option-specs [[nil "--alpha" :validate nil :flag true]]))
102104
:cljs (binding [*print-err-fn* *print-fn*]
103105
(compile-option-specs [[nil "--alpha" :validate nil :flag true]]))))))
104106
(is (re-find #"Warning:.* :validate"
105107
(with-out-str
106108
#?(:clj (binding [*err* *out*]
107109
(compile-option-specs [{:id :alpha :validate nil}]))
110+
:cljr (binding [*err* *out*]
111+
(compile-option-specs [{:id :alpha :validate nil}]))
108112
:cljs (binding [*print-err-fn* *print-fn*]
109113
(compile-option-specs [{:id :alpha :validate nil}])))))))))
110114

@@ -113,6 +117,7 @@
113117

114118
(defn parse-int [x]
115119
#?(:clj (Integer/parseInt x)
120+
:cljr (Int32/Parse x)
116121
:cljs (do (assert (re-seq #"^\d" x))
117122
(js/parseInt x))))
118123

@@ -440,10 +445,6 @@
440445
"Usage: myprog [--alpha|--beta] arg1 arg2"))))
441446

442447
(comment
443-
;; Chas Emerick's PhantomJS test runner
444-
(spit "target/runner.js"
445-
(slurp (clojure.java.io/resource "cemerick/cljs/test/runner.js")))
446-
447448
;; CLJS test runner; same as `lein cljsbuild test`
448449
(defn run-cljs-tests []
449450
(println

0 commit comments

Comments
 (0)