From e9738450732dd94d3a060e3a268e7bdb0db940c8 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Fri, 23 Oct 2026 11:56:41 +0100 Subject: [PATCH] Opt: -33.87 is an operand where no option is a digit; Opt.misuse writes the program's mistake with the synopsis --- diff --git a/doc/raz-lib.md b/doc/raz-lib.md index 3675500..59832ae 100644 --- a/doc/raz-lib.md +++ b/doc/raz-lib.md @@ -958,6 +958,7 @@ proc number*(const p: Parser; lo, hi: I64): I64; (* the value just read, a number from lo to hi *) proc refuse*(const p: Parser; wants: []U8); (* the value just read refused, as number refuses one *) proc fail*(const p: Parser; text: []U8); (* the program's own mistake *) +proc misuse*(const p: Parser; text: []U8); (* the program's own mistake, with the synopsis *) ``` `Opt` reads a command line by the rules of the C library's `getopt`, @@ -1019,6 +1020,10 @@ - `--` ends the options, and isn't an operand; `-` alone is one. The options end at the first operand, so in `keep start site python -m http.server` the `-m` is python's. +- In a scope with no option whose letter is a digit, a word of `-` and + a digit, such as `-33.87`, is an operand, and so ends the options. A + scope that gives a digit a letter, as `head -5` has, reads it as an + option. - Each scope has its own options. An option given twice is read twice. `number` reads the value as `Str.toInt` does. `operand` reads what @@ -1072,7 +1077,9 @@ 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`. +the help is: `keep: no command; see keep -h`. `misuse` writes one with +the scope's synopsis lines after it, for a mistake in the operands: +`Opt.misuse(p, "no tests named")`. A mistake in the table is the program's, not its user's. `init` checks the table before it reads a word, and ends the program with status 1: diff --git a/lib/Opt.raz b/lib/Opt.raz index 66d36ec..c9f58bf 100644 --- a/lib/Opt.raz +++ b/lib/Opt.raz @@ -225,6 +225,21 @@ Rt.exit(0); end; +(* Whether the scope has an option whose letter is a digit, as head's + -5 is; if not, a word like -33.87 is an operand. *) +proc hasDigitOption(const p: Parser): BOOL; +var i: INT; +begin + for i := 0 to LEN(p.table) - 1 do + match p.table[i] of + | option(s, letter, _long, _v, _help): + if (s = p.scope) and (letter >= '0') and (letter <= '9') then return true; end; + | else: + end; + end; + return false; +end; + (* The next option, a word or a letter of one, into p: read1, or readEnd at the end of the options. A mistake is refused, or, if quiet, passed over, as readSkip. *) @@ -237,6 +252,10 @@ if p.ended or (p.at >= ARGC()) then p.ended := true; return readEnd; end; w := Files.arg(p.at); if (LEN(w) < 2) or (w[0] # '-') then p.ended := true; return readEnd; end; (* an operand *) + if (w[1] >= '0') and (w[1] <= '9') and not hasDigitOption(p) then (* a negative number *) + p.ended := true; + return readEnd; + end; INC(p.at); if w = "--" then p.ended := true; return readEnd; end; if w[1] # '-' then @@ -437,6 +456,13 @@ Rt.exit(2); end; +(* The program's own mistake, written as Opt's are, with the scope's + synopsis lines after it, and status 2. *) +proc misuse*(const p: Parser; text: []U8); +begin + mistake(p, "{}", [Fmt.str(text)]); +end; + (* The module's test. main runs itself once for each case, with the case's words and OPTTEST in its environment, which makes it keep, the program of getopt's page, or a program with a bad table, or one with @@ -488,6 +514,30 @@ Fmt.print("\n", []); end; +(* A scope with no digit option, and one with -5. *) +proc digits; +var p: Parser; w: []U8; ok: BOOL; n: INT; +begin + init(p, "digits", [ + usage("", "digits [-v] x y"), + option("", 'v', "", "", "say more"), + sub("", "five", "the scope with -5"), + option("five", '5', "", "", "five")]); + while next(p) do Fmt.print("{}\n", [Fmt.char(p.letter)]); end; + w, ok := operand(p); + if ok and (w = "five") then + scope(p, "five"); + while next(p) do Fmt.print("{}\n", [Fmt.char(p.letter)]); end; + w, ok := operand(p); + end; + while ok do + INC(n); + Fmt.print("operand {}\n", [Fmt.str(w)]); + w, ok := operand(p); + end; + if n = 0 then misuse(p, "no operands"); end; +end; + proc badTableTest; var p: Parser; begin @@ -567,6 +617,7 @@ if mode = "keep" then keep; return; elsif mode = "bad" then badTableTest; return; elsif mode = "plain" then plain; return; + elsif mode = "digits" then digits; return; end; cases := [ {mode: k, args: ["keep", "-v", "--wait=30", "start", "-b", "web", "site", "python", "-m", "http.server", "--port", "8000"], @@ -611,6 +662,11 @@ {mode: "OPTTEST=plain", args: ["plain", "remote", "add", "--color", "always", "-f"], out: "c color=\n"}, {mode: "OPTTEST=plain", args: ["plain", "remote", "add", "-f", "--help"], out: "usage: plain remote add\n\n -f, --force even if it is there\n -h, --here only here\n -c, --color[=when] colour it: always, never or auto\n --help print this and exit\n"}, + {mode: "OPTTEST=digits", args: ["digits", "-33.87", "151.21"], out: "operand -33.87\noperand 151.21\n"}, + {mode: "OPTTEST=digits", args: ["digits", "-v", "-5", "-v"], out: "v\noperand -5\noperand -v\n"}, + {mode: "OPTTEST=digits", args: ["digits", "five", "-5", "x"], out: "5\noperand x\n"}, + {mode: "OPTTEST=digits", args: ["digits", "-x"], err: "digits: unknown option -x\nusage: digits [-v] x y\n", status: 2}, + {mode: "OPTTEST=digits", args: ["digits"], err: "digits: no operands\nusage: digits [-v] x y\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; end; diff --git a/test/doc/Opt.expect b/test/doc/Opt.expect index fe83cb9..ee467c0 100644 --- a/test/doc/Opt.expect +++ b/test/doc/Opt.expect @@ -66,4 +66,8 @@ The program's own mistake, written as Opt's are but alone, and status 2. +proc misuse*(const p: Parser; text: []U8); + The program's own mistake, written as Opt's are, with the scope's + synopsis lines after it, and status 2. + proc main*;