-
Notifications
You must be signed in to change notification settings - Fork 28
/
help_gen.cljc
281 lines (229 loc) · 8.25 KB
/
help_gen.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
(ns cli-matic.help-gen
"
## Generate help texts.
"
(:require [clojure.tools.cli :as cli]
[clojure.spec.alpha :as s]
[clojure.string :as str]
[cli-matic.specs :as S]
[cli-matic.utils :as U]
[cli-matic.utils-v2 :as U2]
[cli-matic.utils-candidates :as UB]
[cli-matic.optionals :as OPT]))
(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 ":")
(U/indent lines)
""]))
(defn generate-sections
"Generates all sections.
All those positional parameters are not that nice.
"
[name version usage commands opts-title opts examples]
(vec
(flatten
[(generate-section "NAME" name)
(generate-section "USAGE" usage)
(generate-section "EXAMPLES" examples)
(generate-section "VERSION" version)
(generate-section "COMMANDS" commands)
(generate-section opts-title opts)])))
(defn- expand-multiline-parts
"Expands multilines within parts so that they can be
aligned appropriately."
[parts]
(mapcat (fn [line-part]
(let [p1 (map str/split-lines line-part)
max-col-length (apply max (map count p1))]
(->> p1
(map #(concat % (repeat (- max-col-length (count %)) "")))
(apply mapv vector))))
parts))
(defn- summarize-for-tools-cli
"This customized `summarize` adapts the version from `clojure.tools.cli`
to expand multiline parts so that they are aligned properly.
Original description from clojure.tools.cli:
Reduce options specs into a options summary for printing at a terminal.
Note that the specs argument should be the compiled version. That effectively
means that you shouldn't call summarize directly. When you call parse-opts
you get back a :summary key which is the result of calling summarize (or
your user-supplied :summary-fn option) on the compiled option specs."
[specs]
(if (seq specs)
(let [show-defaults? (some #(and (:required %)
(or (contains? % :default)
(contains? % :default-fn))) specs)
parts (-> (map (partial cli/make-summary-part show-defaults?) specs)
expand-multiline-parts)
lens (apply map (fn [& cols] (apply max (map count cols))) parts)
lines (cli/format-lines lens parts)]
(str/join \newline lines))
""))
(defn get-options-summary
"To get the summary 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 (U2/rewrite-opts cfg subcmd)
options-str (:summary
(cli/parse-opts [] cli-cfg
:summary-fn summarize-for-tools-cli))]
(str/split-lines options-str)))
(s/fdef
get-options-summary
:args (s/cat :cfg ::S/climatic-cfg
:subcmd ::S/subcommand-path))
(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 generate-a-command
"Maybe we should use a way to format commands
E.g.
(pp/cl-format true \"~{ ~vA ~vA ~vA ~}\" v)
(clojure.pprint/cl-format true \"~3a ~a\" \"pippo\" \"pluto\")
"
[{:keys [command short description]}]
(let [[des0 _] (get-first-rest-description-rows description)]
(str " "
(U/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/subcommands)
:ret (s/coll-of string?))
(defn generate-global-help
"This is where we generate global help, so
global attributes and subcommands.
This is called for all branch nodes (not just the root),
and not leaves, therefore we had to add the path as
a parameter to tell who is who.
"
[cfg path]
(let [cmd-cfg (U2/walk cfg path)
name (U2/canonical-path-to-string
(U2/as-canonical-path
cmd-cfg))
version (U2/get-most-specific-value cfg path :version "-")
descr (U2/get-most-specific-value cfg path :description [])
[desc0 descr-extra] (get-first-rest-description-rows descr)
this-cmd (last cmd-cfg)]
(generate-sections
[(str name " - " desc0) descr-extra]
version
(str name " [global-options] command [command options] [arguments...]")
(generate-global-command-list (:subcommands this-cmd))
"GLOBAL OPTIONS" (get-options-summary cfg path)
(:examples this-cmd))))
(s/fdef
generate-global-help
:args (s/cat :cfg ::S/climatic-cfg
:cmd ::S/subcommand-path)
:ret (s/coll-of string?))
(defn arg-list-with-positional-entries
"Creates the `[arguments...]`"
[cfg cmd]
(let [pos-args (sort-by :short (U2/list-positional-parms cfg cmd))]
(if (empty? pos-args)
"[arguments...]"
(str/join " " (map :option pos-args)))))
(defn generate-subcmd-help
"This is where we generate help for a specific subcommand."
[cfg cmd]
(let [cmd-cfg (U2/walk cfg cmd)
path (U2/as-canonical-path cmd-cfg)
path-but-last (reverse (rest (reverse path)))
fullname (U2/canonical-path-to-string path)
fullname-but-last (U2/canonical-path-to-string path-but-last)
this-cmd (last cmd-cfg)
name (:command this-cmd)
shortname (:short this-cmd)
name-short (if shortname
(str "[" name "|" shortname "]")
name)
descr (:description this-cmd)
version-or-nil (:version this-cmd)
[desc0 descr-extra] (get-first-rest-description-rows descr)
arglist (arg-list-with-positional-entries cfg cmd)]
(generate-sections
[(str fullname " - " desc0) descr-extra]
version-or-nil
(str fullname-but-last " " name-short " [command options] " arglist)
nil
"OPTIONS"
(get-options-summary cfg cmd)
(:examples this-cmd))))
(s/fdef
generate-subcmd-help
:args (s/cat :cfg ::S/climatic-cfg
:cmd ::S/subcommand-path)
:ret (s/coll-of string?))
(def MISTYPE-ERR-RATIO 0.35)
(defn generate-possible-mistypes
"We go searching if we have any candidates
to be considered mistypes.
We require a miss ratio of [[MISTYPE-ERR-RATIO]]
and we return them by similarity.
"
[wrong-subcmd commands aliases]
(let [all-subcmds (-> []
(into commands)
(into aliases))]
(UB/candidate-suggestions all-subcmds wrong-subcmd MISTYPE-ERR-RATIO)))
(s/fdef
generate-possible-mistypes
:args (s/cat :wrong-cmd string?
:subcmd (s/coll-of (s/or :s string? :nil nil?))
:aliases (s/coll-of (s/or :s string? :nil nil?)))
:ret (s/coll-of string?))
(defn generate-help-possible-mistypes
"If we have a wrong subcommand, can we guess what the correct
one could have been?
"
[cfg wrong-subcmd]
(let [appName (get-in cfg [:app :command] "?")
commands (map :command (:commands cfg))
aliases (map :short (:commnads cfg))
candidates (generate-possible-mistypes wrong-subcmd commands aliases)
error (str appName ": unknown sub-command '" wrong-subcmd "'.")]
(if (empty? candidates)
; No candidates, just the error
[error]
; Have some, let's show them.
[error
""
"The most similar subcommands are:"
(mapv U/indent candidates)])))
(s/fdef
generate-help-possible-mistypes
:args (s/cat :cfg ::S/climatic-cfg-classic :cmd ::S/command)
:ret (s/coll-of (s/or :str string?
:cs (s/coll-of string?))))
(defn getGlobalHelperFn
[cfg path]
(U2/get-most-specific-value cfg path :global-help generate-global-help))
(defn getSubcommandHelperFn
[cfg path]
(U2/get-most-specific-value cfg path :subcmd-help generate-subcmd-help))
(OPT/orchestra-instrument)