-
Notifications
You must be signed in to change notification settings - Fork 28
/
help_gen.cljc
226 lines (174 loc) · 5.62 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
(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.platform :as P]
[cli-matic.utils :as U]
[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]
(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 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 (U/rewrite-opts cfg subcmd)
options-str (:summary
(cli/parse-opts [] cli-cfg))]
(str/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 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/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 arg-list-with-positional-entries
"Creates the `[arguments...]`"
[cfg cmd]
(let [pos-args (sort-by :short (U/list-positional-parms cfg cmd))]
(if (empty? pos-args)
"[arguments...]"
(str
(apply str (map :option pos-args))
" ..."))))
(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 (U/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)
arglist (arg-list-with-positional-entries cfg cmd)]
(generate-sections
[(str glname " " name " - " desc0) descr-extra]
nil
(str glname " " name-short " [command options] " arglist)
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?))
(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))]
(U/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 :cmd ::S/command)
:ret (s/coll-of (s/or :str string?
:cs (s/coll-of string?)))
)
(OPT/orchestra-instrument)