raz / commit c06e3bdb

commit
c06e3bdb4cfc78d3155d50dc371888e203b89c4a
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
6970dbf9
Opt: a value that may be left out, written [name]

grep's --color[=WHEN] is auto when bare; in Opt a value was given always
or never, so rre --color x read x as the value, not the pattern. A row's
value written in brackets may be left out, and is "" then: given only in
the option's own word, --color=when or -cwhen, never as the next, as
getopt's :: has it. The help shows -c, --color[=when]; a value with one
bracket and not the other is a mistake in the table. Tested in Opt's
plain cases; its proposal is removed.

patch · log

3 files changed, +39 -21

PROPOSALS.md modified +0 -10

@@ -628,13 +628,3 @@
628628 thread, which can only set a flag that the turn reads after each read
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.
631-
632-## An option whose value may be left out
633-
634-An option takes a value always or never. grep's `--color` is
635-`--color[=WHEN]`: bare, it means `auto`. In `Opt`, rre's `--color`
636-must have its value, and `rre --color x` reads `x` as the value, not
637-the pattern. The proposal: a row's `value` written `[WHEN]` makes the
638-value optional, given only as `--color=WHEN` (never as the next
639-argument, as getopt's `::` has it), `""` when left out; for a letter,
640-only joined, `-cWHEN`. It breaks no program.

doc/raz-lib.md (history) modified +6 -3

@@ -839,7 +839,10 @@
839839 only has a key of its own below `'!'` for a letter, so that the
840840 program can tell it from the others. With a `value`, the name the
841841 help and the messages give it, the option takes a value; with `""` it
842-is a flag.
842+is a flag. A value written in brackets, `"[when]"`, may be left out,
843+and is `""` then: it is given only in the option's own word,
844+`--color=always` or `-calways`, never as the next, as `getopt`'s `::`
845+has it, and the help shows it as `--color[=when]`.
843846
844847 ```
845848 const wait = 1; (* the key of --wait, which has no letter *)
@@ -937,8 +940,8 @@
937940 the table before it reads a word, and ends the program with status 1:
938941 `keep: option table, row 3: the option is in the scope already`. The
939942 others are an option with neither a letter nor a long name, a long
940-name with `=` in it, a letter `-`, and `--help` in the table, since
941-it is `Opt`'s.
943+name with `=` in it, a letter `-`, a value with one bracket but not
944+the other, and `--help` in the table, since it is `Opt`'s.
942945
943946 **Width** says how many columns of a terminal text takes, for a
944947 program with a screen that wraps its lines and cuts them at the

lib/Opt.raz (history) modified +33 -8

@@ -108,7 +108,17 @@
108108 return "-";
109109 end;
110110
111-(* The left column of a row in the help, as "-b, --bar label". *)
111+(* Whether an option's value may be left out, written [name] in its
112+ row: given only in the option's own word, --color=when or -cwhen,
113+ never as the next word, as getopt's :: has it. *)
114+proc optional(value: []U8): BOOL;
115+begin
116+ return (LEN(value) > 2) and (value[0] = '[') and (value[LEN(value) - 1] = ']');
117+end;
118+
119+(* The left column of a row in the help, as "-b, --bar label", or, for
120+ a value that may be left out, "-c, --color[=when]", as grep's help
121+ has it. *)
112122 proc left(b: ^Buf.Buf; r: ^Row);
113123 begin
114124 match r^ of
@@ -119,7 +129,10 @@
119129 if letter >= '!' then Buf.str(b, ", "); else Buf.str(b, " "); end;
120130 Fmt.write(b, "--{}", [Fmt.str(long)]);
121131 end;
122- if LEN(value) > 0 then Fmt.write(b, " {}", [Fmt.str(value)]); end;
132+ if optional(value) and (LEN(long) > 0) then Fmt.write(b, "[={}", [Fmt.str(value[1..LEN(value)])]);
133+ elsif optional(value) then Buf.str(b, value);
134+ elsif LEN(value) > 0 then Fmt.write(b, " {}", [Fmt.str(value)]);
135+ end;
123136 | else:
124137 end;
125138 end;
@@ -243,6 +256,8 @@
243256 mistake(p, "--{} takes no value", [Fmt.str(p^.spelt)]);
244257 elsif eq >= 0 then
245258 p^.value := w[eq + 1..LEN(w)];
259+ elsif optional(val) then
260+ (* left out: the next word is not its value *)
246261 elsif LEN(val) > 0 then
247262 if p^.at >= ARGC() then
248263 if quiet then return readEnd; end;
@@ -270,6 +285,8 @@
270285 if LEN(p^.group) > 0 then
271286 p^.value := p^.group;
272287 p^.group := "";
288+ elsif optional(val) then
289+ (* left out *)
273290 elsif p^.at < ARGC() then
274291 p^.value := Files.arg(p^.at);
275292 INC(p^.at);
@@ -311,12 +328,15 @@
311328 match p^.table[i] of
312329 | sub(_s, name, _help):
313330 if LEN(name) = 0 then badTable(p, i, "a subcommand needs a name"); end;
314- | option(s, letter, long, _v, _help):
331+ | option(s, letter, long, value, _help):
315332 if (letter < '!') and (LEN(long) = 0) then
316333 badTable(p, i, "an option needs a letter, or a key below ! and a long name");
317334 end;
318335 if letter = '-' then badTable(p, i, "no option is -"); end;
319336 if Str.find(long, "=") >= 0 then badTable(p, i, "a long name has no ="); end;
337+ if (LEN(value) > 0) and ((value[0] = '[') or (value[LEN(value) - 1] = ']')) and not optional(value) then
338+ badTable(p, i, "a value that may be left out is written [name]");
339+ end;
320340 if long = "help" then badTable(p, i, "--help is Opt's"); end;
321341 for j := 0 to i - 1 do
322342 match p^.table[j] of
@@ -484,12 +504,13 @@
484504 sub("", "remote", "the remotes"),
485505 sub("remote", "add", "add one"),
486506 option("remote.add", 'f', "force", "", "even if it is there"),
487- option("remote.add", 'h', "here", "", "only here")]);
507+ option("remote.add", 'h', "here", "", "only here"),
508+ option("remote.add", 'c', "color", "[when]", "colour it: always, never or auto")]);
488509 _w, _ok := operand(ADR(p));
489510 scope(ADR(p), "remote");
490511 _w, _ok := operand(ADR(p));
491512 scope(ADR(p), "remote.add");
492- while next(ADR(p)) do Fmt.print("{} {}\n", [Fmt.char(p.letter), Fmt.str(p.spelt)]); end;
513+ while next(ADR(p)) do Fmt.print("{} {}={}\n", [Fmt.char(p.letter), Fmt.str(p.spelt), Fmt.str(p.value)]); end;
493514 end;
494515
495516 type Case = record
@@ -576,11 +597,15 @@
576597 {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},
577598 {mode: k, args: ["keep", "--jobs", "9223372036854775807", "start", "n", "c"], out: "job n: c\n"},
578599 {mode: "OPTTEST=bad", args: ["bad", "-v"], err: "bad: option table, row 2: the option is in the scope already\n", status: 1},
579- {mode: "OPTTEST=plain", args: ["plain", "remote", "add", "-f", "--force"], out: "f f\nf force\n"},
600+ {mode: "OPTTEST=plain", args: ["plain", "remote", "add", "-f", "--force"], out: "f f=\nf force=\n"},
580601 {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"},
602+ {mode: "OPTTEST=plain", args: ["plain", "remote", "add", "-hf"], out: "h h=\nf f=\n"},
603+ {mode: "OPTTEST=plain", args: ["plain", "remote", "add", "-c", "-f"], out: "c c=\nf f=\n"},
604+ {mode: "OPTTEST=plain", args: ["plain", "remote", "add", "-calways", "-fcnever"], out: "c c=always\nf f=\nc c=never\n"},
605+ {mode: "OPTTEST=plain", args: ["plain", "remote", "add", "--color", "-f", "--color=auto"], out: "c color=\nf f=\nc color=auto\n"},
606+ {mode: "OPTTEST=plain", args: ["plain", "remote", "add", "--color", "always", "-f"], out: "c color=\n"},
582607 {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"},
608+ 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"},
584609 {mode: "OPTTEST=plain", args: ["plain", "remote", "-h"], out: "usage: plain remote\n\n add add one\n -h, --help print this and exit\n"}];
585610 for i := 0 to LEN(cases) - 1 do run(cases[i], i); end;
586611 end;