-
Notifications
You must be signed in to change notification settings - Fork 28
/
presets.cljc
262 lines (206 loc) · 6.34 KB
/
presets.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
(ns cli-matic.presets
"
### Known presets
"
(:require [clojure.string :as str]
[cli-matic.optionals :as OPT]
[cli-matic.platform :as P]
[clojure.spec.alpha :as s]
[cli-matic.specs :as S]
[cli-matic.utils-candidates :as BU]))
;; Known presets
(defn asSingleString
"Turns a filename into a single string.
If argument is a String, it tries to resolve it first as a URI, then
as a local file name. URIs with a 'file' protocol are converted to
local file names.
"
[filename]
(cond
(nil? filename) ""
(empty? filename) ""
:else (P/slurp-file filename)))
(defn asLinesString
"Reads a text file and returns it as a collection of lines."
[filename]
(str/split-lines (asSingleString filename)))
(defn asDecodedEdnValue
"Decodes the value as an EDN structure."
[s]
;;(edn/read-string (if (string? s) s (str/join s)))
(P/parseEdn s))
(defn asDecodedEdnFile
"Decodes the contents of a file as a JSON object."
[filename]
(P/parseEdn (asSingleString filename)))
(defn asDecodedJsonValue
"Decodes the value as a JSON object."
[s]
(OPT/json-decode s))
(defn asDecodedJsonFile
"Decodes the contents of a file as a JSON object."
[filename]
(OPT/json-decode (asSingleString filename)))
(defn asDecodedYamlValue
"Decodes the value as a YAML object."
[s]
(OPT/yaml-decode s))
(defn asDecodedYamlFile
"Decodes the contents of a file as a JSON object."
[filename]
(OPT/yaml-decode (asSingleString filename)))
(defn- replace-double-colon
[s]
(if (str/starts-with? s "::")
(str/replace s "::" ":user/")
s))
(defn asKeyword
[s]
(-> s replace-double-colon P/parseEdn keyword))
; =================================================================
; Stuff for sets
; =================================================================
(defn set-help-values
"Given a set, return allowed options as string"
[st]
(let [opts (map name st)]
(str/join "|" (sort opts))))
(s/fdef
set-help-values
:args (s/cat :set ::S/set-of-vals))
(defn set-normalized-entry
"A normalized set entry is a lowercase string
without trailing `:`."
[v]
(let [ne (str/lower-case (name v))]
(cond
(str/starts-with? ne ":") (subs ne 1)
:else ne)))
(defn set-normalize-map
"Builds a normalized map that
has as key the normalized value, and value the
original one."
[st]
(let [vals (map (fn [k]
[(set-normalized-entry k) k])
st)]
(into {} vals)))
(defn set-find-value
"Finds a string value in a set of options.
To do this, we first create a map of
{normalized original}
Returns valFound or nil.
"
[st v]
(let [mOpts (set-normalize-map st)
opt (set-normalized-entry v)]
(get mOpts opt)))
(s/fdef
set-find-value
:args (s/cat :set ::S/set-of-vals :v ::S/existing-string))
(defn set-find-didyoumean
"Finds candidates after normalization.
Return original candidates."
[st v]
(let [optMap (set-normalize-map st)
ov (set-normalized-entry v)
cands (BU/candidate-suggestions (keys optMap) ov 0.33)]
(mapv #(get optMap %) cands)))
(s/fdef
set-find-value
:args (s/cat :set ::S/set-of-vals :v ::S/existing-string))
(defn set-find-didyoumean-str
"Returns ' Did you mean A or B?' or '' if no candidates. "
[st v]
(let [cs (set-find-didyoumean st v)]
(if
(pos? (count cs))
(str " Did you mean '"
(str/join "' or '" cs)
"'?")
"")))
(s/fdef
set-find-value
:args (s/cat :set ::S/set-of-vals :v ::S/existing-string))
(defn asSet
"Sets of options are dark black magic.
They are also darn useful.
A set can be composed of all-keywords or all-strings.
Values are matched ignoring case, and the correct one
is returned in the case and type it appears in the set.
The default value, if present, is returned as-is, even if
not a member of the set.
Keywords are accepted with our without trailing `:`.
On missing values, the closest matches are searched and printed.
For example, on a set #{:a :b :c}, `A` `a` `:A` and `:a` all match `:a`.
Defaults for a set are all values, with no colons, separated by a pipe.
"
[st v]
(let [found (set-find-value st v)]
(cond
(nil? found)
(let [message (str "Value '" v "' not allowed."
(set-find-didyoumean-str st v))]
(throw (ex-info message {})))
:else
found)))
(defn- ->lowercaseSet
"Create a set of lowercase entries"
[seqOfStrings]
(->> seqOfStrings
(map str/lower-case)
set))
(def SET-OF-TRUE-FLAGS (->lowercaseSet ["Y" "Yes" "On" "T" "True" "1"]))
(def SET-OF-FALSE-FLAGS (->lowercaseSet ["N" "No" "Off" "F" "False" "0"]))
(defn parseFlag
"Converts a flag string to a boolean.
Comparison is case-insensitive.
"
[flagStr]
(let [s (str/lower-case flagStr)]
(cond
(SET-OF-TRUE-FLAGS s) true
(SET-OF-FALSE-FLAGS s) false
:else
(throw (ex-info "Unsupported flag value" {:flag s})))))
;; Remember to add these to
;; ::S/type
(def known-presets
{:int {:parse-fn P/parseInt
:placeholder "N"}
:int-0 {:parse-fn P/parseInt
:placeholder "N"
:default 0}
:float {:parse-fn P/parseFloat
:placeholder "N.N"}
:float-0 {:parse-fn P/parseFloat
:placeholder "N.N"
:default 0.0}
:string {:placeholder "S"}
:keyword {:placeholder "S"
:parse-fn asKeyword}
:with-flag {}
:flag {:parse-fn parseFlag
:placeholder "F"}
:slurp {:parse-fn asSingleString
:placeholder "f"}
:slurplines {:parse-fn asLinesString
:placeholder "f"}
:edn {:parse-fn asDecodedEdnValue
:placeholder "edn"}
:ednfile {:parse-fn asDecodedEdnFile
:placeholder "f"}
:json {:parse-fn asDecodedJsonValue
:placeholder "json"}
:jsonfile {:parse-fn asDecodedJsonFile
:placeholder "f"}
:yaml {:parse-fn asDecodedYamlValue
:placeholder "yaml"}
:yamlfile {:parse-fn asDecodedYamlFile
:placeholder "f"}
; dates
:yyyy-mm-dd {:placeholder "YYYY-MM-DD" :parse-fn P/asDate}
;;:validate [#(true)
;; "Must be a date in format YYYY-MM-DD"]
})
(OPT/orchestra-instrument)