From b20b4ce40e270b2fc20aa7bcb339ed75a9890e73 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Fri, 16 Oct 2026 20:17:23 +0000 Subject: [PATCH] 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. --- diff --git a/PROPOSALS.md b/PROPOSALS.md index 9a6ba1a..714b667 100644 --- a/PROPOSALS.md +++ b/PROPOSALS.md @@ -629,30 +629,6 @@ of the reply; a server that says nothing for a minute, as a model does while it thinks, keeps the turn going for that minute. -## The spelling of the option just read - -`Opt.Parser` keeps how the option just read was written (`spelt`, no -dashes) to word its own messages, `--budget wants a number from ...`, -but keeps it private, so a program that refuses a value with -`Opt.fail` must name the option again itself: - -``` -| keyInclude: - if not addGlob(p.value) then - Opt.fail(ADR(p), "--include wants a glob, not ..."); - end; -``` - -The name is then in two places, the table and the message, which -`Opt` was made to keep apart; and the message says `--include` even -when the program has a letter for it and the user wrote that. The -proposal: `spelt*` exported, read only, beside `letter`, `value` and -`long`; or `Opt.refuse*(p: ^Parser; wants: []U8)`, which writes -`{-x or --long} wants {wants}, not '{value}'` as `number` does. It -breaks no program. - -Met in rre's `--include` and `--exclude`, whose globs Rwglob checks. - ## `-h` for a program that wants it `Opt` takes `-h` and `--help` for itself, and a table that names diff --git a/doc/raz-lib.md b/doc/raz-lib.md index b3dd029..574234f 100644 --- a/doc/raz-lib.md +++ b/doc/raz-lib.md @@ -810,6 +810,7 @@ letter*: U8; (* the option just read *) value*: []U8; (* its value; "" for a flag *) long*: BOOL; (* it was written by its long name *) + spelt*: []U8; (* as it was written, no dashes: "b" or "bar"; to read only *) ... end; proc init*(p: ^Parser; name: []U8; table: []Row); (* the arguments from 1 on, by table *) @@ -818,6 +819,7 @@ proc at*(p: ^Parser): INT; (* the argument operand would give next *) proc scope*(p: ^Parser; name: []U8); (* subcommand name's options, from the word after it *) proc number*(p: ^Parser; lo, hi: I64): I64; (* the value just read, a number from lo to hi *) +proc refuse*(p: ^Parser; wants: []U8); (* the value just read refused, as number refuses one *) proc fail*(p: ^Parser; text: []U8); (* the program's own mistake *) ``` @@ -919,7 +921,12 @@ The sentences are `unknown option -x` (or `--name`, up to its `=`), `-b needs a label`, `--follow takes no value` and `--wait wants a number from 1 to 600, not '0'`; for a number with no top, given the -largest `I64` for `hi`, `--jobs wants a number from 1 up, not '0'`. `fail` writes the program's own +largest `I64` for `hi`, `--jobs wants a number from 1 up, not '0'`. `refuse` refuses a value +the program reads itself, as `number` refuses one, naming the option as +the user wrote it, `-b` or `--bar`: `Opt.refuse(ADR(p), "a label with +no spaces")` writes `keep: -b wants a label with no spaces, not 'a b'`, +and the synopsis lines. `p.spelt` is that spelling, for the program's +own sentences. `fail` writes the program's own mistakes the same way, but alone, so that the sentence can say where the help is: `keep: no command; see keep -h`. diff --git a/lib/Opt.raz b/lib/Opt.raz index d6adfdc..27cefc3 100644 --- a/lib/Opt.raz +++ b/lib/Opt.raz @@ -21,13 +21,13 @@ letter*: U8; (* the option just read *) value*: []U8; (* its value; "" for a flag *) long*: BOOL; (* it was written by its long name *) + spelt*: []U8; (* as it was written, no dashes: "b" or "bar"; the program's to read only *) (* the rest are Opt's own *) name: []U8; (* the program's, as the messages begin *) table: []Row; scope: []U8; at: INT; (* the next word *) group: []U8; (* the letters left of a word of them, as -lw *) - spelt: []U8; (* the option just read, as written, no dashes *) ended: BOOL; (* the scope's options have ended *) help: BOOL; (* the option just read is -h or --help *) end; @@ -90,7 +90,7 @@ (* A mistake on the command line: the sentence f, the scope's synopsis lines under it, and status 2. *) -proc refuse(p: ^Parser; f: []U8; args: []Fmt.Arg); +proc mistake(p: ^Parser; f: []U8; args: []Fmt.Arg); var b: Buf.Buf; begin Buf.init(ADR(b), Heap.heap); @@ -225,17 +225,17 @@ r, p^.letter, val := find(p, 0, p^.spelt); if r < 0 then if quiet then return readSkip; end; - refuse(p, "unknown option --{}", [Fmt.str(p^.spelt)]); + mistake(p, "unknown option --{}", [Fmt.str(p^.spelt)]); end; if (eq >= 0) and (LEN(val) = 0) then if quiet then return readSkip; end; - refuse(p, "--{} takes no value", [Fmt.str(p^.spelt)]); + mistake(p, "--{} takes no value", [Fmt.str(p^.spelt)]); elsif eq >= 0 then p^.value := w[eq + 1..LEN(w)]; elsif LEN(val) > 0 then if p^.at >= ARGC() then if quiet then return readEnd; end; - refuse(p, "--{} needs a {}", [Fmt.str(p^.spelt), Fmt.str(val)]); + mistake(p, "--{} needs a {}", [Fmt.str(p^.spelt), Fmt.str(val)]); end; p^.value := Files.arg(p^.at); INC(p^.at); @@ -253,7 +253,7 @@ r, p^.letter, val := find(p, c, ""); if r < 0 then if quiet then return readSkip; end; - refuse(p, "unknown option -{}", [Fmt.str(p^.spelt)]); + mistake(p, "unknown option -{}", [Fmt.str(p^.spelt)]); end; if LEN(val) > 0 then if LEN(p^.group) > 0 then @@ -264,7 +264,7 @@ INC(p^.at); else if quiet then return readEnd; end; - refuse(p, "-{} needs a {}", [Fmt.str(p^.spelt), Fmt.str(val)]); + mistake(p, "-{} needs a {}", [Fmt.str(p^.spelt), Fmt.str(val)]); end; end; return read1; @@ -377,15 +377,24 @@ n, ok := Str.toInt(p^.value); if not ok or (n < lo) or (n > hi) then if hi = largest then - refuse(p, "{}{} wants a number from {} up, not '{}'", + mistake(p, "{}{} wants a number from {} up, not '{}'", [Fmt.str(dashes(p)), Fmt.str(p^.spelt), Fmt.int(lo), Fmt.str(p^.value)]); end; - refuse(p, "{}{} wants a number from {} to {}, not '{}'", + mistake(p, "{}{} wants a number from {} to {}, not '{}'", [Fmt.str(dashes(p)), Fmt.str(p^.spelt), Fmt.int(lo), Fmt.int(hi), Fmt.str(p^.value)]); end; return n; end; +(* The value of the option just read refused, as number refuses one: + "-b wants wants, not 'value'", the option as it was written, with + the scope's synopsis lines, and status 2. *) +proc refuse*(p: ^Parser; wants: []U8); +begin + mistake(p, "{}{} wants {}, not '{}'", + [Fmt.str(dashes(p)), Fmt.str(p^.spelt), Fmt.str(wants), Fmt.str(p^.value)]); +end; + (* The program's own mistake, written as Opt's are but alone, and status 2. *) proc fail*(p: ^Parser; text: []U8); @@ -429,7 +438,9 @@ end; scope(ADR(p), "start"); while next(ADR(p)) do - if p.letter = 'b' then bar := p.value; end; + if (p.letter = 'b') and (Str.find(p.value, " ") >= 0) then refuse(ADR(p), "a label with no spaces"); + elsif p.letter = 'b' then bar := p.value; + end; end; job, ok := operand(ADR(p)); (* the command and its arguments, as they are, by at *) @@ -465,7 +476,7 @@ scope(ADR(p), "remote"); _w, _ok := operand(ADR(p)); scope(ADR(p), "remote.add"); - while next(ADR(p)) do Fmt.print("{}\n", [Fmt.char(p.letter)]); end; + while next(ADR(p)) do Fmt.print("{} {}\n", [Fmt.char(p.letter), Fmt.str(p.spelt)]); end; end; type Case = record @@ -545,10 +556,14 @@ {mode: k, args: ["keep", "--wait", "0", "start", "a", "b"], err: "keep: --wait wants a number from 1 to 600, not '0'\nusage: keep [-v] [--wait secs] start ...\n", status: 2}, {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}, + {mode: k, args: ["keep", "start", "-b", "a b", "n", "c"], + err: "keep: -b wants a label with no spaces, not 'a b'\nusage: keep start [-b label] name command [argument ...]\n", status: 2}, + {mode: k, args: ["keep", "start", "--bar=a b", "n", "c"], + err: "keep: --bar wants a label with no spaces, not 'a b'\nusage: keep start [-b label] name command [argument ...]\n", status: 2}, {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}, {mode: k, args: ["keep", "--jobs", "9223372036854775807", "start", "n", "c"], out: "job n: c\n"}, {mode: "OPTTEST=bad", args: ["bad", "-v"], err: "bad: option table, row 2: the option is in the scope already\n", status: 1}, - {mode: "OPTTEST=plain", args: ["plain", "remote", "add", "-f", "--force"], out: "f\nf\n"}, + {mode: "OPTTEST=plain", args: ["plain", "remote", "add", "-f", "--force"], out: "f f\nf force\n"}, {mode: "OPTTEST=plain", args: ["plain", "remote", "add", "-x"], err: "plain: unknown option -x\nusage: plain remote add\n", status: 2}, {mode: "OPTTEST=plain", args: ["plain", "remote", "-h"], out: "usage: plain remote\n\n add add one\n -h, --help print this and exit\n"}]; for i := 0 to LEN(cases) - 1 do run(cases[i], i); end; diff --git a/test/doc/Opt.expect b/test/doc/Opt.expect index 31f597e..2f88867 100644 --- a/test/doc/Opt.expect +++ b/test/doc/Opt.expect @@ -20,13 +20,13 @@ letter*: U8; (* the option just read *) value*: []U8; (* its value; "" for a flag *) long*: BOOL; (* it was written by its long name *) + spelt*: []U8; (* as it was written, no dashes: "b" or "bar"; the program's to read only *) (* the rest are Opt's own *) name: []U8; (* the program's, as the messages begin *) table: []Row; scope: []U8; at: INT; (* the next word *) group: []U8; (* the letters left of a word of them, as -lw *) - spelt: []U8; (* the option just read, as written, no dashes *) ended: BOOL; (* the scope's options have ended *) help: BOOL; (* the option just read is -h or --help *) end; @@ -56,6 +56,11 @@ The value of the option just read, as a whole number from lo to hi; anything else is a mistake. A number with no top is given the largest I64 for hi, and the mistake says "from lo up". + +proc refuse*(p: ^Parser; wants: []U8); + The value of the option just read refused, as number refuses one: + "-b wants wants, not 'value'", the option as it was written, with + the scope's synopsis lines, and status 2. proc fail*(p: ^Parser; text: []U8); The program's own mistake, written as Opt's are but alone, and