-
Notifications
You must be signed in to change notification settings - Fork 28
/
core.clj
610 lines (508 loc) · 17.8 KB
/
core.clj
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
(ns cli-matic.core
(:require [cli-matic.specs :as S]
[clojure.tools.cli :refer [parse-opts]]
[clojure.spec.alpha :as s]
[orchestra.spec.test :as st]
[cli-matic.presets :as PRESETS]))
;; ================ ATTENTION ====================
;; Cli-matic has one main entry-point: run!
;; Actually, most of the logic will be run in run*
;; to make testing easier.
;;
;; -----------------------------------------------
(defn assoc-new-multivalue
"Associates a new multiple value to the
current parameter map.
If the current value is not a vector, creates
a new vector with the new value."
[parameter-map option v]
(let [curr-val (get parameter-map option [])
new-val (if (vector? curr-val)
(conj curr-val v)
[v])]
(assoc parameter-map option new-val)))
;; Rewrite options from our format
;; {:opt "x" :as "Port number" :type :int}
;; to tools.cli:
;; ["-x" nil "Port number"
;; :parse-fn #(Integer/parseInt %)]
;; as specified in
;; https://github.com/clojure/tools.cli/blob/master/src/main/clojure/clojure/tools/cli.clj#L488
;;
(defn mk-cli-option
"Builds a tools.cli option out of our own format.
"
[{:keys [option short as type default multiple] :as cm-option}]
(let [preset (get PRESETS/known-presets type :unknown)
placeholder (str (:placeholder preset)
(if (= :present default) "*" ""))
positional-opts [(if (string? short)
(str "-" short)
nil)
(str "--" option " " placeholder)
as]
;; step 1 - remove :placeholder
opts-1 (dissoc preset :placeholder)
;; step 2 - add default if present and is not ":present"
opts-2 (if (and (some? default) (not= :present default))
(assoc opts-1 :default default)
opts-1)
;; step 3 - if multivalue, add correct assoc-fns
opts-3 (if multiple
(assoc opts-2 :assoc-fn assoc-new-multivalue)
opts-2)]
(apply
conj positional-opts
(flatten (seq opts-3)))))
(s/fdef mk-cli-option
:args (s/cat :opts ::S/climatic-option)
:ret some?)
(defn get-subcommand
"Given args and the canonical name of a subcommand,
returns the map describing it.
"
[climatic-args subcmd]
(let [subcommands (:commands climatic-args)]
(first (filter #(= (:command %) subcmd) subcommands))))
(s/fdef get-subcommand
:args (s/cat :args ::S/climatic-cfg :subcmd string?)
:ret ::S/a-command)
(defn all-subcommands-aliases
"Maps all subcommands and subcommand aliases
to their canonical name.
E.g. {'add': 'add', 'a': 'add'}.
We basically add them all, then remove
nil keys.
"
[climatic-args]
(let [subcommands (:commands climatic-args)]
(dissoc
(merge
;; a map of 'cmd' -> 'cmd'
(into {}
(map
(fn [{:keys [command short]}]
[command command])
subcommands))
(into {}
(map
(fn [{:keys [command short]}]
[short command])
subcommands)))
nil)))
(s/fdef all-subcommands-aliases
:args (s/cat :args ::S/climatic-cfg)
:ret (s/map-of string? string?))
(defn all-subcommands
"Returns all subcommands, as strings.
We get all versions of all subcommands.
"
[climatic-args]
(set (keys (all-subcommands-aliases climatic-args))))
(s/fdef all-subcommands
:args (s/cat :args ::S/climatic-cfg)
:ret set?)
(defn canonicalize-subcommand
"Returns the 'canonical' name of a subcommand,
i.e. the one that appears in :command, even
if we pass an alias or short version."
[commands subcmd]
(get (all-subcommands-aliases commands) subcmd))
(s/fdef canonicalize-subcommand
:args (s/cat :args ::S/climatic-cfg :sub string?)
:ret string?)
(defn get-options-for
"Gets specific :options for a subcommand or,
if nil, for global."
[climatic-args subcmd]
(if (nil? subcmd)
(:global-opts climatic-args)
(:opts (get-subcommand climatic-args subcmd))))
;; Out of a cli-matic arg list,
;; generates a set of commands for tools.cli
(defn rewrite-opts
"
Out of a cli-matic arg list, generates a set of
options for tools.cli.
It also adds in the -? and --help options
to trigger display of helpness.
"
[climatic-args subcmd]
(conj
(mapv mk-cli-option (get-options-for climatic-args subcmd))
["-?" "--help" "" :id :_help_trigger]))
(s/fdef rewrite-opts
:args (s/cat :args some?
:mode (s/or :common nil?
:a-subcommand string?))
:ret some?)
;; ------------------------------------------------
;; Stuff to generate help pages
;; ------------------------------------------------
(defn asString
"Turns a collection of strings into one string,
or the string itself."
[s]
(if (string? s)
s
(clojure.string/join "\n" s)))
(defn indent-string
"Indents a single string."
[s]
(str " " s))
(defn indent
"Indents a single string, or each string
in a collection of strings."
[s]
(if (string? s)
(indent-string s)
(map indent-string (flatten s))))
(defn generate-section
"Generates a section (as a collection of strings,
possibly nested, but we'll flatten it out).
If a section has no content, we return [].
"
[title lines]
(if (empty? lines)
[]
[(str title ":")
(indent lines)
""]))
(defn generate-sections
"Generates all sections.
All those positional parameters are not that nice.
"
[name version usage commands opts-title opts]
(vec
(flatten
[(generate-section "NAME" name)
(generate-section "USAGE" usage)
(generate-section "VERSION" version)
(generate-section "COMMANDS" commands)
(generate-section opts-title opts)])))
(defn get-options-summary
"To get the sumamry of options, we pass options to
tools.cli parse-opts and an empty set of arguments.
Parsing will fail but we get the :summary.
We then split it into a collection of lines."
[cfg subcmd]
(let [cli-cfg (rewrite-opts cfg subcmd)
options-str (:summary
(parse-opts [] cli-cfg))]
(clojure.string/split-lines options-str)))
(defn get-first-rest-description-rows
"get title and description of description rows"
[row-or-rows]
(cond
(string? row-or-rows)
[row-or-rows []]
(zero? (count row-or-rows))
["?" []]
:else
[(first row-or-rows) (rest row-or-rows)]))
(defn pad
"Pads s[, s1] to so many characters"
[s s1 len]
(subs (str s
(when s1
(str ", " s1))
" ")
0 len))
(defn generate-a-command
"Maybe we should use a way to format commands
E.g.
(pp/cl-format true \"~{ ~vA ~vA ~vA ~}\" v)"
[{:keys [command short description]}]
(let [[des0 _] (get-first-rest-description-rows description)]
(str " "
(pad command short 20)
" "
des0)))
(defn generate-global-command-list
"Creates a list of commands and descriptions.
Commands are of kind ::S/commands
"
[commands]
(map generate-a-command commands))
(s/fdef
generate-global-command-list
:args (s/cat :commands ::S/commands)
:ret (s/coll-of string?))
(defn generate-global-help
"This is where we generate global help, so
global attributes and subcommands."
[cfg]
(let [name (get-in cfg [:app :command])
version (get-in cfg [:app :version])
descr (get-in cfg [:app :description])
[desc0 descr-extra] (get-first-rest-description-rows descr)]
(generate-sections
[(str name " - " desc0) descr-extra]
version
(str name " [global-options] command [command options] [arguments...]")
(generate-global-command-list (:commands cfg))
"GLOBAL OPTIONS"
(get-options-summary cfg nil))))
(s/fdef
generate-global-help
:args (s/cat :cfg ::S/climatic-cfg)
:ret (s/coll-of string?))
(defn generate-subcmd-help
"This is where we generate help for a specific subcommand."
[cfg cmd]
(let [glname (get-in cfg [:app :command])
cmd-cfg (get-subcommand cfg cmd)
name (:command cmd-cfg)
shortname (:short cmd-cfg)
name-short (if shortname
(str "[" name "|" shortname "]")
name)
descr (:description cmd-cfg)
[desc0 descr-extra] (get-first-rest-description-rows descr)]
(generate-sections
[(str glname " " name " - " desc0) descr-extra]
nil
(str glname " " name-short " [command options] [arguments...]")
nil
"OPTIONS"
(get-options-summary cfg cmd))))
(s/fdef
generate-subcmd-help
:args (s/cat :cfg ::S/climatic-cfg :cmd ::S/command)
:ret (s/coll-of string?))
;; -----------------------------------------------------
;; Here we parse our command line.
;; -----------------------------------------------------
(defn mkError
"Builds an error condition."
[config subcommand error text]
{:subcommand subcommand
:subcommand-def (if (or (= error :ERR-UNKNOWN-SUBCMD)
(= error :ERR-NO-SUBCMD)
(= error :ERR-PARMS-GLOBAL)
(= error :HELP-GLOBAL))
nil
(get-subcommand config subcommand))
:commandline {}
:parse-errors error
:error-text (asString text)})
;; TODO s/fdef
(defn errors-for-missing-mandatory-args
"Gets us a sequence of errors if mandatory options are missing"
[climatic-options parsed-opts]
(let [mandatory-options (filter
#(= :present (:default %))
climatic-options)
curr-options (:options parsed-opts)]
(reduce
(fn [a v]
(let [optname (:option v)
val (get curr-options (keyword optname) :MISSING)]
(if (= :MISSING val)
(conj a (str "Missing option: " optname))
a)))
[]
mandatory-options)))
(s/fdef errors-for-missing-mandatory-args
:args (s/cat :options ::S/opts
:parsed-opts map?)
:ret (s/coll-of string?))
(defn parse-cmds
"This is where magic happens.
We first parse global options, then stop,
get the subcommand, parse specific options for the subcommand
and if all went well we prepare run it.
This fuction returns a structure ::S/lineParseResult
that containe information about what went wrong or the command
to run.
"
[cmdline config]
(let [cli-gl-options (rewrite-opts config nil)
;_ (prn "Cmdline" cmdline)
parsed-gl-opts (parse-opts cmdline cli-gl-options :in-order true)
missing-gl-opts (errors-for-missing-mandatory-args
(get-options-for config nil)
parsed-gl-opts)
;_ (prn "Common cmdline" parsed-common-cmdline)
{gl-errs :errors gl-opts :options gl-args :arguments} parsed-gl-opts]
(cond
; any parse errors?
(some? gl-errs)
(mkError config nil :ERR-PARMS-GLOBAL gl-errs)
; did we ask for help?
(some? (:_help_trigger gl-opts))
(mkError config nil :HELP-GLOBAL nil)
:else
(let [subcommand (first gl-args)
subcommand-parms (vec (rest gl-args))]
(cond
(nil? subcommand)
(mkError config nil :ERR-NO-SUBCMD "")
(nil? ((all-subcommands config) subcommand))
(mkError config subcommand :ERR-UNKNOWN-SUBCMD "")
:else
(let [canonical-subcommand (canonicalize-subcommand config subcommand)
cli-cmd-options (rewrite-opts config canonical-subcommand)
;_ (prn "O" cli-cmd-options)
parsed-cmd-opts (parse-opts subcommand-parms cli-cmd-options)
missing-cmd-opts (errors-for-missing-mandatory-args
(get-options-for config canonical-subcommand)
parsed-cmd-opts)
;_ (prn "Subcmd cmdline" parsed-cmd-opts)
;_ (prn "G" missing-gl-opts)
;_ (prn "C" missing-cmd-opts)
{cmd-errs :errors cmd-opts :options cmd-args :arguments} parsed-cmd-opts]
(cond
; asking for help?
(some? (:_help_trigger cmd-opts))
(mkError config canonical-subcommand :HELP-SUBCMD nil)
; any missing required global parm?
(pos? (count missing-gl-opts))
(mkError config nil :ERR-PARMS-GLOBAL missing-gl-opts)
; missing required parms?
(pos? (count missing-cmd-opts))
(mkError config canonical-subcommand :ERR-PARMS-SUBCMD missing-cmd-opts)
; no errors?
(nil? cmd-errs)
{:subcommand canonical-subcommand
:subcommand-def (get-subcommand config canonical-subcommand)
:commandline (into
(into gl-opts cmd-opts)
{:_arguments cmd-args})
:parse-errors :NONE
:error-text ""}; having errors....
:else
(mkError config canonical-subcommand :ERR-PARMS-SUBCMD cmd-errs))))))))
(s/fdef
parse-cmds
:args (s/cat :args (s/coll-of string?)
:opts ::S/climatic-cfg)
:ret ::S/lineParseResult)
(defn assert-unique-values
"Check that all values are unique.
name is the area of the configuration
vec-opts are the options to check
option is the keyword to check.
"
[name vec-opts option]
(let [optName (if (nil? name) "global" name)
allOptions (filter some? (map option vec-opts))
dupes (filterv (fn [[k v]] (> v 1)) (frequencies allOptions))]
(cond
(not (empty? dupes))
(throw (IllegalAccessException.
(str "In option area: " optName " for options of type " option " some option names are not unique: " dupes))))))
(s/fdef
assert-unique-values
:args (s/cat :name (s/or :some-subcmd ::S/existing-string
:global nil?)
:vec-opts any? ; ::S/commands
:option keyword?))
;;
;; Asserts sanity of initial configuration.
;; If this goes wrong, throws an error.
;;
(defn assert-cfg-sanity
"Checks configuration and throws if anything wrong.
1. are :option values unique?
2. are :short values unique?
First we make a list of `nil` plus all subcmds.
"
[currentCfg]
(let [all-subcommands (into [nil]
(all-subcommands currentCfg))]
(mapv #(assert-unique-values %
(get-options-for currentCfg %)
:option) all-subcommands)
(mapv #(assert-unique-values %
(get-options-for currentCfg %)
:short) all-subcommands)
; just say nil
nil))
(s/fdef assert-cfg-sanity
:args (s/cat :opts ::S/climatic-cfg))
;
; builds a return value
;
(defn ->RV
"This is a Return Value, i.e. what happens after the
parsing is done and possibly the subcommand was invoked."
[return-code type stdout subcmd stderr]
(let [fnStrVec (fn [s]
(cond
(nil? s) []
(string? s) [s]
:else s))]
{:retval return-code
:status type
:help stdout
:subcmd subcmd
:stderr (fnStrVec stderr)}))
(s/fdef
->RV
:args (s/cat :rv int? :status some? :help any? :subcmd any? :stderr any?)
:rets ::S/RV)
;
;
;
;
(defn invoke-subcmd
"Invokes a subcommand, and produces a Return Value.
The subcommand may:
- return an integer (to specify exit code)
- return nil
- throw a Throwable object
"
[subcommand-def options]
(try
(let [rv ((:runs subcommand-def) options)]
(cond
(nil? rv) (->RV 0 :OK nil nil nil)
(int? rv) (if (zero? rv)
(->RV 0 :OK nil nil nil)
(->RV rv :ERR nil nil nil))
:else (->RV 0 :OK nil nil nil)))
(catch Throwable t
(->RV -1 :EXCEPTION nil nil
(str "JVM Exception: "
(with-out-str (println t)))))))
;; Executes our code.
;; It will try and parse the arguments via clojure.tools.cli
;; and detect our subcommand.
;; If no subcommand was found, it will print the error reminder.
;; On exceptions, it will raise an exception message.
(defn run-cmd*
[setup args]
(let [parsed-opts (parse-cmds args setup)]
;; maybe there was an error parsing
(condp = (:parse-errors parsed-opts)
:ERR-CFG (->RV -1 :ERR-CFG nil nil "Error in cli-matic configuration")
:ERR-NO-SUBCMD (->RV -1 :ERR-NO-SUBCMD :HELP-GLOBAL nil "No sub-command specified")
:ERR-UNKNOWN-SUBCMD (->RV -1 :ERR-UNKNOWN-SUBCMD :HELP-GLOBAL nil "Unknown sub-command")
:HELP-GLOBAL (->RV 0 :OK :HELP-GLOBAL nil nil)
:ERR-PARMS-GLOBAL (->RV -1 :ERR-PARMS-GLOBAL :HELP-GLOBAL nil
(str "Global option error: " (:error-text parsed-opts)))
:HELP-SUBCMD (->RV 0 :OK :HELP-SUBCMD (:subcommand parsed-opts) nil)
:ERR-PARMS-SUBCMD (->RV -1 :ERR-PARMS-SUBCMD :HELP-SUBCMD (:subcommand parsed-opts)
(str "Option error: " (:error-text parsed-opts)))
:NONE (invoke-subcmd (:subcommand-def parsed-opts) (:commandline parsed-opts)))))
(defn run-cmd
"This is the actual function that is executed.
It wraps run-cmd* and then does the printing
of any errors, of help pages and System.exit.
"
[args setup]
(let [{:keys [help stderr subcmd retval]}
(run-cmd* setup (if (nil? args) [] args))]
(if (not (empty? stderr))
(println
(asString
(flatten
["** ERROR: **" stderr "" ""]))))
(cond
(= :HELP-GLOBAL help)
(println (asString (generate-global-help setup)))
(= :HELP-SUBCMD help)
(println (asString (generate-subcmd-help setup subcmd))))
(System/exit retval)))
(st/instrument)