raz / commit 6970dbf9

commit
6970dbf9b6fb15118c3b844c66ebc2f2314ce0a7
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
b20b4ce4
Opt: a scope may have -h of its own

grep's -h, no file names, couldn't be written: a table that named -h was
refused. A scope may now give -h to an option of its own, and help is
--help alone there; only --help is still Opt's. The help's own row is
made as a table's row is, with no letter where the scope has -h, so
left and item no longer take nil for it. Tested in Opt's plain cases;
its proposal is removed.

patch · log

4 files changed, +54 -44

PROPOSALS.md modified +0 -9

@@ -629,15 +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-## `-h` for a program that wants it
633-
634-`Opt` takes `-h` and `--help` for itself, and a table that names
635-either is the program's mistake ("-h and --help are Opt's"). grep's
636-`-h`, no file names, can't be written: rre, which follows grep, has
637-`--no-filename` without a letter. The proposal: a table may give `-h`
638-to an option of its own, and then help is `--help` only, as it is for
639-grep. It breaks no program, since no table names `-h` now.
640-
641632 ## An option whose value may be left out
642633
643634 An option takes a value always or never. grep's `--color` is

doc/raz-lib.md (history) modified +5 -2

@@ -908,6 +908,9 @@
908908
909909 A scope with several synopsis lines has the others indented under the
910910 first; one with none, `usage:` and its words, as `usage: keep start`.
911+A scope may give `-h` to an option of its own, as grep's "no file
912+names"; help there is `--help` alone, and its row in the help has no
913+letter.
911914
912915 A mistake on the command line is a sentence on standard error, the
913916 program's name first, then the scope's synopsis lines, as the help
@@ -934,8 +937,8 @@
934937 the table before it reads a word, and ends the program with status 1:
935938 `keep: option table, row 3: the option is in the scope already`. The
936939 others are an option with neither a letter nor a long name, a long
937-name with `=` in it, a letter `-`, and `-h` or `--help` in the table,
938-since they are `Opt`'s.
940+name with `=` in it, a letter `-`, and `--help` in the table, since
941+it is `Opt`'s.
939942
940943 **Width** says how many columns of a terminal text takes, for a
941944 program with a screen that wraps its lines and cuts them at the

lib/Opt.raz (history) modified +47 -31

@@ -6,8 +6,8 @@
66
77 Depends on Files for the words, and writes the help and the
88 messages itself, then ends the program.
9- Leaves out: options after operands (GNU's reordering), a program
10- that keeps -h for itself, and completion for concha. *)
9+ Leaves out: options after operands (GNU's reordering), and
10+ completion for concha. *)
1111 module Opt;
1212 import Files, Str, Buf, Fmt, Heap, Out, Sys, Rt;
1313
@@ -108,11 +108,9 @@
108108 return "-";
109109 end;
110110
111-(* The left column of a row in the help, as "-b, --bar label"; the
112- help's own row if r is nil. *)
111+(* The left column of a row in the help, as "-b, --bar label". *)
113112 proc left(b: ^Buf.Buf; r: ^Row);
114113 begin
115- if r = nil then Buf.str(b, "-h, --help"); return; end;
116114 match r^ of
117115 | sub(_s, name, _help): Buf.str(b, name);
118116 | option(_s, letter, long, value, _help):
@@ -144,7 +142,6 @@
144142 Buf.str(b, " ");
145143 left(b, r);
146144 for i := leftWidth(r) to width + 2 do Buf.add(b, ' '); end;
147- if r = nil then Buf.str(b, "print this and exit\n"); return; end;
148145 match r^ of
149146 | sub(_s, _name, help): Fmt.write(b, "{}\n", [Fmt.str(help)]);
150147 | option(_s, _l, _long, _v, help): Fmt.write(b, "{}\n", [Fmt.str(help)]);
@@ -152,15 +149,46 @@
152149 end;
153150 end;
154151
152+(* The scope's option with the letter c, or with the long name long:
153+ its row, or -1, and its letter and the name of its value. *)
154+proc find(p: ^Parser; c: U8; long: []U8): (INT, U8, []U8);
155+var i: INT;
156+begin
157+ for i := 0 to LEN(p^.table) - 1 do
158+ match p^.table[i] of
159+ | option(s, letter, lg, value, _help):
160+ if (s = p^.scope) and ((c >= '!') and (letter = c) or (LEN(long) > 0) and (lg = long)) then
161+ return i, letter, value;
162+ end;
163+ | else:
164+ end;
165+ end;
166+ return -1, 0, "";
167+end;
168+
169+(* Whether the scope has an option -h of its own, so that help is
170+ --help alone there, as it is for grep, whose -h is "no file names". *)
171+proc ownsH(p: ^Parser): BOOL;
172+var r: INT; _l: U8; _v: []U8;
173+begin
174+ r, _l, _v := find(p, 'h', "");
175+ return r >= 0;
176+end;
177+
155178 (* The scope's help: its synopsis lines, then its subcommands, its
156- options and the help itself, each with its line; and status 0. *)
179+ options and the help itself, each with its line; and status 0. The
180+ help's own row is made as the table's are, with no letter if the
181+ scope has -h. *)
157182 proc printHelp(p: ^Parser);
158-var b: Buf.Buf; i, width: INT; r: ^Row;
183+var b: Buf.Buf; i, width: INT; r: ^Row; own: Row; h: U8;
159184 begin
185+ h := 'h';
186+ if ownsH(p) then h := 0; end;
187+ own := option(p^.scope, h, "help", "", "print this and exit");
160188 Buf.init(ADR(b), Heap.heap);
161189 usageOf(p, ADR(b));
162190 Buf.add(ADR(b), 10);
163- width := leftWidth(nil);
191+ width := leftWidth(ADR(own));
164192 for i := 0 to LEN(p^.table) - 1 do
165193 r := ADR(p^.table[i]);
166194 if inScope(p, r^) and (leftWidth(r) > width) then width := leftWidth(r); end;
@@ -179,28 +207,11 @@
179207 | else:
180208 end;
181209 end;
182- item(ADR(b), nil, width);
210+ item(ADR(b), ADR(own), width);
183211 say(Sys.stdout, ADR(b));
184212 Rt.exit(0);
185213 end;
186214
187-(* The scope's option with the letter c, or with the long name long:
188- its row, or -1, and its letter and the name of its value. *)
189-proc find(p: ^Parser; c: U8; long: []U8): (INT, U8, []U8);
190-var i: INT;
191-begin
192- for i := 0 to LEN(p^.table) - 1 do
193- match p^.table[i] of
194- | option(s, letter, lg, value, _help):
195- if (s = p^.scope) and ((c >= '!') and (letter = c) or (LEN(long) > 0) and (lg = long)) then
196- return i, letter, value;
197- end;
198- | else:
199- end;
200- end;
201- return -1, 0, "";
202-end;
203-
204215 (* The next option, a word or a letter of one, into p: read1, or
205216 readEnd at the end of the options. A mistake is refused, or, if
206217 quiet, passed over, as readSkip. *)
@@ -249,8 +260,8 @@
249260 p^.group := p^.group[1..LEN(p^.group)];
250261 p^.long := false;
251262 p^.letter := c;
252- if c = 'h' then p^.help := true; return read1; end;
253263 r, p^.letter, val := find(p, c, "");
264+ if (c = 'h') and (r < 0) then p^.help := true; return read1; end;
254265 if r < 0 then
255266 if quiet then return readSkip; end;
256267 mistake(p, "unknown option -{}", [Fmt.str(p^.spelt)]);
@@ -306,7 +317,7 @@
306317 end;
307318 if letter = '-' then badTable(p, i, "no option is -"); end;
308319 if Str.find(long, "=") >= 0 then badTable(p, i, "a long name has no ="); end;
309- if (letter = 'h') or (long = "help") then badTable(p, i, "-h and --help are Opt's"); end;
320+ if long = "help" then badTable(p, i, "--help is Opt's"); end;
310321 for j := 0 to i - 1 do
311322 match p^.table[j] of
312323 | option(s2, letter2, long2, _v2, _help2):
@@ -464,14 +475,16 @@
464475 option("", 'v', "loud", "", "say it again")]);
465476 end;
466477
467-(* Subcommands of subcommands, and no synopsis lines. *)
478+(* Subcommands of subcommands, no synopsis lines, and a -h of the
479+ program's own. *)
468480 proc plain;
469481 var p: Parser; _w: []U8; _ok: BOOL;
470482 begin
471483 init(ADR(p), "plain", [
472484 sub("", "remote", "the remotes"),
473485 sub("remote", "add", "add one"),
474- option("remote.add", 'f', "force", "", "even if it is there")]);
486+ option("remote.add", 'f', "force", "", "even if it is there"),
487+ option("remote.add", 'h', "here", "", "only here")]);
475488 _w, _ok := operand(ADR(p));
476489 scope(ADR(p), "remote");
477490 _w, _ok := operand(ADR(p));
@@ -565,6 +578,9 @@
565578 {mode: "OPTTEST=bad", args: ["bad", "-v"], err: "bad: option table, row 2: the option is in the scope already\n", status: 1},
566579 {mode: "OPTTEST=plain", args: ["plain", "remote", "add", "-f", "--force"], out: "f f\nf force\n"},
567580 {mode: "OPTTEST=plain", args: ["plain", "remote", "add", "-x"], err: "plain: unknown option -x\nusage: plain remote add\n", status: 2},
581+ {mode: "OPTTEST=plain", args: ["plain", "remote", "add", "-hf"], out: "h h\nf f\n"},
582+ {mode: "OPTTEST=plain", args: ["plain", "remote", "add", "-f", "--help"],
583+ out: "usage: plain remote add\n\n -f, --force even if it is there\n -h, --here only here\n --help print this and exit\n"},
568584 {mode: "OPTTEST=plain", args: ["plain", "remote", "-h"], out: "usage: plain remote\n\n add add one\n -h, --help print this and exit\n"}];
569585 for i := 0 to LEN(cases) - 1 do run(cases[i], i); end;
570586 end;

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

@@ -7,8 +7,8 @@
77
88 Depends on Files for the words, and writes the help and the
99 messages itself, then ends the program.
10- Leaves out: options after operands (GNU's reordering), a program
11- that keeps -h for itself, and completion for concha.
10+ Leaves out: options after operands (GNU's reordering), and
11+ completion for concha.
1212
1313 type Row* = variant
1414 usage(scope, line: []U8); (* a synopsis, without "usage: " *)