raz / commit e9738450

commit
e9738450732dd94d3a060e3a268e7bdb0db940c8
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
66c926e6
Opt: -33.87 is an operand where no option is a digit; Opt.misuse writes the program's mistake with the synopsis

patch · log

3 files changed, +68 -1

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

@@ -958,6 +958,7 @@
958958 proc number*(const p: Parser; lo, hi: I64): I64; (* the value just read, a number from lo to hi *)
959959 proc refuse*(const p: Parser; wants: []U8); (* the value just read refused, as number refuses one *)
960960 proc fail*(const p: Parser; text: []U8); (* the program's own mistake *)
961+proc misuse*(const p: Parser; text: []U8); (* the program's own mistake, with the synopsis *)
961962 ```
962963
963964 `Opt` reads a command line by the rules of the C library's `getopt`,
@@ -1019,6 +1020,10 @@
10191020 - `--` ends the options, and isn't an operand; `-` alone is one. The
10201021 options end at the first operand, so in `keep start site python -m
10211022 http.server` the `-m` is python's.
1023+- In a scope with no option whose letter is a digit, a word of `-` and
1024+ a digit, such as `-33.87`, is an operand, and so ends the options. A
1025+ scope that gives a digit a letter, as `head -5` has, reads it as an
1026+ option.
10221027 - Each scope has its own options. An option given twice is read twice.
10231028
10241029 `number` reads the value as `Str.toInt` does. `operand` reads what
@@ -1072,7 +1077,9 @@
10721077 and the synopsis lines. `p.spelt` is that spelling, for the program's
10731078 own sentences. `fail` writes the program's own
10741079 mistakes the same way, but alone, so that the sentence can say where
1075-the help is: `keep: no command; see keep -h`.
1080+the help is: `keep: no command; see keep -h`. `misuse` writes one with
1081+the scope's synopsis lines after it, for a mistake in the operands:
1082+`Opt.misuse(p, "no tests named")`.
10761083
10771084 A mistake in the table is the program's, not its user's. `init` checks
10781085 the table before it reads a word, and ends the program with status 1:

lib/Opt.raz (history) modified +56 -0

@@ -225,6 +225,21 @@
225225 Rt.exit(0);
226226 end;
227227
228+(* Whether the scope has an option whose letter is a digit, as head's
229+ -5 is; if not, a word like -33.87 is an operand. *)
230+proc hasDigitOption(const p: Parser): BOOL;
231+var i: INT;
232+begin
233+ for i := 0 to LEN(p.table) - 1 do
234+ match p.table[i] of
235+ | option(s, letter, _long, _v, _help):
236+ if (s = p.scope) and (letter >= '0') and (letter <= '9') then return true; end;
237+ | else:
238+ end;
239+ end;
240+ return false;
241+end;
242+
228243 (* The next option, a word or a letter of one, into p: read1, or
229244 readEnd at the end of the options. A mistake is refused, or, if
230245 quiet, passed over, as readSkip. *)
@@ -237,6 +252,10 @@
237252 if p.ended or (p.at >= ARGC()) then p.ended := true; return readEnd; end;
238253 w := Files.arg(p.at);
239254 if (LEN(w) < 2) or (w[0] # '-') then p.ended := true; return readEnd; end; (* an operand *)
255+ if (w[1] >= '0') and (w[1] <= '9') and not hasDigitOption(p) then (* a negative number *)
256+ p.ended := true;
257+ return readEnd;
258+ end;
240259 INC(p.at);
241260 if w = "--" then p.ended := true; return readEnd; end;
242261 if w[1] # '-' then
@@ -437,6 +456,13 @@
437456 Rt.exit(2);
438457 end;
439458
459+(* The program's own mistake, written as Opt's are, with the scope's
460+ synopsis lines after it, and status 2. *)
461+proc misuse*(const p: Parser; text: []U8);
462+begin
463+ mistake(p, "{}", [Fmt.str(text)]);
464+end;
465+
440466 (* The module's test. main runs itself once for each case, with the
441467 case's words and OPTTEST in its environment, which makes it keep, the
442468 program of getopt's page, or a program with a bad table, or one with
@@ -488,6 +514,30 @@
488514 Fmt.print("\n", []);
489515 end;
490516
517+(* A scope with no digit option, and one with -5. *)
518+proc digits;
519+var p: Parser; w: []U8; ok: BOOL; n: INT;
520+begin
521+ init(p, "digits", [
522+ usage("", "digits [-v] x y"),
523+ option("", 'v', "", "", "say more"),
524+ sub("", "five", "the scope with -5"),
525+ option("five", '5', "", "", "five")]);
526+ while next(p) do Fmt.print("{}\n", [Fmt.char(p.letter)]); end;
527+ w, ok := operand(p);
528+ if ok and (w = "five") then
529+ scope(p, "five");
530+ while next(p) do Fmt.print("{}\n", [Fmt.char(p.letter)]); end;
531+ w, ok := operand(p);
532+ end;
533+ while ok do
534+ INC(n);
535+ Fmt.print("operand {}\n", [Fmt.str(w)]);
536+ w, ok := operand(p);
537+ end;
538+ if n = 0 then misuse(p, "no operands"); end;
539+end;
540+
491541 proc badTableTest;
492542 var p: Parser;
493543 begin
@@ -567,6 +617,7 @@
567617 if mode = "keep" then keep; return;
568618 elsif mode = "bad" then badTableTest; return;
569619 elsif mode = "plain" then plain; return;
620+ elsif mode = "digits" then digits; return;
570621 end;
571622 cases := [
572623 {mode: k, args: ["keep", "-v", "--wait=30", "start", "-b", "web", "site", "python", "-m", "http.server", "--port", "8000"],
@@ -611,6 +662,11 @@
611662 {mode: "OPTTEST=plain", args: ["plain", "remote", "add", "--color", "always", "-f"], out: "c color=\n"},
612663 {mode: "OPTTEST=plain", args: ["plain", "remote", "add", "-f", "--help"],
613664 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"},
665+ {mode: "OPTTEST=digits", args: ["digits", "-33.87", "151.21"], out: "operand -33.87\noperand 151.21\n"},
666+ {mode: "OPTTEST=digits", args: ["digits", "-v", "-5", "-v"], out: "v\noperand -5\noperand -v\n"},
667+ {mode: "OPTTEST=digits", args: ["digits", "five", "-5", "x"], out: "5\noperand x\n"},
668+ {mode: "OPTTEST=digits", args: ["digits", "-x"], err: "digits: unknown option -x\nusage: digits [-v] x y\n", status: 2},
669+ {mode: "OPTTEST=digits", args: ["digits"], err: "digits: no operands\nusage: digits [-v] x y\n", status: 2},
614670 {mode: "OPTTEST=plain", args: ["plain", "remote", "-h"], out: "usage: plain remote\n\n add add one\n -h, --help print this and exit\n"}];
615671 for i := 0 to LEN(cases) - 1 do run(cases[i], i); end;
616672 end;

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

@@ -66,4 +66,8 @@
6666 The program's own mistake, written as Opt's are but alone, and
6767 status 2.
6868
69+proc misuse*(const p: Parser; text: []U8);
70+ The program's own mistake, written as Opt's are, with the scope's
71+ synopsis lines after it, and status 2.
72+
6973 proc main*;