-
Notifications
You must be signed in to change notification settings - Fork 28
/
specs.cljc
202 lines (134 loc) · 5.08 KB
/
specs.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
(ns cli-matic.specs
(:require [clojure.spec.alpha :as s]))
(defn has-elements? [s]
(pos? (count s)))
(s/def ::anything (s/or :nil nil?
:some some?))
(s/def ::existing-string (s/and string? has-elements?))
; In many places you can have "foo” or ["foo" "bar"]
(s/def ::string-or-strings (s/or :a-string ::existing-string
:coll-str (s/coll-of string?)))
(s/def ::climatic-errors #{:ERR-CFG
:ERR-NO-SUBCMD
:ERR-UNKNOWN-SUBCMD
:ERR-PARMS-GLOBAL
:ERR-PARMS-SUBCMD})
;; The way your process runs; will generate
;; a return val and some lines for stdout/stderr.
(s/def ::retval int?)
(s/def ::status (s/or :oth #{:OK :EXCEPTION}
:err ::climatic-errors))
(s/def ::help (s/or :none nil?
:some #{:HELP-GLOBAL :HELP-SUBCMD}))
(s/def ::subcmd (s/coll-of ::existing-string))
(s/def ::stderr (s/coll-of string?))
(s/def ::RV
(s/keys :req-un [::retval ::status ::help ::subcmd ::stderr]))
;; Cli-matic option definition
(s/def ::option ::existing-string) ;; ex-string
(s/def ::positional-arg (s/and integer?
(s/or :z zero? :p pos?)))
(s/def ::short (s/or :str ::existing-string
:pos ::positional-arg))
(s/def ::as ::string-or-strings)
(s/def ::set-of-strings
(s/and set?
(s/coll-of ::existing-string :min-count 1)))
(s/def ::set-of-kws
(s/and set?
(s/coll-of keyword? :min-count 1)))
(s/def ::set-of-vals
(s/or :set-str ::set-of-strings
:set-kw ::set-of-kws))
(s/def ::type
(s/or :plain-kw #{:int :int-0
:string :keyword
:float :float-0
:yyyy-mm-dd
:slurp :slurplines
:edn :ednfile
:json :jsonfile
:with-flag :flag
:yaml :yamlfile}
:set-vals ::set-of-vals))
(s/def ::default some?)
(s/def ::env ::existing-string)
(s/def ::spec some?) ; \TODO how do we know it's a valid spec?
(s/def ::climatic-option
(s/keys :req-un [::option ::as ::type]
:opt-un [::short ::default ::env ::spec]))
;; CLI-matic configuration
(s/def ::description ::string-or-strings)
(s/def ::examples ::string-or-strings)
(s/def ::version ::existing-string)
(s/def ::command ::existing-string)
(s/def ::opts (s/coll-of ::climatic-option))
(s/def ::runs ifn?)
(s/def ::on-shutdown ifn?)
(s/def ::app (s/keys :req-un [::command ::description ::version]))
(s/def ::global-opts ::opts)
(s/def ::a-command (s/keys :req-un [::command ::opts ::runs]
:opt-un [::short ::description ::spec ::on-shutdown]))
(s/def ::commands (s/coll-of ::a-command))
(s/def ::climatic-cfg-classic
(s/keys :req-un [::app ::global-opts ::commands]))
(s/def ::global-help ifn?)
(s/def ::subcmd-help ifn?)
;; Return value of parsing with tools.cli
(s/def ::parsedCliOpts map?)
(s/def ::mapOfCliParams (s/map-of string? (s/or :empty nil? :str string?)))
;
; Configuration for nested sub-commands
; (see bug #69 and more)
;
(s/def ::any-subcommand (s/keys :req-un [::command ::opts]
:opt-un [::short ::description ::spec
::version
::examples
::on-shutdown ::global-help ::subcmd-help]))
; root has a version
(s/def ::root-subcommand
(s/keys :req-un [::version]))
(defn no-positional-opts
"Makes sure that this subcommand does not have
any positional argument in its opts"
[any-subcmd]
(let [o (:opts any-subcmd)]
(empty?
(filter int? o))))
(s/def ::branch-subcommand
(s/and
::any-subcommand
(s/keys :req-un [::subcommands])
; no positional arguments
no-positional-opts))
(s/def ::leaf-subcommand
(s/and
::any-subcommand
(s/keys :req-un [::runs])))
(s/def ::a-subcommand (s/or :branch ::branch-subcommand
:leaf ::leaf-subcommand))
(s/def ::subcommands (s/coll-of
::a-subcommand))
(s/def ::climatic-cfg
::a-subcommand)
; A subcommand path.
; If empty, no subcommands and no globals
; Each member is the canonical name of a subcommand.
; Each member but the last is of type ::branch-subcommand
(s/def ::subcommand-path
(s/coll-of ::existing-string))
; A path exploded into the actual subcommad definitions
(s/def ::subcommand-executable-path
(s/coll-of ::any-subcommand
:min-count 1))
;; Parsing of options.
(s/def ::subcommand (s/or :empty nil?
:some ::existing-string))
(s/def ::subcommand-def (s/or :empty nil?
:some ::a-command))
(s/def ::commandline map?) ;; contains :_arguments as vec
(s/def ::parse-errors (s/or :oth #{:NONE :HELP-GLOBAL :HELP-SUBCMD}
:err ::climatic-errors))
(s/def ::error-text string?)
(s/def ::lineParseResult (s/keys :req-un [::subcommand ::subcommand-path ::subcommand-def ::commandline ::parse-errors ::error-text]))