raz / commit b20b4ce4

commit
b20b4ce40e270b2fc20aa7bcb339ed75a9890e73
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
c08b4be8
Opt: spelt, the option as it was written, and refuse

A program that refused a value with fail named the option again itself,
in two places, and as --include even when the user wrote its letter.
spelt is exported, to read only, beside letter, value and long; and
refuse(p, wants) writes -x or --long wants wants, not 'value', with
the synopsis lines, as number does. The private refuse is now mistake.
Tested in Opt's own cases; its proposal is removed.

patch · log

4 files changed, +41 -38

PROPOSALS.md modified +0 -24

@@ -629,30 +629,6 @@
629629 of the reply; a server that says nothing for a minute, as a model does
630630 while it thinks, keeps the turn going for that minute.
631631
632-## The spelling of the option just read
633-
634-`Opt.Parser` keeps how the option just read was written (`spelt`, no
635-dashes) to word its own messages, `--budget wants a number from ...`,
636-but keeps it private, so a program that refuses a value with
637-`Opt.fail` must name the option again itself:
638-
639-```
640-| keyInclude:
641- if not addGlob(p.value) then
642- Opt.fail(ADR(p), "--include wants a glob, not ...");
643- end;
644-```
645-
646-The name is then in two places, the table and the message, which
647-`Opt` was made to keep apart; and the message says `--include` even
648-when the program has a letter for it and the user wrote that. The
649-proposal: `spelt*` exported, read only, beside `letter`, `value` and
650-`long`; or `Opt.refuse*(p: ^Parser; wants: []U8)`, which writes
651-`{-x or --long} wants {wants}, not '{value}'` as `number` does. It
652-breaks no program.
653-
654-Met in rre's `--include` and `--exclude`, whose globs Rwglob checks.
655-
656632 ## `-h` for a program that wants it
657633
658634 `Opt` takes `-h` and `--help` for itself, and a table that names

doc/raz-lib.md (history) modified +8 -1

@@ -810,6 +810,7 @@
810810 letter*: U8; (* the option just read *)
811811 value*: []U8; (* its value; "" for a flag *)
812812 long*: BOOL; (* it was written by its long name *)
813+ spelt*: []U8; (* as it was written, no dashes: "b" or "bar"; to read only *)
813814 ...
814815 end;
815816 proc init*(p: ^Parser; name: []U8; table: []Row); (* the arguments from 1 on, by table *)
@@ -818,6 +819,7 @@
818819 proc at*(p: ^Parser): INT; (* the argument operand would give next *)
819820 proc scope*(p: ^Parser; name: []U8); (* subcommand name's options, from the word after it *)
820821 proc number*(p: ^Parser; lo, hi: I64): I64; (* the value just read, a number from lo to hi *)
822+proc refuse*(p: ^Parser; wants: []U8); (* the value just read refused, as number refuses one *)
821823 proc fail*(p: ^Parser; text: []U8); (* the program's own mistake *)
822824 ```
823825
@@ -919,7 +921,12 @@
919921 The sentences are `unknown option -x` (or `--name`, up to its `=`),
920922 `-b needs a label`, `--follow takes no value` and `--wait wants a
921923 number from 1 to 600, not '0'`; for a number with no top, given the
922-largest `I64` for `hi`, `--jobs wants a number from 1 up, not '0'`. `fail` writes the program's own
924+largest `I64` for `hi`, `--jobs wants a number from 1 up, not '0'`. `refuse` refuses a value
925+the program reads itself, as `number` refuses one, naming the option as
926+the user wrote it, `-b` or `--bar`: `Opt.refuse(ADR(p), "a label with
927+no spaces")` writes `keep: -b wants a label with no spaces, not 'a b'`,
928+and the synopsis lines. `p.spelt` is that spelling, for the program's
929+own sentences. `fail` writes the program's own
923930 mistakes the same way, but alone, so that the sentence can say where
924931 the help is: `keep: no command; see keep -h`.
925932

lib/Opt.raz (history) modified +27 -12

@@ -21,13 +21,13 @@
2121 letter*: U8; (* the option just read *)
2222 value*: []U8; (* its value; "" for a flag *)
2323 long*: BOOL; (* it was written by its long name *)
24+ spelt*: []U8; (* as it was written, no dashes: "b" or "bar"; the program's to read only *)
2425 (* the rest are Opt's own *)
2526 name: []U8; (* the program's, as the messages begin *)
2627 table: []Row;
2728 scope: []U8;
2829 at: INT; (* the next word *)
2930 group: []U8; (* the letters left of a word of them, as -lw *)
30- spelt: []U8; (* the option just read, as written, no dashes *)
3131 ended: BOOL; (* the scope's options have ended *)
3232 help: BOOL; (* the option just read is -h or --help *)
3333 end;
@@ -90,7 +90,7 @@
9090
9191 (* A mistake on the command line: the sentence f, the scope's synopsis
9292 lines under it, and status 2. *)
93-proc refuse(p: ^Parser; f: []U8; args: []Fmt.Arg);
93+proc mistake(p: ^Parser; f: []U8; args: []Fmt.Arg);
9494 var b: Buf.Buf;
9595 begin
9696 Buf.init(ADR(b), Heap.heap);
@@ -225,17 +225,17 @@
225225 r, p^.letter, val := find(p, 0, p^.spelt);
226226 if r < 0 then
227227 if quiet then return readSkip; end;
228- refuse(p, "unknown option --{}", [Fmt.str(p^.spelt)]);
228+ mistake(p, "unknown option --{}", [Fmt.str(p^.spelt)]);
229229 end;
230230 if (eq >= 0) and (LEN(val) = 0) then
231231 if quiet then return readSkip; end;
232- refuse(p, "--{} takes no value", [Fmt.str(p^.spelt)]);
232+ mistake(p, "--{} takes no value", [Fmt.str(p^.spelt)]);
233233 elsif eq >= 0 then
234234 p^.value := w[eq + 1..LEN(w)];
235235 elsif LEN(val) > 0 then
236236 if p^.at >= ARGC() then
237237 if quiet then return readEnd; end;
238- refuse(p, "--{} needs a {}", [Fmt.str(p^.spelt), Fmt.str(val)]);
238+ mistake(p, "--{} needs a {}", [Fmt.str(p^.spelt), Fmt.str(val)]);
239239 end;
240240 p^.value := Files.arg(p^.at);
241241 INC(p^.at);
@@ -253,7 +253,7 @@
253253 r, p^.letter, val := find(p, c, "");
254254 if r < 0 then
255255 if quiet then return readSkip; end;
256- refuse(p, "unknown option -{}", [Fmt.str(p^.spelt)]);
256+ mistake(p, "unknown option -{}", [Fmt.str(p^.spelt)]);
257257 end;
258258 if LEN(val) > 0 then
259259 if LEN(p^.group) > 0 then
@@ -264,7 +264,7 @@
264264 INC(p^.at);
265265 else
266266 if quiet then return readEnd; end;
267- refuse(p, "-{} needs a {}", [Fmt.str(p^.spelt), Fmt.str(val)]);
267+ mistake(p, "-{} needs a {}", [Fmt.str(p^.spelt), Fmt.str(val)]);
268268 end;
269269 end;
270270 return read1;
@@ -377,15 +377,24 @@
377377 n, ok := Str.toInt(p^.value);
378378 if not ok or (n < lo) or (n > hi) then
379379 if hi = largest then
380- refuse(p, "{}{} wants a number from {} up, not '{}'",
380+ mistake(p, "{}{} wants a number from {} up, not '{}'",
381381 [Fmt.str(dashes(p)), Fmt.str(p^.spelt), Fmt.int(lo), Fmt.str(p^.value)]);
382382 end;
383- refuse(p, "{}{} wants a number from {} to {}, not '{}'",
383+ mistake(p, "{}{} wants a number from {} to {}, not '{}'",
384384 [Fmt.str(dashes(p)), Fmt.str(p^.spelt), Fmt.int(lo), Fmt.int(hi), Fmt.str(p^.value)]);
385385 end;
386386 return n;
387387 end;
388388
389+(* The value of the option just read refused, as number refuses one:
390+ "-b wants wants, not 'value'", the option as it was written, with
391+ the scope's synopsis lines, and status 2. *)
392+proc refuse*(p: ^Parser; wants: []U8);
393+begin
394+ mistake(p, "{}{} wants {}, not '{}'",
395+ [Fmt.str(dashes(p)), Fmt.str(p^.spelt), Fmt.str(wants), Fmt.str(p^.value)]);
396+end;
397+
389398 (* The program's own mistake, written as Opt's are but alone, and
390399 status 2. *)
391400 proc fail*(p: ^Parser; text: []U8);
@@ -429,7 +438,9 @@
429438 end;
430439 scope(ADR(p), "start");
431440 while next(ADR(p)) do
432- if p.letter = 'b' then bar := p.value; end;
441+ if (p.letter = 'b') and (Str.find(p.value, " ") >= 0) then refuse(ADR(p), "a label with no spaces");
442+ elsif p.letter = 'b' then bar := p.value;
443+ end;
433444 end;
434445 job, ok := operand(ADR(p));
435446 (* the command and its arguments, as they are, by at *)
@@ -465,7 +476,7 @@
465476 scope(ADR(p), "remote");
466477 _w, _ok := operand(ADR(p));
467478 scope(ADR(p), "remote.add");
468- while next(ADR(p)) do Fmt.print("{}\n", [Fmt.char(p.letter)]); end;
479+ while next(ADR(p)) do Fmt.print("{} {}\n", [Fmt.char(p.letter), Fmt.str(p.spelt)]); end;
469480 end;
470481
471482 type Case = record
@@ -545,10 +556,14 @@
545556 {mode: k, args: ["keep", "--wait", "0", "start", "a", "b"],
546557 err: "keep: --wait wants a number from 1 to 600, not '0'\nusage: keep [-v] [--wait secs] start ...\n", status: 2},
547558 {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},
559+ {mode: k, args: ["keep", "start", "-b", "a b", "n", "c"],
560+ err: "keep: -b wants a label with no spaces, not 'a b'\nusage: keep start [-b label] name command [argument ...]\n", status: 2},
561+ {mode: k, args: ["keep", "start", "--bar=a b", "n", "c"],
562+ err: "keep: --bar wants a label with no spaces, not 'a b'\nusage: keep start [-b label] name command [argument ...]\n", status: 2},
548563 {mode: k, args: ["keep", "--jobs=0", "start"], err: "keep: --jobs wants a number from 1 up, not '0'\nusage: keep [-v] [--wait secs] start ...\n", status: 2},
549564 {mode: k, args: ["keep", "--jobs", "9223372036854775807", "start", "n", "c"], out: "job n: c\n"},
550565 {mode: "OPTTEST=bad", args: ["bad", "-v"], err: "bad: option table, row 2: the option is in the scope already\n", status: 1},
551- {mode: "OPTTEST=plain", args: ["plain", "remote", "add", "-f", "--force"], out: "f\nf\n"},
566+ {mode: "OPTTEST=plain", args: ["plain", "remote", "add", "-f", "--force"], out: "f f\nf force\n"},
552567 {mode: "OPTTEST=plain", args: ["plain", "remote", "add", "-x"], err: "plain: unknown option -x\nusage: plain remote add\n", status: 2},
553568 {mode: "OPTTEST=plain", args: ["plain", "remote", "-h"], out: "usage: plain remote\n\n add add one\n -h, --help print this and exit\n"}];
554569 for i := 0 to LEN(cases) - 1 do run(cases[i], i); end;

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

@@ -20,13 +20,13 @@
2020 letter*: U8; (* the option just read *)
2121 value*: []U8; (* its value; "" for a flag *)
2222 long*: BOOL; (* it was written by its long name *)
23+ spelt*: []U8; (* as it was written, no dashes: "b" or "bar"; the program's to read only *)
2324 (* the rest are Opt's own *)
2425 name: []U8; (* the program's, as the messages begin *)
2526 table: []Row;
2627 scope: []U8;
2728 at: INT; (* the next word *)
2829 group: []U8; (* the letters left of a word of them, as -lw *)
29- spelt: []U8; (* the option just read, as written, no dashes *)
3030 ended: BOOL; (* the scope's options have ended *)
3131 help: BOOL; (* the option just read is -h or --help *)
3232 end;
@@ -56,6 +56,11 @@
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".
59+
60+proc refuse*(p: ^Parser; wants: []U8);
61+ The value of the option just read refused, as number refuses one:
62+ "-b wants wants, not 'value'", the option as it was written, with
63+ the scope's synopsis lines, and status 2.
5964
6065 proc fail*(p: ^Parser; text: []U8);
6166 The program's own mistake, written as Opt's are but alone, and