raz / commit ccd59f55

commit
ccd59f55210654fa660b99212a3d63f345bd176d
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
0ad25c63
Opt takes the parser by var, or const where it only reads it

Opt.init, next, operand, at and scope move the parser and take var
p: Parser; number, refuse and fail only read it and take const, as do
the private procedures that only read it. Sh's option table, and Opt's
own test of keep, are now constants of Opt.Row, a variant (report
2026-10.33, section 4); badTableTest and plain keep theirs in the call,
which stays allowed. Volume 2 and test/doc say the new signatures.

patch · log

5 files changed, +168 -162

doc/raz-lib.md (history) modified +20 -19

@@ -839,14 +839,14 @@
839839 spelt*: []U8; (* as it was written, no dashes: "b" or "bar"; to read only *)
840840 ...
841841 end;
842-proc init*(p: ^Parser; name: []U8; table: []Row); (* the arguments from 1 on, by table *)
843-proc next*(p: ^Parser): BOOL; (* the next option; false when they end *)
844-proc operand*(p: ^Parser): ([]U8, BOOL); (* the next operand; false when there are no more *)
845-proc at*(p: ^Parser): INT; (* the argument operand would give next *)
846-proc scope*(p: ^Parser; name: []U8); (* subcommand name's options, from the word after it *)
847-proc number*(p: ^Parser; lo, hi: I64): I64; (* the value just read, a number from lo to hi *)
848-proc refuse*(p: ^Parser; wants: []U8); (* the value just read refused, as number refuses one *)
849-proc fail*(p: ^Parser; text: []U8); (* the program's own mistake *)
842+proc init*(var p: Parser; name: []U8; table: []Row); (* the arguments from 1 on, by table *)
843+proc next*(var p: Parser): BOOL; (* the next option; false when they end *)
844+proc operand*(var p: Parser): ([]U8, BOOL); (* the next operand; false when there are no more *)
845+proc at*(var p: Parser): INT; (* the argument operand would give next *)
846+proc scope*(var p: Parser; name: []U8); (* subcommand name's options, from the word after it *)
847+proc number*(const p: Parser; lo, hi: I64): I64; (* the value just read, a number from lo to hi *)
848+proc refuse*(const p: Parser; wants: []U8); (* the value just read refused, as number refuses one *)
849+proc fail*(const p: Parser; text: []U8); (* the program's own mistake *)
850850 ```
851851
852852 `Opt` reads a command line by the rules of the C library's `getopt`,
@@ -857,7 +857,8 @@
857857 drift from it; the program's test of `p.letter` is the only other
858858 place an option appears. The parser is a record the program owns, and
859859 the table a slice it keeps as long as the parser: a literal written in
860-the call to `init` lives as long as the procedure that makes it.
860+the call to `init` lives as long as the procedure that makes it, and a
861+constant, `const Rows: []Opt.Row = [...]`, as long as the program.
861862
862863 A row's `scope` is `""` for the program itself, or a subcommand's
863864 name: `"start"`, or `"remote.add"` for a subcommand of `remote`. An
@@ -873,25 +874,25 @@
873874 ```
874875 const wait = 1; (* the key of --wait, which has no letter *)
875876 ...
876-Opt.init(ADR(p), "keep", [
877+Opt.init(p, "keep", [
877878 Opt.usage("", "keep [-v] [--wait secs] start ..."),
878879 Opt.sub("", "start", "start a job"),
879880 Opt.option("", 'v', "verbose", "", "say more"),
880881 Opt.option("", wait, "wait", "secs", "give the job secs to start"),
881882 Opt.usage("start", "keep start [-b label] name command [argument ...]"),
882883 Opt.option("start", 'b', "bar", "label", "show the job in the bar as label")]);
883-while Opt.next(ADR(p)) do
884+while Opt.next(p) do
884885 if p.letter = 'v' then verbose := true;
885- elsif p.letter = wait then secs := Opt.number(ADR(p), 1, 600);
886+ elsif p.letter = wait then secs := Opt.number(p, 1, 600);
886887 end;
887888 end;
888-w, ok := Opt.operand(ADR(p));
889-if not ok or (w # "start") then Opt.fail(ADR(p), "no command; see keep -h"); end;
890-Opt.scope(ADR(p), "start");
891-while Opt.next(ADR(p)) do
889+w, ok := Opt.operand(p);
890+if not ok or (w # "start") then Opt.fail(p, "no command; see keep -h"); end;
891+Opt.scope(p, "start");
892+while Opt.next(p) do
892893 if p.letter = 'b' then bar := p.value; end;
893894 end;
894-job, ok := Opt.operand(ADR(p));
895+job, ok := Opt.operand(p);
895896 ```
896897
897898 The rules are `getopt`'s:
@@ -916,8 +917,8 @@
916917 that an interpreter hands a script its arguments as they are:
917918
918919 ```
919-script, ok := Opt.operand(ADR(p)); (* tery file arg ... *)
920-first := Opt.at(ADR(p)); (* file's own arguments, from here on *)
920+script, ok := Opt.operand(p); (* tery file arg ... *)
921+first := Opt.at(p); (* file's own arguments, from here on *)
921922 ```
922923
923924 `-h` or `--help` among a scope's options prints its help on standard

examples/Sh.raz (history) modified +8 -5

@@ -358,17 +358,20 @@
358358 if interactive then show("\n", []); end;
359359 end;
360360
361+(* The options, a constant table of Opt's rows *)
362+const Options: []Opt.Row = [
363+ Opt.usage("", "Sh [-c script | file]"),
364+ Opt.option("", 'c', "command", "script", "run script, not standard input")];
365+
361366 proc main*: INT;
362367 var p: Opt.Parser; script, file: []U8; given, found: BOOL; b: Buf.Buf;
363368 begin
364369 if not Arena.init(arena, 1 shl 24) then Rt.stop("Sh: no memory"); end;
365- Opt.init(ADR(p), "Sh", [
366- Opt.usage("", "Sh [-c script | file]"),
367- Opt.option("", 'c', "command", "script", "run script, not standard input")]);
368- while Opt.next(ADR(p)) do
370+ Opt.init(p, "Sh", Options);
371+ while Opt.next(p) do
369372 if p.letter = 'c' then script := p.value; given := true; end;
370373 end;
371- file, found := Opt.operand(ADR(p));
374+ file, found := Opt.operand(p);
372375 if given then
373376 run(script);
374377 elsif found then

lib/Opt.raz (history) modified +126 -124

@@ -50,25 +50,25 @@
5050 Buf.done(b);
5151 end;
5252
53-proc inScope(p: ^Parser; r: Row): BOOL;
53+proc inScope(const p: Parser; r: Row): BOOL;
5454 begin
5555 match r of
56- | usage(s, _line): return s = p^.scope;
57- | sub(s, _name, _help): return s = p^.scope;
58- | option(s, _l, _long, _v, _help): return s = p^.scope;
56+ | usage(s, _line): return s = p.scope;
57+ | sub(s, _name, _help): return s = p.scope;
58+ | option(s, _l, _long, _v, _help): return s = p.scope;
5959 end;
6060 end;
6161
6262 (* The scope's synopsis lines, as the help and the mistakes begin:
6363 "usage: " and the first, the others under it; or, for a scope with
6464 none, "usage: keep start". *)
65-proc usageOf(p: ^Parser; var b: Buf.Buf);
65+proc usageOf(const p: Parser; var b: Buf.Buf);
6666 var i, lines: INT; c: U8;
6767 begin
68- for i := 0 to LEN(p^.table) - 1 do
69- match p^.table[i] of
68+ for i := 0 to LEN(p.table) - 1 do
69+ match p.table[i] of
7070 | usage(s, line):
71- if s = p^.scope then
71+ if s = p.scope then
7272 if lines = 0 then Buf.str(b, "usage: "); else Buf.str(b, " "); end;
7373 Fmt.write(b, "{}\n", [Fmt.str(line)]);
7474 INC(lines);
@@ -77,10 +77,10 @@
7777 end;
7878 end;
7979 if lines = 0 then
80- Fmt.write(b, "usage: {}", [Fmt.str(p^.name)]);
81- if LEN(p^.scope) > 0 then Buf.add(b, ' '); end;
82- for i := 0 to LEN(p^.scope) - 1 do
83- c := p^.scope[i];
80+ Fmt.write(b, "usage: {}", [Fmt.str(p.name)]);
81+ if LEN(p.scope) > 0 then Buf.add(b, ' '); end;
82+ for i := 0 to LEN(p.scope) - 1 do
83+ c := p.scope[i];
8484 if c = '.' then c := ' '; end;
8585 Buf.add(b, c);
8686 end;
@@ -90,11 +90,11 @@
9090
9191 (* A mistake on the command line: the sentence f, the scope's synopsis
9292 lines under it, and status 2. *)
93-proc mistake(p: ^Parser; f: []U8; args: []Fmt.Arg);
93+proc mistake(const p: Parser; f: []U8; args: []Fmt.Arg);
9494 var b: Buf.Buf;
9595 begin
9696 Buf.init(b, Heap.heap);
97- Fmt.write(b, "{}: ", [Fmt.str(p^.name)]);
97+ Fmt.write(b, "{}: ", [Fmt.str(p.name)]);
9898 Fmt.write(b, f, args);
9999 Buf.add(b, 10);
100100 usageOf(p, b);
@@ -102,9 +102,9 @@
102102 Rt.exit(2);
103103 end;
104104
105-proc dashes(p: ^Parser): []U8;
105+proc dashes(const p: Parser): []U8;
106106 begin
107- if p^.long then return "--"; end;
107+ if p.long then return "--"; end;
108108 return "-";
109109 end;
110110
@@ -164,13 +164,13 @@
164164
165165 (* The scope's option with the letter c, or with the long name long:
166166 its row, or -1, and its letter and the name of its value. *)
167-proc find(p: ^Parser; c: U8; long: []U8): (INT, U8, []U8);
167+proc find(const p: Parser; c: U8; long: []U8): (INT, U8, []U8);
168168 var i: INT;
169169 begin
170- for i := 0 to LEN(p^.table) - 1 do
171- match p^.table[i] of
170+ for i := 0 to LEN(p.table) - 1 do
171+ match p.table[i] of
172172 | option(s, letter, lg, value, _help):
173- if (s = p^.scope) and ((c >= '!') and (letter = c) or (LEN(long) > 0) and (lg = long)) then
173+ if (s = p.scope) and ((c >= '!') and (letter = c) or (LEN(long) > 0) and (lg = long)) then
174174 return i, letter, value;
175175 end;
176176 | else:
@@ -181,7 +181,7 @@
181181
182182 (* Whether the scope has an option -h of its own, so that help is
183183 --help alone there, as it is for grep, whose -h is "no file names". *)
184-proc ownsH(p: ^Parser): BOOL;
184+proc ownsH(const p: Parser): BOOL;
185185 var r: INT; _l: U8; _v: []U8;
186186 begin
187187 r, _l, _v := find(p, 'h', "");
@@ -192,31 +192,31 @@
192192 options and the help itself, each with its line; and status 0. The
193193 help's own row is made as the table's are, with no letter if the
194194 scope has -h. *)
195-proc printHelp(p: ^Parser);
195+proc printHelp(const p: Parser);
196196 var b: Buf.Buf; i, width: INT; r: ^Row; own: Row; h: U8;
197197 begin
198198 h := 'h';
199199 if ownsH(p) then h := 0; end;
200- own := option(p^.scope, h, "help", "", "print this and exit");
200+ own := option(p.scope, h, "help", "", "print this and exit");
201201 Buf.init(b, Heap.heap);
202202 usageOf(p, b);
203203 Buf.add(b, 10);
204204 width := leftWidth(ADR(own));
205- for i := 0 to LEN(p^.table) - 1 do
206- r := ADR(p^.table[i]);
205+ for i := 0 to LEN(p.table) - 1 do
206+ r := ADR(p.table[i]);
207207 if inScope(p, r^) and (leftWidth(r) > width) then width := leftWidth(r); end;
208208 end;
209- for i := 0 to LEN(p^.table) - 1 do
210- r := ADR(p^.table[i]);
209+ for i := 0 to LEN(p.table) - 1 do
210+ r := ADR(p.table[i]);
211211 match r^ of
212- | sub(s, _name, _help): if s = p^.scope then item(b, r, width); end;
212+ | sub(s, _name, _help): if s = p.scope then item(b, r, width); end;
213213 | else:
214214 end;
215215 end;
216- for i := 0 to LEN(p^.table) - 1 do
217- r := ADR(p^.table[i]);
216+ for i := 0 to LEN(p.table) - 1 do
217+ r := ADR(p.table[i]);
218218 match r^ of
219- | option(s, _l, _long, _v, _help): if s = p^.scope then item(b, r, width); end;
219+ | option(s, _l, _long, _v, _help): if s = p.scope then item(b, r, width); end;
220220 | else:
221221 end;
222222 end;
@@ -228,71 +228,71 @@
228228 (* The next option, a word or a letter of one, into p: read1, or
229229 readEnd at the end of the options. A mistake is refused, or, if
230230 quiet, passed over, as readSkip. *)
231-proc read(p: ^Parser; quiet: BOOL): INT;
231+proc read(var p: Parser; quiet: BOOL): INT;
232232 var w, val: []U8; r, eq: INT; c: U8;
233233 begin
234- p^.help := false;
235- p^.value := "";
236- if LEN(p^.group) = 0 then
237- if p^.ended or (p^.at >= ARGC()) then p^.ended := true; return readEnd; end;
238- w := Files.arg(p^.at);
239- if (LEN(w) < 2) or (w[0] # '-') then p^.ended := true; return readEnd; end; (* an operand *)
240- INC(p^.at);
241- if w = "--" then p^.ended := true; return readEnd; end;
234+ p.help := false;
235+ p.value := "";
236+ if LEN(p.group) = 0 then
237+ if p.ended or (p.at >= ARGC()) then p.ended := true; return readEnd; end;
238+ w := Files.arg(p.at);
239+ if (LEN(w) < 2) or (w[0] # '-') then p.ended := true; return readEnd; end; (* an operand *)
240+ INC(p.at);
241+ if w = "--" then p.ended := true; return readEnd; end;
242242 if w[1] # '-' then
243- p^.group := w[1..LEN(w)];
243+ p.group := w[1..LEN(w)];
244244 else
245245 eq := Str.find(w, "=");
246- if eq < 0 then p^.spelt := w[2..LEN(w)]; else p^.spelt := w[2..eq]; end;
247- p^.long := true;
248- if p^.spelt = "help" then p^.help := true; return read1; end;
249- r, p^.letter, val := find(p, 0, p^.spelt);
246+ if eq < 0 then p.spelt := w[2..LEN(w)]; else p.spelt := w[2..eq]; end;
247+ p.long := true;
248+ if p.spelt = "help" then p.help := true; return read1; end;
249+ r, p.letter, val := find(p, 0, p.spelt);
250250 if r < 0 then
251251 if quiet then return readSkip; end;
252- mistake(p, "unknown option --{}", [Fmt.str(p^.spelt)]);
252+ mistake(p, "unknown option --{}", [Fmt.str(p.spelt)]);
253253 end;
254254 if (eq >= 0) and (LEN(val) = 0) then
255255 if quiet then return readSkip; end;
256- mistake(p, "--{} takes no value", [Fmt.str(p^.spelt)]);
256+ mistake(p, "--{} takes no value", [Fmt.str(p.spelt)]);
257257 elsif eq >= 0 then
258- p^.value := w[eq + 1..LEN(w)];
258+ p.value := w[eq + 1..LEN(w)];
259259 elsif optional(val) then
260260 (* left out: the next word is not its value *)
261261 elsif LEN(val) > 0 then
262- if p^.at >= ARGC() then
262+ if p.at >= ARGC() then
263263 if quiet then return readEnd; end;
264- mistake(p, "--{} needs a {}", [Fmt.str(p^.spelt), Fmt.str(val)]);
264+ mistake(p, "--{} needs a {}", [Fmt.str(p.spelt), Fmt.str(val)]);
265265 end;
266- p^.value := Files.arg(p^.at);
267- INC(p^.at);
266+ p.value := Files.arg(p.at);
267+ INC(p.at);
268268 end;
269269 return read1;
270270 end;
271271 end;
272272 (* a letter of a group *)
273- c := p^.group[0];
274- p^.spelt := p^.group[0..1];
275- p^.group := p^.group[1..LEN(p^.group)];
276- p^.long := false;
277- p^.letter := c;
278- r, p^.letter, val := find(p, c, "");
279- if (c = 'h') and (r < 0) then p^.help := true; return read1; end;
273+ c := p.group[0];
274+ p.spelt := p.group[0..1];
275+ p.group := p.group[1..LEN(p.group)];
276+ p.long := false;
277+ p.letter := c;
278+ r, p.letter, val := find(p, c, "");
279+ if (c = 'h') and (r < 0) then p.help := true; return read1; end;
280280 if r < 0 then
281281 if quiet then return readSkip; end;
282- mistake(p, "unknown option -{}", [Fmt.str(p^.spelt)]);
282+ mistake(p, "unknown option -{}", [Fmt.str(p.spelt)]);
283283 end;
284284 if LEN(val) > 0 then
285- if LEN(p^.group) > 0 then
286- p^.value := p^.group;
287- p^.group := "";
285+ if LEN(p.group) > 0 then
286+ p.value := p.group;
287+ p.group := "";
288288 elsif optional(val) then
289289 (* left out *)
290- elsif p^.at < ARGC() then
291- p^.value := Files.arg(p^.at);
292- INC(p^.at);
290+ elsif p.at < ARGC() then
291+ p.value := Files.arg(p.at);
292+ INC(p.at);
293293 else
294294 if quiet then return readEnd; end;
295- mistake(p, "-{} needs a {}", [Fmt.str(p^.spelt), Fmt.str(val)]);
295+ mistake(p, "-{} needs a {}", [Fmt.str(p.spelt), Fmt.str(val)]);
296296 end;
297297 end;
298298 return read1;
@@ -301,31 +301,31 @@
301301 (* -h or --help among the scope's options is answered before the
302302 program sees one: a copy of the parser reads them, passing over
303303 mistakes. *)
304-proc answerHelp(p: ^Parser);
304+proc answerHelp(const p: Parser);
305305 var q: Parser; k: INT;
306306 begin
307- q := p^;
307+ q := p;
308308 repeat
309- k := read(ADR(q), true);
309+ k := read(q, true);
310310 if q.help and (k = read1) then printHelp(p); end;
311311 until k = readEnd;
312312 end;
313313
314314 (* A mistake in the table: the program's, with status 1. *)
315-proc badTable(p: ^Parser; row: INT; why: []U8);
315+proc badTable(const p: Parser; row: INT; why: []U8);
316316 var b: Buf.Buf;
317317 begin
318318 Buf.init(b, Heap.heap);
319- Fmt.write(b, "{}: option table, row {}: {}\n", [Fmt.str(p^.name), Fmt.int(row), Fmt.str(why)]);
319+ Fmt.write(b, "{}: option table, row {}: {}\n", [Fmt.str(p.name), Fmt.int(row), Fmt.str(why)]);
320320 say(Sys.stderr, b);
321321 Rt.exit(1);
322322 end;
323323
324-proc check(p: ^Parser);
324+proc check(const p: Parser);
325325 var i, j: INT;
326326 begin
327- for i := 0 to LEN(p^.table) - 1 do
328- match p^.table[i] of
327+ for i := 0 to LEN(p.table) - 1 do
328+ match p.table[i] of
329329 | sub(_s, name, _help):
330330 if LEN(name) = 0 then badTable(p, i, "a subcommand needs a name"); end;
331331 | option(s, letter, long, value, _help):
@@ -339,7 +339,7 @@
339339 end;
340340 if long = "help" then badTable(p, i, "--help is Opt's"); end;
341341 for j := 0 to i - 1 do
342- match p^.table[j] of
342+ match p.table[j] of
343343 | option(s2, letter2, long2, _v2, _help2):
344344 if (s2 = s) and ((letter2 = letter) or (LEN(long) > 0) and (long2 = long)) then
345345 badTable(p, i, "the option is in the scope already");
@@ -354,65 +354,65 @@
354354
355355 (* p reads the arguments from 1 on, by table, in the program's own
356356 scope; name begins the messages. *)
357-proc init*(p: ^Parser; name: []U8; table: []Row);
357+proc init*(var p: Parser; name: []U8; table: []Row);
358358 begin
359- p^ := {name: name, table: table, at: 1};
359+ p := {name: name, table: table, at: 1};
360360 check(p);
361361 answerHelp(p);
362362 end;
363363
364364 (* The next option, in p.letter, p.value and p.long; false when the
365365 scope's options end. *)
366-proc next*(p: ^Parser): BOOL;
366+proc next*(var p: Parser): BOOL;
367367 begin
368368 if read(p, false) = readEnd then return false; end;
369- if p^.help then printHelp(p); end;
369+ if p.help then printHelp(p); end;
370370 return true;
371371 end;
372372
373373 (* The next operand, after what options are left; false when there are
374374 no more. *)
375-proc operand*(p: ^Parser): ([]U8, BOOL);
375+proc operand*(var p: Parser): ([]U8, BOOL);
376376 begin
377377 while next(p) do end;
378- if p^.at >= ARGC() then return "", false; end;
379- INC(p^.at);
380- return Files.arg(p^.at - 1), true;
378+ if p.at >= ARGC() then return "", false; end;
379+ INC(p.at);
380+ return Files.arg(p.at - 1), true;
381381 end;
382382
383383 (* The number of the argument operand would give next, after what
384384 options are left: the operands from there on are Files.arg(i), for
385385 i up to ARGC() - 1, as an interpreter hands them to its script. *)
386-proc at*(p: ^Parser): INT;
386+proc at*(var p: Parser): INT;
387387 begin
388388 while next(p) do end;
389- return p^.at;
389+ return p.at;
390390 end;
391391
392392 (* The options of the subcommand name, from the word after its own,
393393 which operand has given. *)
394-proc scope*(p: ^Parser; name: []U8);
394+proc scope*(var p: Parser; name: []U8);
395395 begin
396- p^.scope := name;
397- p^.ended := false;
398- p^.group := "";
396+ p.scope := name;
397+ p.ended := false;
398+ p.group := "";
399399 answerHelp(p);
400400 end;
401401
402402 (* The value of the option just read, as a whole number from lo to hi;
403403 anything else is a mistake. A number with no top is given the
404404 largest I64 for hi, and the mistake says "from lo up". *)
405-proc number*(p: ^Parser; lo, hi: I64): I64;
405+proc number*(const p: Parser; lo, hi: I64): I64;
406406 var n: I64; ok: BOOL;
407407 begin
408- n, ok := Str.toInt(p^.value);
408+ n, ok := Str.toInt(p.value);
409409 if not ok or (n < lo) or (n > hi) then
410410 if hi = largest then
411411 mistake(p, "{}{} wants a number from {} up, not '{}'",
412- [Fmt.str(dashes(p)), Fmt.str(p^.spelt), Fmt.int(lo), Fmt.str(p^.value)]);
412+ [Fmt.str(dashes(p)), Fmt.str(p.spelt), Fmt.int(lo), Fmt.str(p.value)]);
413413 end;
414414 mistake(p, "{}{} wants a number from {} to {}, not '{}'",
415- [Fmt.str(dashes(p)), Fmt.str(p^.spelt), Fmt.int(lo), Fmt.int(hi), Fmt.str(p^.value)]);
415+ [Fmt.str(dashes(p)), Fmt.str(p.spelt), Fmt.int(lo), Fmt.int(hi), Fmt.str(p.value)]);
416416 end;
417417 return n;
418418 end;
@@ -420,19 +420,19 @@
420420 (* The value of the option just read refused, as number refuses one:
421421 "-b wants wants, not 'value'", the option as it was written, with
422422 the scope's synopsis lines, and status 2. *)
423-proc refuse*(p: ^Parser; wants: []U8);
423+proc refuse*(const p: Parser; wants: []U8);
424424 begin
425425 mistake(p, "{}{} wants {}, not '{}'",
426- [Fmt.str(dashes(p)), Fmt.str(p^.spelt), Fmt.str(wants), Fmt.str(p^.value)]);
426+ [Fmt.str(dashes(p)), Fmt.str(p.spelt), Fmt.str(wants), Fmt.str(p.value)]);
427427 end;
428428
429429 (* The program's own mistake, written as Opt's are but alone, and
430430 status 2. *)
431-proc fail*(p: ^Parser; text: []U8);
431+proc fail*(const p: Parser; text: []U8);
432432 var b: Buf.Buf;
433433 begin
434434 Buf.init(b, Heap.heap);
435- Fmt.write(b, "{}: {}\n", [Fmt.str(p^.name), Fmt.str(text)]);
435+ Fmt.write(b, "{}: {}\n", [Fmt.str(p.name), Fmt.str(text)]);
436436 say(Sys.stderr, b);
437437 Rt.exit(2);
438438 end;
@@ -442,54 +442,56 @@
442442 program of getopt's page, or a program with a bad table, or one with
443443 no synopsis lines; and holds each to what it writes, and its status. *)
444444 proc keep;
445-const wait = 1; jobs = 2;
446-var p: Parser; verbose, ok: BOOL; secs: I64; bar, job, w: []U8; b: Buf.Buf; i: INT;
447-begin
448- secs := 10;
449- init(ADR(p), "keep", [
445+const
446+ wait = 1; jobs = 2;
447+ Rows: []Row = [ (* a constant table; badTableTest and plain write theirs in the call *)
450448 usage("", "keep [-v] [--wait secs] start ..."),
451449 sub("", "start", "start a job"),
452450 option("", 'v', "verbose", "", "say more"),
453451 option("", wait, "wait", "secs", "give the job secs to start"),
454452 option("", jobs, "jobs", "n", "keep n jobs at most"),
455453 usage("start", "keep start [-b label] name command [argument ...]"),
456- option("start", 'b', "bar", "label", "show the job in the bar as label")]);
457- while next(ADR(p)) do
454+ option("start", 'b', "bar", "label", "show the job in the bar as label")];
455+var p: Parser; verbose, ok: BOOL; secs: I64; bar, job, w: []U8; b: Buf.Buf; i: INT;
456+begin
457+ secs := 10;
458+ init(p, "keep", Rows);
459+ while next(p) do
458460 if p.letter = 'v' then verbose := true;
459- elsif p.letter = wait then secs := number(ADR(p), 1, 600);
460- elsif p.letter = jobs then _ := number(ADR(p), 1, largest);
461+ elsif p.letter = wait then secs := number(p, 1, 600);
462+ elsif p.letter = jobs then _ := number(p, 1, largest);
461463 end;
462464 end;
463- w, ok := operand(ADR(p));
464- if not ok then fail(ADR(p), "no command; see keep -h"); end;
465+ w, ok := operand(p);
466+ if not ok then fail(p, "no command; see keep -h"); end;
465467 if w # "start" then
466468 Buf.init(b, Heap.heap);
467469 Fmt.write(b, "no command {}; see keep -h", [Fmt.str(w)]);
468- fail(ADR(p), Buf.bytes(b));
470+ fail(p, Buf.bytes(b));
469471 end;
470- scope(ADR(p), "start");
471- while next(ADR(p)) do
472- if (p.letter = 'b') and (Str.find(p.value, " ") >= 0) then refuse(ADR(p), "a label with no spaces");
472+ scope(p, "start");
473+ while next(p) do
474+ if (p.letter = 'b') and (Str.find(p.value, " ") >= 0) then refuse(p, "a label with no spaces");
473475 elsif p.letter = 'b' then bar := p.value;
474476 end;
475477 end;
476- job, ok := operand(ADR(p));
478+ job, ok := operand(p);
477479 (* the command and its arguments, as they are, by at *)
478- if not ok or (at(ADR(p)) >= ARGC()) then
479- fail(ADR(p), "start needs a name and a command; see keep start -h");
480+ if not ok or (at(p) >= ARGC()) then
481+ fail(p, "start needs a name and a command; see keep start -h");
480482 end;
481483 if verbose then Fmt.print("waiting {}s\n", [Fmt.int(secs)]); end;
482484 Fmt.print("job {}", [Fmt.str(job)]);
483485 if LEN(bar) > 0 then Fmt.print(" [{}]", [Fmt.str(bar)]); end;
484486 Fmt.print(":", []);
485- for i := at(ADR(p)) to ARGC() - 1 do Fmt.print(" {}", [Fmt.str(Files.arg(i))]); end;
487+ for i := at(p) to ARGC() - 1 do Fmt.print(" {}", [Fmt.str(Files.arg(i))]); end;
486488 Fmt.print("\n", []);
487489 end;
488490
489491 proc badTableTest;
490492 var p: Parser;
491493 begin
492- init(ADR(p), "bad", [
494+ init(p, "bad", [
493495 option("", 'v', "verbose", "", "say more"),
494496 option("", 'q', "quiet", "", "say less"),
495497 option("", 'v', "loud", "", "say it again")]);
@@ -500,17 +502,17 @@
500502 proc plain;
501503 var p: Parser; _w: []U8; _ok: BOOL;
502504 begin
503- init(ADR(p), "plain", [
505+ init(p, "plain", [
504506 sub("", "remote", "the remotes"),
505507 sub("remote", "add", "add one"),
506508 option("remote.add", 'f', "force", "", "even if it is there"),
507509 option("remote.add", 'h', "here", "", "only here"),
508510 option("remote.add", 'c', "color", "[when]", "colour it: always, never or auto")]);
509- _w, _ok := operand(ADR(p));
510- scope(ADR(p), "remote");
511- _w, _ok := operand(ADR(p));
512- scope(ADR(p), "remote.add");
513- while next(ADR(p)) do Fmt.print("{} {}={}\n", [Fmt.char(p.letter), Fmt.str(p.spelt), Fmt.str(p.value)]); end;
511+ _w, _ok := operand(p);
512+ scope(p, "remote");
513+ _w, _ok := operand(p);
514+ scope(p, "remote.add");
515+ while next(p) do Fmt.print("{} {}={}\n", [Fmt.char(p.letter), Fmt.str(p.spelt), Fmt.str(p.value)]); end;
514516 end;
515517
516518 type Case = record

site/content/advanced/04-files.txt (history) modified +6 -6

@@ -191,23 +191,23 @@
191191 var p: Opt.Parser; verbose: BOOL; n, i: INT; w: []U8; ok: BOOL;
192192 begin
193193 n := 1;
194- Opt.init(ADR(p), "tool", [
194+ Opt.init(p, "tool", [
195195 Opt.usage("", "tool [-v] [--count n] word ..."),
196196 Opt.option("", 'v', "verbose", "", "say more"),
197197 Opt.option("", count, "count", "n", "repeat each word n times")]);
198- while Opt.next(ADR(p)) do
198+ while Opt.next(p) do
199199 if p.letter = 'v' then verbose := true;
200- elsif p.letter = count then n := INT(Opt.number(ADR(p), 1, 5));
200+ elsif p.letter = count then n := INT(Opt.number(p, 1, 5));
201201 end;
202202 end;
203- w, ok := Opt.operand(ADR(p));
204- if not ok then Opt.fail(ADR(p), "no words; see tool -h"); end;
203+ w, ok := Opt.operand(p);
204+ if not ok then Opt.fail(p, "no words; see tool -h"); end;
205205 while ok do
206206 for i := 1 to n do
207207 if verbose then Out.str("word: "); end;
208208 Fmt.print("{}\n", [Fmt.str(w)]);
209209 end;
210- w, ok := Opt.operand(ADR(p));
210+ w, ok := Opt.operand(p);
211211 end;
212212 end;
213213

test/doc/Opt.expect (history) modified +8 -8

@@ -31,38 +31,38 @@
3131 help: BOOL; (* the option just read is -h or --help *)
3232 end;
3333
34-proc init*(p: ^Parser; name: []U8; table: []Row);
34+proc init*(var p: Parser; name: []U8; table: []Row);
3535 p reads the arguments from 1 on, by table, in the program's own
3636 scope; name begins the messages.
3737
38-proc next*(p: ^Parser): BOOL;
38+proc next*(var p: Parser): BOOL;
3939 The next option, in p.letter, p.value and p.long; false when the
4040 scope's options end.
4141
42-proc operand*(p: ^Parser): ([]U8, BOOL);
42+proc operand*(var p: Parser): ([]U8, BOOL);
4343 The next operand, after what options are left; false when there are
4444 no more.
4545
46-proc at*(p: ^Parser): INT;
46+proc at*(var p: Parser): INT;
4747 The number of the argument operand would give next, after what
4848 options are left: the operands from there on are Files.arg(i), for
4949 i up to ARGC() - 1, as an interpreter hands them to its script.
5050
51-proc scope*(p: ^Parser; name: []U8);
51+proc scope*(var p: Parser; name: []U8);
5252 The options of the subcommand name, from the word after its own,
5353 which operand has given.
5454
55-proc number*(p: ^Parser; lo, hi: I64): I64;
55+proc number*(const p: Parser; lo, hi: I64): I64;
5656 The value of the option just read, as a whole number from lo to hi;
5757 anything else is a mistake. A number with no top is given the
5858 largest I64 for hi, and the mistake says "from lo up".
5959
60-proc refuse*(p: ^Parser; wants: []U8);
60+proc refuse*(const p: Parser; wants: []U8);
6161 The value of the option just read refused, as number refuses one:
6262 "-b wants wants, not 'value'", the option as it was written, with
6363 the scope's synopsis lines, and status 2.
6464
65-proc fail*(p: ^Parser; text: []U8);
65+proc fail*(const p: Parser; text: []U8);
6666 The program's own mistake, written as Opt's are but alone, and
6767 status 2.
6868