-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathhelp.dylan
More file actions
314 lines (283 loc) · 10.8 KB
/
help.dylan
File metadata and controls
314 lines (283 loc) · 10.8 KB
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
Module: command-line-parser
Synopsis: Implements the --help flag and help subcommand
// TODO(cgay): Automatically display option default values. It's too easy to
// forget to add %default% to the help string.
define function program-name () => (name :: <string>)
locator-base(as(<file-locator>, application-name()))
end function;
define method command-help
(cmd :: <command>) => (help :: <string>)
let result = cmd.%command-help;
for (subst in *pattern-substitutions*)
let replacement = subst.substitution-function(result);
if (replacement)
result := replace-substrings(result, subst.substitution-pattern, replacement);
end;
end;
result
end method;
// make open generic?
define function add-help-subcommand
(parser :: <command-line-parser>) => ()
add-subcommand(parser,
make(<help-subcommand>,
name: "help",
help: "Display help for a subcommand.",
options: list(make(<positional-option>,
name: "subcommand",
required?: #f,
repeated?: #t,
help: "A subcommand name."))))
end function;
// TODO(cgay): we also have canonical-option-name, but I don't like it; it's
// overkill. Need to have a look at format-option-usage...
define function canonical-name
(option :: <option>) => (name :: <string>)
option.option-names[0]
end function;
define method option-help
(option :: <option>) => (help :: <string>)
let result = option.%option-help;
for (subst in *pattern-substitutions*)
let replacement = subst.substitution-function(option);
result := replace-substrings(result, subst.substitution-pattern, replacement);
end;
result
end method;
define method option-variable
(option :: <option>) => (variable-name :: <string>)
option.%option-variable
| uppercase(canonical-name(option))
end;
define class <help-subcommand> (<subcommand>)
keyword name = "help";
keyword help = "Display help message for a subcommand.";
end class;
define method execute-subcommand
(parser :: <command-line-parser>, subcmd :: <help-subcommand>)
=> (status :: false-or(<integer>));
let name = get-option-value(subcmd, "subcommand");
if (name)
let subcmd = find-subcommand(parser, name);
if (subcmd)
print-help(subcmd);
else
usage-error("Subcommand %= not found.", join(name, " "));
end;
else
print-help(parser); // 'app help' same as 'app --help'
end;
end method;
// This has a class of its own so that the help option doesn't have to be
// identified by name (which should be user settable).
define open class <help-option> (<flag-option>)
end;
// make open generic?
define function add-help-option
(parser :: <command>) => ()
add-option(parser, make(<help-option>,
names: #("help", "h"),
help: "Display this message."));
end function;
define class <substitution> (<object>)
constant slot substitution-pattern :: <string>, required-init-keyword: pattern:;
constant slot substitution-function :: <function>, required-init-keyword: function:;
end class;
// TODO(cgay): "%choices%"
define variable *pattern-substitutions*
= list(make(<substitution>,
pattern: "%default%",
function: method (option)
if (instance?(option, <option>))
// TODO(cgay): Make <boolean>s print as true/false in
// %default% substitutions. There's some subtlety for
// <flag-option> because of negative options.
// Make a format-option-value generic?
format-to-string("%s", option.option-default)
end
end),
make(<substitution>,
pattern: "%app%",
function: always(program-name())));
// For use by extension modules.
define function add-pattern-substitution
(pattern :: <string>, fn :: <function>) => ()
*pattern-substitutions*
:= concatenate(*pattern-substitutions*,
make(<substitution>, pattern: pattern, function: fn));
end function;
define method visible-option-name
(raw-name :: <string>) => (dash-name :: <string>)
concatenate(if (raw-name.size = 1) "-" else "--" end, raw-name)
end;
define method canonical-option-name
(option :: <option>, #key plain?) => (dash-name :: <string>)
if (plain?)
option.option-names.first
else
option.option-names.first.visible-option-name
end
end;
// Return a string showing how this option is used on the command-line.
// TODO(cgay): this is not called. I probably broke it at some point.
// I think it should affect the way the option is displayed in the Options:
// table. e.g., "--flag[=yes/no]"
define open generic format-option-usage
(option :: <option>) => (usage :: <string>);
define method format-option-usage
(option :: <option>) => (usage :: <string>)
option.canonical-option-name
end;
define function print-help
(cmd :: <command>, #key stream :: <stream> = *standard-output*)
format(stream, "%s\n", command-help(cmd));
format(stream, "\n%s\n", generate-usage(cmd));
print-options(cmd, stream);
if (cmd.has-subcommands?)
format(stream, "\nSubcommands:\n");
let rows = subcommand-rows(cmd);
if (~empty?(rows))
columnize(stream, $subcommand-columns, rows);
new-line(stream);
end;
let help-subcommand = find-subcommand(cmd, <help-subcommand>);
if (help-subcommand)
new-line(stream);
format(stream, "Use '%s %s <cmd> [<cmd> ...]' to see subcommand options.\n",
program-name(), subcommand-name(help-subcommand));
end;
end;
if (~instance?(cmd, <command-line-parser>))
let help-option = find-option(cmd, <help-option>);
if (help-option)
new-line(stream);
format(stream, "Use '%s %s' to see global options.\n",
program-name(), help-option.canonical-name.visible-option-name);
end;
end;
end function;
define function print-options
(cmd :: <command>, stream :: <stream>) => ()
// Column widths are chosen to have a max table width of 79 until columnist can
// determine the screen width.
let o-rows = option-rows(cmd);
if (~empty?(o-rows))
format(stream, "\nOptions:\n");
columnize(stream, $optional-options-columns, o-rows);
new-line(stream);
end;
let p-rows = positional-option-rows(cmd);
if (~empty?(p-rows))
format(stream, "\nPositional arguments:\n");
columnize(stream, $positional-option-columns, p-rows);
new-line(stream);
end;
end function;
define constant $positional-option-columns
= list(make(<column>),
make(<column>, maximum-width: 25),
make(<column>, maximum-width: 50, pad?: #f));
define function positional-option-rows
(cmd :: <command>) => (rows :: <sequence>)
let rows = make(<stretchy-vector>);
for (opt in cmd.positional-options)
let name = opt.option-variable;
if (opt.option-repeated?)
name := concatenate(name, "...");
end;
add!(rows, list("", name, opt.option-help));
end;
rows
end function;
define constant $optional-options-columns
= list(make(<column>), // empty string, creates column border
make(<column>), // short option names
make(<column>, maximum-width: 25), // long option names
make(<column>, maximum-width: 50, pad?: #f)); // docs
// Return rows of #[short-names, long-names, documentation]
define function option-rows
(parser :: <command>) => (rows :: <sequence>)
let rows = make(<stretchy-vector>);
for (option in parser.pass-by-name-options)
let flag? = instance?(option, <flag-option>);
add!(rows,
vector("", // causes a two space indent
join(map(visible-option-name, option.short-names), ", "),
join(map(method (name)
concatenate(visible-option-name(name),
flag? & "" | "=",
flag? & "" | (option.option-variable
| canonical-name(option)))
end,
option.long-names),
" "),
option.option-help));
end for;
rows
end function;
define constant $subcommand-columns
= list(make(<column>), // empty string, creates column border
make(<column>), // subcommand name
make(<column>, // subcommand doc
maximum-width: 50, pad?: #f));
define function subcommand-rows
(cmd :: <command>) => (rows :: <sequence>)
let rows = make(<stretchy-vector>);
iterate loop (subs = as(<list>, cmd.command-subcommands), indent = "")
if (~empty?(subs))
let subcmd = subs[0];
add!(rows, list("",
concatenate(indent, subcmd.subcommand-name),
subcmd.command-help));
if (subcmd.has-subcommands?)
loop(subcmd.command-subcommands, concatenate(indent, " "));
end;
loop(tail(subs), indent)
end;
end iterate;
rows
end function;
// Generate a one-line usage message showing the order of options and arguments.
define generic generate-usage
(cmd :: <command>) => (usage :: <string>);
define method generate-usage
(cmd :: <command-line-parser>) => (usage :: <string>)
with-output-to-string (stream)
// Be careful to show where the two sets of options (global/sub) must go.
let subs? = cmd.has-subcommands?;
format(stream, "Usage: %s", program-name());
if (cmd.pass-by-name-options.size > 0)
format(stream, " [options]");
end;
if (subs?)
format(stream, " <cmd> [cmd options] args...")
end;
print-positional-args(stream, cmd);
end
end method;
define method generate-usage
(subcmd :: <subcommand>) => (usage :: <string>)
with-output-to-string (stream)
format(stream, "Usage: %s %s%s", program-name(), subcommand-name(subcmd),
if (subcmd.pass-by-name-options.size > 0)
" [options]"
else
""
end);
print-positional-args(stream, subcmd);
end;
end method;
define function print-positional-args
(stream :: <stream>, cmd :: <command>) => ()
// When positional options are added to the command we verify that certain
// constraints are met, like you can't add a repeated arg before a
// non-repeated arg or add an optional arg before a required arg, so here we
// assume those properties hold.
for (option in cmd.positional-options)
let var = option.option-variable;
format(stream,
if (option.option-required?) " %s%s" else " [%s]%s" end,
var,
if (option.option-repeated?) " ..." else "" end);
end;
end function;