raz / commit f9b557c0

commit
f9b557c0b77509a411e9ccc13ca0bf9eb5f8541d
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
1d64f37c
Opt, a command line through one table; Files to the library

Opt after ~/lib/getopt: grouped letters, attached and next-word
values, long options in full with = or the next word, -- and -, the
end at the first operand. Help and messages made from the table, word
for word as getopt's; -h and --help answered on entering a scope, by
a quiet read of a copy of the parser. The table checked first,
status 1. Its main runs itself per case through Sys.spawn and pipes:
27 cases, getopt's keep among them. Doc held in test/doc, Pretty
leaves it as written, Mutate fuzzes it in make check. Files moves
from tools unchanged; the tools and rloc find it in lib.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

patch · log

7 files changed, +646 -50

PLAN.md (history) modified +1 -1

@@ -42,7 +42,7 @@
4242 - [x] 5. `Str.encode`, `Str.decode`, `Str.toInt`; `Map.remove` and
4343 `Map.next`; `Sort`; `Math`; `Fmt.real`. Each module's `main`
4444 tests it.
45-- [ ] 5a. Asked for rloc, and every program after it: `Opt`, a
45+- [x] 5a. Asked for rloc, and every program after it: `Opt`, a
4646 command line read through one table, after `~/lib/getopt`.
4747 getopt(3)'s rules with GNU's long options (`-lw`, `-b dev`,
4848 `--bar=dev`, `--`); the usage line and a sentence for every

README.md (history) modified +3 -2

@@ -127,8 +127,9 @@
127127
128128 `Rt` (ending the program, failed checks, signals), `Sys` (system calls), `Out`
129129 (buffered output), `C`, `Lock`, `Thread` (threads without C), `Mem`,
130-`Heap`, `Arena`, `Str`, `Buf`, `Fmt`, `Math`, `List(T)`, `Map(K, V;
131-hash, eq)` and `Sort(T; less)`. `tools/Doc.raz` prints any of them:
130+`Heap`, `Arena`, `Str`, `Buf`, `Fmt`, `Math`, `Files`, `Opt` (a command
131+line read through one table), `List(T)`, `Map(K, V; hash, eq)` and
132+`Sort(T; less)`. `tools/Doc.raz` prints any of them:
132133
133134 $ ./raz -o Doc tools/Doc.raz && ./Doc lib/Str.raz
134135

lib/Files.raz (history) added +46 -0

@@ -0,0 +1,46 @@
1+(* What most programs need of the command line and of files: an
2+ argument as a string, and a whole file read or written. *)
3+module Files;
4+import Sys, Buf;
5+
6+(* Argument i, a C string, as a slice. *)
7+proc arg*(i: I64): []U8;
8+var s: ^U8; n: I64;
9+begin
10+ s := ARGV()[i];
11+ n := 0;
12+ while s[n] # 0 do INC(n); end;
13+ return s[0..n];
14+end;
15+
16+(* Adds the file at path to b; false if it can't be read. *)
17+proc read*(path: []U8; b: ^Buf.Buf): BOOL;
18+var fd, n: I64; chunk: [4096]U8;
19+begin
20+ fd := Sys.openRead(path);
21+ if fd < 0 then return false; end;
22+ repeat
23+ n := Sys.read(fd, chunk);
24+ if n > 0 then Buf.str(b, chunk[0..n]); end;
25+ until n <= 0;
26+ Sys.close(fd);
27+ return n = 0;
28+end;
29+
30+(* Makes the file at path hold data; false if it can't be written. *)
31+proc write*(path, data: []U8): BOOL;
32+var fd, n, at: I64;
33+begin
34+ fd := Sys.create(path);
35+ if fd < 0 then return false; end;
36+ at := 0;
37+ while at < LEN(data) do
38+ n := Sys.write(fd, data[at..LEN(data)]);
39+ if n <= 0 then Sys.close(fd); return false; end;
40+ at := at + n;
41+ end;
42+ Sys.close(fd);
43+ return true;
44+end;
45+
46+end Files.

lib/Opt.raz (history) added +537 -0

@@ -0,0 +1,537 @@
1+(* A command line read through one table, by the rules of the C
2+ library's getopt with GNU's long options (report, section 11). The
3+ program describes its synopsis lines, subcommands and options once,
4+ in a slice of Row; the help and the messages for mistakes are made
5+ from it, so they can't drift from it.
6+
7+ Depends on Files for the words, and writes the help and the
8+ messages itself, then ends the program.
9+ Leaves out: options after operands (GNU's reordering), a program
10+ that keeps -h for itself, and completion for concha. *)
11+module Opt;
12+import Files, Str, Buf, Fmt, Heap, Out, Sys, Rt;
13+
14+type
15+ Row* = variant
16+ usage(scope, line: []U8); (* a synopsis line, without "usage: " *)
17+ sub(scope, name, help: []U8); (* a subcommand *)
18+ option(scope: []U8; letter: U8; long, value, help: []U8);
19+ end;
20+ Parser* = record
21+ letter*: U8; (* the option just read *)
22+ value*: []U8; (* its value; "" for a flag *)
23+ long*: BOOL; (* it was written by its long name *)
24+ (* the rest are Opt's own *)
25+ name: []U8; (* the program's, as the messages begin *)
26+ table: []Row;
27+ scope: []U8;
28+ at: I64; (* the next word *)
29+ group: []U8; (* the letters left of a word of them, as -lw *)
30+ spelt: []U8; (* the option just read as written, without its dashes *)
31+ ended: BOOL; (* the scope's options have ended *)
32+ help: BOOL; (* the option just read is -h or --help *)
33+ end;
34+
35+(* What read makes of a word or a letter. *)
36+const read1 = 1; readEnd = 0; readSkip = -1;
37+
38+(* b written whole to fd, then freed. *)
39+proc say(fd: I64; b: ^Buf.Buf);
40+var s: []U8; n: I64;
41+begin
42+ s := Buf.bytes(b);
43+ while LEN(s) > 0 do
44+ n := Sys.write(fd, s);
45+ if n <= 0 then break; end;
46+ s := s[n..LEN(s)];
47+ end;
48+ Buf.done(b);
49+end;
50+
51+proc inScope(p: ^Parser; r: Row): BOOL;
52+begin
53+ match r of
54+ | usage(s, _line): return s = p^.scope;
55+ | sub(s, _name, _help): return s = p^.scope;
56+ | option(s, _l, _long, _v, _help): return s = p^.scope;
57+ end;
58+end;
59+
60+(* The scope's synopsis lines, as the help and the mistakes begin:
61+ "usage: " and the first, the others under it; or, for a scope with
62+ none, "usage: keep start". *)
63+proc usageOf(p: ^Parser; b: ^Buf.Buf);
64+var i, lines: I64; c: U8;
65+begin
66+ for i := 0 to LEN(p^.table) - 1 do
67+ match p^.table[i] of
68+ | usage(s, line):
69+ if s = p^.scope then
70+ if lines = 0 then Buf.str(b, "usage: "); else Buf.str(b, " "); end;
71+ Fmt.write(b, "{}\n", [Fmt.str(line)]);
72+ INC(lines);
73+ end;
74+ | else:
75+ end;
76+ end;
77+ if lines = 0 then
78+ Fmt.write(b, "usage: {}", [Fmt.str(p^.name)]);
79+ if LEN(p^.scope) > 0 then Buf.add(b, ' '); end;
80+ for i := 0 to LEN(p^.scope) - 1 do
81+ c := p^.scope[i];
82+ if c = '.' then c := ' '; end;
83+ Buf.add(b, c);
84+ end;
85+ Buf.add(b, 10);
86+ end;
87+end;
88+
89+(* A mistake on the command line: the sentence f, the scope's synopsis
90+ lines under it, and status 2. *)
91+proc refuse(p: ^Parser; f: []U8; args: []Fmt.Arg);
92+var b: Buf.Buf;
93+begin
94+ Buf.init(ADR(b), Heap.heap);
95+ Fmt.write(ADR(b), "{}: ", [Fmt.str(p^.name)]);
96+ Fmt.write(ADR(b), f, args);
97+ Buf.add(ADR(b), 10);
98+ usageOf(p, ADR(b));
99+ say(Sys.stderr, ADR(b));
100+ Rt.exit(2);
101+end;
102+
103+proc dashes(p: ^Parser): []U8;
104+begin
105+ if p^.long then return "--"; end;
106+ return "-";
107+end;
108+
109+(* The left column of a row in the help, as "-b, --bar label"; the
110+ help's own row if r is nil. *)
111+proc left(b: ^Buf.Buf; r: ^Row);
112+begin
113+ if r = nil then Buf.str(b, "-h, --help"); return; end;
114+ match r^ of
115+ | sub(_s, name, _help): Buf.str(b, name);
116+ | option(_s, letter, long, value, _help):
117+ if letter >= '!' then Fmt.write(b, "-{}", [Fmt.char(letter)]); else Buf.str(b, " "); end;
118+ if LEN(long) > 0 then
119+ if letter >= '!' then Buf.str(b, ", "); else Buf.str(b, " "); end;
120+ Fmt.write(b, "--{}", [Fmt.str(long)]);
121+ end;
122+ if LEN(value) > 0 then Fmt.write(b, " {}", [Fmt.str(value)]); end;
123+ | else:
124+ end;
125+end;
126+
127+proc leftWidth(r: ^Row): I64;
128+var b: Buf.Buf; n: I64;
129+begin
130+ Buf.init(ADR(b), Heap.heap);
131+ left(ADR(b), r);
132+ n := LEN(Buf.bytes(ADR(b)));
133+ Buf.done(ADR(b));
134+ return n;
135+end;
136+
137+(* A line of the list: the left column, padded to width and three
138+ spaces more, and the line of help. *)
139+proc item(b: ^Buf.Buf; r: ^Row; width: I64);
140+var i: I64;
141+begin
142+ Buf.str(b, " ");
143+ left(b, r);
144+ for i := leftWidth(r) to width + 2 do Buf.add(b, ' '); end;
145+ if r = nil then Buf.str(b, "print this and exit\n"); return; end;
146+ match r^ of
147+ | sub(_s, _name, help): Fmt.write(b, "{}\n", [Fmt.str(help)]);
148+ | option(_s, _l, _long, _v, help): Fmt.write(b, "{}\n", [Fmt.str(help)]);
149+ | else:
150+ end;
151+end;
152+
153+(* The scope's help: its synopsis lines, then its subcommands, its
154+ options and the help itself, each with its line; and status 0. *)
155+proc printHelp(p: ^Parser);
156+var b: Buf.Buf; i, width: I64; r: ^Row;
157+begin
158+ Buf.init(ADR(b), Heap.heap);
159+ usageOf(p, ADR(b));
160+ Buf.add(ADR(b), 10);
161+ width := leftWidth(nil);
162+ for i := 0 to LEN(p^.table) - 1 do
163+ r := ADR(p^.table[i]);
164+ if inScope(p, r^) and (leftWidth(r) > width) then width := leftWidth(r); end;
165+ end;
166+ for i := 0 to LEN(p^.table) - 1 do
167+ r := ADR(p^.table[i]);
168+ match r^ of
169+ | sub(s, _name, _help): if s = p^.scope then item(ADR(b), r, width); end;
170+ | else:
171+ end;
172+ end;
173+ for i := 0 to LEN(p^.table) - 1 do
174+ r := ADR(p^.table[i]);
175+ match r^ of
176+ | option(s, _l, _long, _v, _help): if s = p^.scope then item(ADR(b), r, width); end;
177+ | else:
178+ end;
179+ end;
180+ item(ADR(b), nil, width);
181+ say(Sys.stdout, ADR(b));
182+ Rt.exit(0);
183+end;
184+
185+(* The scope's option with the letter c, or with the long name long:
186+ its row, or -1, and its letter and the name of its value. *)
187+proc find(p: ^Parser; c: U8; long: []U8): (I64, U8, []U8);
188+var i: I64;
189+begin
190+ for i := 0 to LEN(p^.table) - 1 do
191+ match p^.table[i] of
192+ | option(s, letter, lg, value, _help):
193+ if (s = p^.scope) and ((c >= '!') and (letter = c) or (LEN(long) > 0) and (lg = long)) then
194+ return i, letter, value;
195+ end;
196+ | else:
197+ end;
198+ end;
199+ return -1, 0, "";
200+end;
201+
202+(* The next option, a word or a letter of one, into p: read1, or
203+ readEnd at the end of the options. A mistake is refused, or, if
204+ quiet, passed over, as readSkip. *)
205+proc read(p: ^Parser; quiet: BOOL): I64;
206+var w, val: []U8; r, eq: I64; c: U8;
207+begin
208+ p^.help := false;
209+ p^.value := "";
210+ if LEN(p^.group) = 0 then
211+ if p^.ended or (p^.at >= ARGC()) then p^.ended := true; return readEnd; end;
212+ w := Files.arg(p^.at);
213+ if (LEN(w) < 2) or (w[0] # '-') then p^.ended := true; return readEnd; end; (* an operand *)
214+ INC(p^.at);
215+ if w = "--" then p^.ended := true; return readEnd; end;
216+ if w[1] # '-' then
217+ p^.group := w[1..LEN(w)];
218+ else
219+ eq := Str.find(w, "=");
220+ if eq < 0 then p^.spelt := w[2..LEN(w)]; else p^.spelt := w[2..eq]; end;
221+ p^.long := true;
222+ if p^.spelt = "help" then p^.help := true; return read1; end;
223+ r, p^.letter, val := find(p, 0, p^.spelt);
224+ if r < 0 then
225+ if quiet then return readSkip; end;
226+ refuse(p, "unknown option --{}", [Fmt.str(p^.spelt)]);
227+ end;
228+ if (eq >= 0) and (LEN(val) = 0) then
229+ if quiet then return readSkip; end;
230+ refuse(p, "--{} takes no value", [Fmt.str(p^.spelt)]);
231+ elsif eq >= 0 then
232+ p^.value := w[eq + 1..LEN(w)];
233+ elsif LEN(val) > 0 then
234+ if p^.at >= ARGC() then
235+ if quiet then return readEnd; end;
236+ refuse(p, "--{} needs a {}", [Fmt.str(p^.spelt), Fmt.str(val)]);
237+ end;
238+ p^.value := Files.arg(p^.at);
239+ INC(p^.at);
240+ end;
241+ return read1;
242+ end;
243+ end;
244+ (* a letter of a group *)
245+ c := p^.group[0];
246+ p^.spelt := p^.group[0..1];
247+ p^.group := p^.group[1..LEN(p^.group)];
248+ p^.long := false;
249+ p^.letter := c;
250+ if c = 'h' then p^.help := true; return read1; end;
251+ r, p^.letter, val := find(p, c, "");
252+ if r < 0 then
253+ if quiet then return readSkip; end;
254+ refuse(p, "unknown option -{}", [Fmt.str(p^.spelt)]);
255+ end;
256+ if LEN(val) > 0 then
257+ if LEN(p^.group) > 0 then
258+ p^.value := p^.group;
259+ p^.group := "";
260+ elsif p^.at < ARGC() then
261+ p^.value := Files.arg(p^.at);
262+ INC(p^.at);
263+ else
264+ if quiet then return readEnd; end;
265+ refuse(p, "-{} needs a {}", [Fmt.str(p^.spelt), Fmt.str(val)]);
266+ end;
267+ end;
268+ return read1;
269+end;
270+
271+(* -h or --help among the scope's options is answered before the
272+ program sees one: a copy of the parser reads them, passing over
273+ mistakes. *)
274+proc answerHelp(p: ^Parser);
275+var q: Parser; k: I64;
276+begin
277+ q := p^;
278+ repeat
279+ k := read(ADR(q), true);
280+ if q.help and (k = read1) then printHelp(p); end;
281+ until k = readEnd;
282+end;
283+
284+(* A mistake in the table: the program's, with status 1. *)
285+proc badTable(p: ^Parser; row: I64; why: []U8);
286+var b: Buf.Buf;
287+begin
288+ Buf.init(ADR(b), Heap.heap);
289+ Fmt.write(ADR(b), "{}: option table, row {}: {}\n", [Fmt.str(p^.name), Fmt.int(row), Fmt.str(why)]);
290+ say(Sys.stderr, ADR(b));
291+ Rt.exit(1);
292+end;
293+
294+proc check(p: ^Parser);
295+var i, j: I64;
296+begin
297+ for i := 0 to LEN(p^.table) - 1 do
298+ match p^.table[i] of
299+ | sub(_s, name, _help):
300+ if LEN(name) = 0 then badTable(p, i, "a subcommand needs a name"); end;
301+ | option(s, letter, long, _v, _help):
302+ if (letter < '!') and (LEN(long) = 0) then
303+ badTable(p, i, "an option needs a letter, or a key below ! and a long name");
304+ end;
305+ if letter = '-' then badTable(p, i, "no option is -"); end;
306+ if Str.find(long, "=") >= 0 then badTable(p, i, "a long name has no ="); end;
307+ if (letter = 'h') or (long = "help") then badTable(p, i, "-h and --help are Opt's"); end;
308+ for j := 0 to i - 1 do
309+ match p^.table[j] of
310+ | option(s2, letter2, long2, _v2, _help2):
311+ if (s2 = s) and ((letter2 = letter) or (LEN(long) > 0) and (long2 = long)) then
312+ badTable(p, i, "the option is in the scope already");
313+ end;
314+ | else:
315+ end;
316+ end;
317+ | else:
318+ end;
319+ end;
320+end;
321+
322+(* p reads the arguments from 1 on, by table, in the program's own
323+ scope; name begins the messages. *)
324+proc init*(p: ^Parser; name: []U8; table: []Row);
325+begin
326+ p^ := {name: name, table: table, at: 1};
327+ check(p);
328+ answerHelp(p);
329+end;
330+
331+(* The next option, in p.letter, p.value and p.long; false when the
332+ scope's options end. *)
333+proc next*(p: ^Parser): BOOL;
334+begin
335+ if read(p, false) = readEnd then return false; end;
336+ if p^.help then printHelp(p); end;
337+ return true;
338+end;
339+
340+(* The next operand, after what options are left; false when there are
341+ no more. *)
342+proc operand*(p: ^Parser): ([]U8, BOOL);
343+begin
344+ while next(p) do end;
345+ if p^.at >= ARGC() then return "", false; end;
346+ INC(p^.at);
347+ return Files.arg(p^.at - 1), true;
348+end;
349+
350+(* The options of the subcommand name, from the word after its own,
351+ which operand has given. *)
352+proc scope*(p: ^Parser; name: []U8);
353+begin
354+ p^.scope := name;
355+ p^.ended := false;
356+ p^.group := "";
357+ answerHelp(p);
358+end;
359+
360+(* The value of the option just read, as a whole number from lo to hi;
361+ anything else is a mistake. *)
362+proc number*(p: ^Parser; lo, hi: I64): I64;
363+var n: I64; ok: BOOL;
364+begin
365+ n, ok := Str.toInt(p^.value);
366+ if not ok or (n < lo) or (n > hi) then
367+ refuse(p, "{}{} wants a number from {} to {}, not '{}'",
368+ [Fmt.str(dashes(p)), Fmt.str(p^.spelt), Fmt.int(lo), Fmt.int(hi), Fmt.str(p^.value)]);
369+ end;
370+ return n;
371+end;
372+
373+(* The program's own mistake, written as Opt's are but alone, and
374+ status 2. *)
375+proc fail*(p: ^Parser; text: []U8);
376+var b: Buf.Buf;
377+begin
378+ Buf.init(ADR(b), Heap.heap);
379+ Fmt.write(ADR(b), "{}: {}\n", [Fmt.str(p^.name), Fmt.str(text)]);
380+ say(Sys.stderr, ADR(b));
381+ Rt.exit(2);
382+end;
383+
384+(* The module's test. main runs itself once for each case, with the
385+ case's words and OPTTEST in its environment, which makes it keep, the
386+ program of getopt's page, or a program with a bad table, or one with
387+ no synopsis lines; and holds each to what it writes, and its status. *)
388+proc keep;
389+const wait = 1;
390+var p: Parser; verbose, ok: BOOL; secs: I64; bar, job, w: []U8; b: Buf.Buf;
391+begin
392+ secs := 10;
393+ init(ADR(p), "keep", [
394+ usage("", "keep [-v] [--wait secs] start ..."),
395+ sub("", "start", "start a job"),
396+ option("", 'v', "verbose", "", "say more"),
397+ option("", wait, "wait", "secs", "give the job secs to start"),
398+ usage("start", "keep start [-b label] name command [argument ...]"),
399+ option("start", 'b', "bar", "label", "show the job in the bar as label")]);
400+ while next(ADR(p)) do
401+ if p.letter = 'v' then verbose := true;
402+ elsif p.letter = wait then secs := number(ADR(p), 1, 600);
403+ end;
404+ end;
405+ w, ok := operand(ADR(p));
406+ if not ok then fail(ADR(p), "no command; see keep -h"); end;
407+ if w # "start" then
408+ Buf.init(ADR(b), Heap.heap);
409+ Fmt.write(ADR(b), "no command {}; see keep -h", [Fmt.str(w)]);
410+ fail(ADR(p), Buf.bytes(ADR(b)));
411+ end;
412+ scope(ADR(p), "start");
413+ while next(ADR(p)) do
414+ if p.letter = 'b' then bar := p.value; end;
415+ end;
416+ job, ok := operand(ADR(p));
417+ w, ok := operand(ADR(p));
418+ if not ok then fail(ADR(p), "start needs a name and a command; see keep start -h"); end;
419+ if verbose then Fmt.print("waiting {}s\n", [Fmt.int(secs)]); end;
420+ Fmt.print("job {}", [Fmt.str(job)]);
421+ if LEN(bar) > 0 then Fmt.print(" [{}]", [Fmt.str(bar)]); end;
422+ Fmt.print(":", []);
423+ while ok do
424+ Fmt.print(" {}", [Fmt.str(w)]);
425+ w, ok := operand(ADR(p));
426+ end;
427+ Fmt.print("\n", []);
428+end;
429+
430+proc badTableTest;
431+var p: Parser;
432+begin
433+ init(ADR(p), "bad", [
434+ option("", 'v', "verbose", "", "say more"),
435+ option("", 'q', "quiet", "", "say less"),
436+ option("", 'v', "loud", "", "say it again")]);
437+end;
438+
439+(* Subcommands of subcommands, and no synopsis lines. *)
440+proc plain;
441+var p: Parser; _w: []U8; _ok: BOOL;
442+begin
443+ init(ADR(p), "plain", [
444+ sub("", "remote", "the remotes"),
445+ sub("remote", "add", "add one"),
446+ option("remote.add", 'f', "force", "", "even if it is there")]);
447+ _w, _ok := operand(ADR(p));
448+ scope(ADR(p), "remote");
449+ _w, _ok := operand(ADR(p));
450+ scope(ADR(p), "remote.add");
451+ while next(ADR(p)) do Fmt.print("{}\n", [Fmt.char(p.letter)]); end;
452+end;
453+
454+type Case = record
455+ mode: []U8; (* OPTTEST=... *)
456+ args: [][]U8;
457+ out, err: []U8;
458+ status: I64;
459+end;
460+
461+(* Everything left to read on fd, into buf: how much. *)
462+proc readAll(fd: I64; buf: []U8): I64;
463+var n, k: I64;
464+begin
465+ repeat
466+ k := Sys.read(fd, buf[n..LEN(buf)]);
467+ if k > 0 then INC(n, k); end;
468+ until k <= 0;
469+ Sys.close(fd);
470+ return n;
471+end;
472+
473+proc run(c: Case; n: I64);
474+var outR, outW, errR, errW, pid, status, no, ne: I64; st: Sys.Status; out, err: [4096]U8;
475+begin
476+ outR, outW := Sys.pipe();
477+ errR, errW := Sys.pipe();
478+ pid := Sys.spawn({path: "/proc/self/exe", args: c.args, env: [c.mode], fds: [0, outW, errW]});
479+ Sys.close(outW);
480+ Sys.close(errW);
481+ if pid < 0 then Rt.stop("Opt: can't run the test's cases"); end;
482+ pid, st, status := Sys.wait(pid, true);
483+ no := readAll(outR, out);
484+ ne := readAll(errR, err);
485+ if (st # Sys.exited) or (status # c.status) or (out[0..no] # c.out) or (err[0..ne] # c.err) then
486+ Fmt.print("case {}: status {}, out:\n{}err:\n{}", [Fmt.int(n), Fmt.int(status), Fmt.str(out[0..no]), Fmt.str(err[0..ne])]);
487+ Out.flush;
488+ Rt.stop("Opt: a case went wrong");
489+ end;
490+end;
491+
492+proc main*;
493+const
494+ keepHelp = "usage: keep [-v] [--wait secs] start ...\n\n start start a job\n -v, --verbose say more\n --wait secs give the job secs to start\n -h, --help print this and exit\n";
495+ startHelp = "usage: keep start [-b label] name command [argument ...]\n\n -b, --bar label show the job in the bar as label\n -h, --help print this and exit\n";
496+ k = "OPTTEST=keep";
497+var cases: []Case; i: I64; mode: []U8;
498+begin
499+ mode := Sys.env("OPTTEST");
500+ if mode = "keep" then keep; return;
501+ elsif mode = "bad" then badTableTest; return;
502+ elsif mode = "plain" then plain; return;
503+ end;
504+ cases := [
505+ {mode: k, args: ["keep", "-v", "--wait=30", "start", "-b", "web", "site", "python", "-m", "http.server", "--port", "8000"],
506+ out: "waiting 30s\njob site [web]: python -m http.server --port 8000\n"},
507+ {mode: k, args: ["keep", "--wait", "30", "-vv", "start", "--bar=web", "site", "x"], out: "waiting 30s\njob site [web]: x\n"},
508+ {mode: k, args: ["keep", "start", "-bweb", "n", "c"], out: "job n [web]: c\n"},
509+ {mode: k, args: ["keep", "start", "-b", "", "n", "c"], out: "job n: c\n"},
510+ {mode: k, args: ["keep", "start", "-b", "-x", "n", "c"], out: "job n [-x]: c\n"},
511+ {mode: k, args: ["keep", "start", "--", "-n", "ls", "-l"], out: "job -n: ls -l\n"},
512+ {mode: k, args: ["keep", "start", "-", "c", "--", "-h"], out: "job -: c -- -h\n"},
513+ {mode: k, args: ["keep", "start", "n", "c", "-b", "x"], out: "job n: c -b x\n"},
514+ {mode: k, args: ["keep", "start", "-bh", "n", "c"], out: "job n [h]: c\n"},
515+ {mode: k, args: ["keep", "-h"], out: keepHelp},
516+ {mode: k, args: ["keep", "-v", "--help", "-x"], out: keepHelp},
517+ {mode: k, args: ["keep", "-vh"], out: keepHelp},
518+ {mode: k, args: ["keep", "start", "-x", "-h"], out: startHelp},
519+ {mode: k, args: ["keep"], err: "keep: no command; see keep -h\n", status: 2},
520+ {mode: k, args: ["keep", "stop"], err: "keep: no command stop; see keep -h\n", status: 2},
521+ {mode: k, args: ["keep", "start", "-x"], err: "keep: unknown option -x\nusage: keep start [-b label] name command [argument ...]\n", status: 2},
522+ {mode: k, args: ["keep", "--verb", "start"], err: "keep: unknown option --verb\nusage: keep [-v] [--wait secs] start ...\n", status: 2},
523+ {mode: k, args: ["keep", "--bar=x", "start"], err: "keep: unknown option --bar\nusage: keep [-v] [--wait secs] start ...\n", status: 2},
524+ {mode: k, args: ["keep", "--verbose=x"], err: "keep: --verbose takes no value\nusage: keep [-v] [--wait secs] start ...\n", status: 2},
525+ {mode: k, args: ["keep", "start", "-b"], err: "keep: -b needs a label\nusage: keep start [-b label] name command [argument ...]\n", status: 2},
526+ {mode: k, args: ["keep", "--wait"], err: "keep: --wait needs a secs\nusage: keep [-v] [--wait secs] start ...\n", status: 2},
527+ {mode: k, args: ["keep", "--wait", "0", "start", "a", "b"],
528+ err: "keep: --wait wants a number from 1 to 600, not '0'\nusage: keep [-v] [--wait secs] start ...\n", status: 2},
529+ {mode: k, args: ["keep", "--wait=9x", "start"], err: "keep: --wait wants a number from 1 to 600, not '9x'\nusage: keep [-v] [--wait secs] start ...\n", status: 2},
530+ {mode: "OPTTEST=bad", args: ["bad", "-v"], err: "bad: option table, row 2: the option is in the scope already\n", status: 1},
531+ {mode: "OPTTEST=plain", args: ["plain", "remote", "add", "-f", "--force"], out: "f\nf\n"},
532+ {mode: "OPTTEST=plain", args: ["plain", "remote", "add", "-x"], err: "plain: unknown option -x\nusage: plain remote add\n", status: 2},
533+ {mode: "OPTTEST=plain", args: ["plain", "remote", "-h"], out: "usage: plain remote\n\n add add one\n -h, --help print this and exit\n"}];
534+ for i := 0 to LEN(cases) - 1 do run(cases[i], i); end;
535+end;
536+
537+end Opt.

test/check.sh (history) modified +1 -1

@@ -158,7 +158,7 @@
158158 else pass=$((pass + 1)); fi
159159 }
160160 if ./raz -Werror -o $out/Mutate tools/Mutate.raz 2>$out/Mutate.cerr; then
161- for f in examples/*.raz test/run/*.raz; do
161+ for f in examples/*.raz test/run/*.raz lib/Opt.raz; do
162162 $raz -I $(dirname $f)/mods -o $out/mutant $f 2>/dev/null || continue
163163 for seed in ${FUZZ_SEEDS:-1 2 3}; do fuzz $f $seed; done
164164 done

test/doc/Opt.expect (history) added +58 -0

@@ -0,0 +1,58 @@
1+module Opt;
2+ A command line read through one table, by the rules of the C
3+ library's getopt with GNU's long options (report, section 11). The
4+ program describes its synopsis lines, subcommands and options once,
5+ in a slice of Row; the help and the messages for mistakes are made
6+ from it, so they can't drift from it.
7+
8+ Depends on Files for the words, and writes the help and the
9+ messages itself, then ends the program.
10+ Leaves out: options after operands (GNU's reordering), a program
11+ that keeps -h for itself, and completion for concha.
12+
13+type Row* = variant
14+ usage(scope, line: []U8); (* a synopsis line, without "usage: " *)
15+ sub(scope, name, help: []U8); (* a subcommand *)
16+ option(scope: []U8; letter: U8; long, value, help: []U8);
17+end;
18+
19+type Parser* = record
20+ letter*: U8; (* the option just read *)
21+ value*: []U8; (* its value; "" for a flag *)
22+ long*: BOOL; (* it was written by its long name *)
23+ (* the rest are Opt's own *)
24+ name: []U8; (* the program's, as the messages begin *)
25+ table: []Row;
26+ scope: []U8;
27+ at: I64; (* the next word *)
28+ group: []U8; (* the letters left of a word of them, as -lw *)
29+ spelt: []U8; (* the option just read as written, without its dashes *)
30+ ended: BOOL; (* the scope's options have ended *)
31+ help: BOOL; (* the option just read is -h or --help *)
32+end;
33+
34+proc init*(p: ^Parser; name: []U8; table: []Row);
35+ p reads the arguments from 1 on, by table, in the program's own
36+ scope; name begins the messages.
37+
38+proc next*(p: ^Parser): BOOL;
39+ The next option, in p.letter, p.value and p.long; false when the
40+ scope's options end.
41+
42+proc operand*(p: ^Parser): ([]U8, BOOL);
43+ The next operand, after what options are left; false when there are
44+ no more.
45+
46+proc scope*(p: ^Parser; name: []U8);
47+ The options of the subcommand name, from the word after its own,
48+ which operand has given.
49+
50+proc number*(p: ^Parser; lo, hi: I64): I64;
51+ The value of the option just read, as a whole number from lo to hi;
52+ anything else is a mistake.
53+
54+proc fail*(p: ^Parser; text: []U8);
55+ The program's own mistake, written as Opt's are but alone, and
56+ status 2.
57+
58+proc main*;

tools/Files.raz deleted +0 -46

@@ -1,46 +0,0 @@
1-(* Files: what the tools need of the command line and of files: an
2- argument as a string, and a whole file read or written. *)
3-module Files;
4-import Sys, Buf;
5-
6-(* Argument i, a C string, as a slice. *)
7-proc arg*(i: I64): []U8;
8-var s: ^U8; n: I64;
9-begin
10- s := ARGV()[i];
11- n := 0;
12- while s[n] # 0 do INC(n); end;
13- return s[0..n];
14-end;
15-
16-(* Adds the file at path to b; false if it can't be read. *)
17-proc read*(path: []U8; b: ^Buf.Buf): BOOL;
18-var fd, n: I64; chunk: [4096]U8;
19-begin
20- fd := Sys.openRead(path);
21- if fd < 0 then return false; end;
22- repeat
23- n := Sys.read(fd, chunk);
24- if n > 0 then Buf.str(b, chunk[0..n]); end;
25- until n <= 0;
26- Sys.close(fd);
27- return n = 0;
28-end;
29-
30-(* Makes the file at path hold data; false if it can't be written. *)
31-proc write*(path, data: []U8): BOOL;
32-var fd, n, at: I64;
33-begin
34- fd := Sys.create(path);
35- if fd < 0 then return false; end;
36- at := 0;
37- while at < LEN(data) do
38- n := Sys.write(fd, data[at..LEN(data)]);
39- if n <= 0 then Sys.close(fd); return false; end;
40- at := at + n;
41- end;
42- Sys.close(fd);
43- return true;
44-end;
45-
46-end Files.