forked from dylan-lang/command-line-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
macros.dylan
330 lines (313 loc) · 11.3 KB
/
macros.dylan
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
module: command-line-parser
synopsis: Interface macros for parser definition and option access.
authors: David Lichteblau <[email protected]>
copyright: See LICENSE file in this distribution.
// Introduction
// ============
//
// This is a set of macros designed to work on top of Eric Kidd's
// command-line-parser library. The idea is to provide a more readable
// interface for parser definition and option access.
//
//
// Emacs
// =====
//
// You will want to edit your .emacs to recognize keywords:
//
// (add-hook 'dylan-mode-hook
// (lambda ()
// (add-dylan-keyword 'dyl-parameterized-definition-words
// "command-line")
// (add-dylan-keyword 'dyl-other-keywords "option")
// (add-dylan-keyword 'dyl-other-keywords "positional-options")
// (add-dylan-keyword 'dyl-other-keywords "synopsis")))
//
//
// Command Line definition
// =======================
//
// Use ``define command-line'' to define a new parser class. This
// macro is intended to look similar to ``define class'', but doesn't
// define slots as such. Instead it takes ``option'' clauses. At
// initialisation time of an instance, corresponding option parsers will
// be made automatically.
//
// define command-line <my-parser> ()
// option verbose?, names: #("verbose", "v");
// end;
//
// Notes:
// - Default superclass is <command-line-parser>.
//
// - Default option class is <flag-option>.
//
// You can specify an alternative class with the kind: keyword:
// option logfile, kind: <parameter-option>;
//
// - For the options, default values are possible:
// option logfile = "default.log",
// kind: <parameter-option>,
// names: #("logfile", "L");
//
// - If you omit ``names:'', the default name is the option name.
//
// - You may want to specify types for an option:
// option logfile :: false-or(<string>), ...
// or
// option logfile :: <string> = "default.log", ...
//
// Currently type checking is done, but errors are not handled.
// Future version will probably provide a facility for automatic
// error handling and error message generation.
//
// - Remaining keywords are handed as initargs to make.
//
// - Besides ``option'' there is also ``positional-options'' and
// ``synopsis'':
// positional-options file-names;
// synopsis "Usage: foo\n",
// description: "This program fooifies.\n";
//
//
// Parsing the Command Line
// ========================
//
// Originally I had macros to make a command-line parser and do the
// parsing transparently. It wasn't consistent enough, though, and
// therefore I decided to throw out that code for now.
//
// Just do it manually:
//
// define method main (appname, #rest args);
// let parser = make(<my-parser>);
// parse-command-line(parser, args);
//
// // Here we go.
// end method main;
//
//
// Accessing the options
// =====================
//
// ``define command-line'' defines function to access the options as
// if they were real slots:
//
// define command-line <my-parser> ()
// option verbose?, names: #("v");
// end command-line;
//
// define method main (appname, #rest args);
// let parser = make(<my-parser>);
// parse-command-line(parser, args);
//
// if (parser.verbose?)
// ...
// end if;
// end method main;
//
//
// If you happen to need the option parsers, they are accessible as
// slots with "-parser" appended to the name:
// let option-parser = parser.verbose?-parser;
//
//
// Synopsis generation
// ===================
//
// Suppose you say
//
// define command-line <main-parser> ()
// synopsis "test [options] file...",
// description: "Stupid test program doing nothing with the args.";
// option verbose?, names: #("v", "verbose"),
// help: "Explanation";
// option other, names: #("other-option"),
// help: "foo";
// option version, help: "Show version";
// end command-line;
//
// Then print-synopsis(parser, stream) will print something like:
//
// Usage: test [options] file...
// Stupid test program doing nothing with the args.
// -v, --verbose Explanation
// --other-option foo
// --version Show version
//
// Macro COMMAND-LINE-DEFINER--exported
// =======================================
// Syntax: define command-line ?:name (?supers:*) ?options end
//
// - Let `?supers' default to <command-line-parser>.
//
// - Transform human-readable `?options' into patterns of the form
// [option-name, type, [default-if-any], #rest initargs]
// [positional-options-name]
// (usage, description)
//
// - Hand it over to `defcmdline-rec'.
//
// Explanation: This macro defines the visible syntax of the various clauses
// and converts each into the internal syntax described just above for
// processing by defcmdline-rec.
//
define macro command-line-definer
{ define command-line ?:name () ?options end }
=> { defcmdline-rec ?name (<command-line-parser>) () ?options end }
{ define command-line ?:name (?supers) ?options end }
=> { defcmdline-rec ?name (?supers) () ?options end }
supers:
{ ?super:expression, ... } => { ?super, ... }
{ } => { }
options:
{ option ?:name :: ?value-type:expression, #rest ?initargs:*; ... }
=> { [?name, ?value-type, [], ?initargs] ... }
{ option ?:name :: ?value-type:expression = ?default:expression,
#rest ?initargs:*; ... }
=> { [?name, ?value-type, [?default], ?initargs] ... }
{ positional-options ?:name; ... }
=> { [?name] ... }
{ synopsis ?usage:expression, #key ?description:expression = #f; ... }
=> { (?usage, ?description) ... }
{ } => { }
end macro;
// Macro DEFCMDLINE-REC--internal
// ================================
// Syntax: defcmdline-rec ?:name (?supers:*) (?processed:*) ?options end
//
// - Start out without `?processed' forms.
// - (Recursively) take each `?options' form and add it to ?processed,
// prepending it with the parser class name in the case of "option"
// or "positional-options" clauses. The resulting `?processed' form
// is a sequence of the following forms:
// (usage, description)
// [class-name, option-name, value-type, [default], initargs]
// [class-name, positional-options-name]
// - Finally, pass the `?processed' forms to `defcmdline-aux'.
//
// Explanation: The options will be processed by auxiliary rules
// managed by ``defcmdline-aux''. However, these rules need the class
// name ``?name'', which would be available to main rules only. This
// macro makes the name accessible to the auxiliary rules.
//
define macro defcmdline-rec
{ defcmdline-rec ?:name (?supers:*) (?processed:*) end }
=> { defcmdline-aux ?name (?supers) ?processed end }
{ defcmdline-rec ?:name (?supers:*) (?processed:*) [?option:*] ?rem:* end }
=> { defcmdline-rec ?name (?supers)
(?processed [?name, ?option]) ?rem
end }
{ defcmdline-rec ?:name (?supers:*) (?processed:*) (?usage:*) ?rem:* end }
=> { defcmdline-rec ?name (?supers)
((?usage) ?processed) ?rem
end }
end macro;
// Macro DEFCMDLINE-AUX--internal
// ================================
// Syntax: defcmdline-aux ?:name (?supers:*) ?options end
//
// Explanation: This is rather staightforward; code generation is
// performed by auxillary macros that output
//
// - (defcmdline-class) a class definition for `?name'
//
// - (defcmdline-init) initialize methods that add our option
// parsers (held in slots named `.*-parser') using add-option
//
// - (defcmdline-accessors) accessors that ask the parsers for the
// values that were found
//
// - (defcmdline-synopsis) a method printing usage information
//
define macro defcmdline-aux
{ defcmdline-aux ?:name (?supers:*) ?options:* end }
=> { defcmdline-class ?name (?supers) ?options end;
defcmdline-init ?name ?options end;
defcmdline-accessors ?name ?options end;
defcmdline-synopsis ?name ?options end }
end macro;
define macro defcmdline-class
{ defcmdline-class ?:name (?supers:*) ?slots end }
=> { define class ?name (?supers)
?slots
end class }
slots:
{ [?class:name, ?option:name, ?value-type:expression, [?default:*],
#rest ?initargs:*,
#key ?kind:expression = <flag-option>,
?names:expression = #f,
#all-keys] ... }
=> { constant slot ?option ## "-parser"
= begin
let names = ?names;
make(?kind,
names: names | #( ?"option" ),
type: ?value-type,
?default,
?initargs);
end; ... }
{ [?class:name, ?positional-options:name] ... }
=> { ... }
{ (?usage:*) ... }
=> { ... }
{ } => { }
default:
{ ?:expression }
=> { default: ?expression }
{ } => { }
end macro;
define macro defcmdline-init
{ defcmdline-init ?:name ?adders end }
=> { define method initialize (instance :: ?name,
#next next-method, #key, #all-keys)
=> ();
next-method();
?adders
end method initialize }
adders:
{ [?class:name, ?option:name, ?value-type:expression, [?default:*],
?initargs:*] ... }
=> { add-option(instance, ?option ## "-parser" (instance)); ... }
{ [?class:name, ?positional-options:name] ... }
=> { ... }
{ (?usage:*) ... }
=> { ... }
{ } => { }
end macro;
define macro defcmdline-accessors
{ defcmdline-accessors ?:name ?accessors end }
=> { ?accessors }
accessors:
{ [?class:name, ?option:name, ?value-type:expression,
[?default:*], ?initargs:*] ... }
=> { // Value may differ from ?value-type for <repeated-parameter-option>
define method ?option (arglistparser :: ?class)
=> (value);
let optionparser = ?option ## "-parser" (arglistparser);
option-value(optionparser);
end method ?option; ... }
{ [?class:name, ?positional-options:name] ... }
=> { define method ?positional-options (arglistparser :: ?class)
=> (value :: <sequence>);
positional-options(arglistparser);
end method; ... }
{ (?usage:*) ... }
=> { ... }
{ } => { }
end macro;
define macro defcmdline-synopsis
{ defcmdline-synopsis ?:name (?usage:expression, ?description:expression)
?options:*
end }
=> { define method print-synopsis (parser :: ?name, stream :: <stream>,
#next next-method,
#key usage, description)
=> ();
let usage = usage | ?usage;
let desc = description | ?description;
next-method(parser, stream, usage: usage, description: desc)
end method }
{ defcmdline-synopsis ?:name ?options:* end }
=> { }
end macro;