From 6970dbf9b6fb15118c3b844c66ebc2f2314ce0a7 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Fri, 16 Oct 2026 20:18:45 +0000 Subject: [PATCH] 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. --- diff --git a/PROPOSALS.md b/PROPOSALS.md index 714b667..aaf0705 100644 --- a/PROPOSALS.md +++ b/PROPOSALS.md @@ -629,15 +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. -## `-h` for a program that wants it - -`Opt` takes `-h` and `--help` for itself, and a table that names -either is the program's mistake ("-h and --help are Opt's"). grep's -`-h`, no file names, can't be written: rre, which follows grep, has -`--no-filename` without a letter. The proposal: a table may give `-h` -to an option of its own, and then help is `--help` only, as it is for -grep. It breaks no program, since no table names `-h` now. - ## An option whose value may be left out An option takes a value always or never. grep's `--color` is diff --git a/doc/raz-lib.md b/doc/raz-lib.md index 574234f..a7292f5 100644 --- a/doc/raz-lib.md +++ b/doc/raz-lib.md @@ -908,6 +908,9 @@ A scope with several synopsis lines has the others indented under the first; one with none, `usage:` and its words, as `usage: keep start`. +A scope may give `-h` to an option of its own, as grep's "no file +names"; help there is `--help` alone, and its row in the help has no +letter. A mistake on the command line is a sentence on standard error, the program's name first, then the scope's synopsis lines, as the help @@ -934,8 +937,8 @@ the table before it reads a word, and ends the program with status 1: `keep: option table, row 3: the option is in the scope already`. The others are an option with neither a letter nor a long name, a long -name with `=` in it, a letter `-`, and `-h` or `--help` in the table, -since they are `Opt`'s. +name with `=` in it, a letter `-`, and `--help` in the table, since +it is `Opt`'s. **Width** says how many columns of a terminal text takes, for a program with a screen that wraps its lines and cuts them at the diff --git a/lib/Opt.raz b/lib/Opt.raz index 27cefc3..f9ffeb3 100644 --- a/lib/Opt.raz +++ b/lib/Opt.raz @@ -6,8 +6,8 @@ Depends on Files for the words, and writes the help and the messages itself, then ends the program. - Leaves out: options after operands (GNU's reordering), a program - that keeps -h for itself, and completion for concha. *) + Leaves out: options after operands (GNU's reordering), and + completion for concha. *) module Opt; import Files, Str, Buf, Fmt, Heap, Out, Sys, Rt; @@ -108,11 +108,9 @@ return "-"; end; -(* The left column of a row in the help, as "-b, --bar label"; the - help's own row if r is nil. *) +(* The left column of a row in the help, as "-b, --bar label". *) proc left(b: ^Buf.Buf; r: ^Row); begin - if r = nil then Buf.str(b, "-h, --help"); return; end; match r^ of | sub(_s, name, _help): Buf.str(b, name); | option(_s, letter, long, value, _help): @@ -144,7 +142,6 @@ Buf.str(b, " "); left(b, r); for i := leftWidth(r) to width + 2 do Buf.add(b, ' '); end; - if r = nil then Buf.str(b, "print this and exit\n"); return; end; match r^ of | sub(_s, _name, help): Fmt.write(b, "{}\n", [Fmt.str(help)]); | option(_s, _l, _long, _v, help): Fmt.write(b, "{}\n", [Fmt.str(help)]); @@ -152,15 +149,46 @@ end; end; +(* The scope's option with the letter c, or with the long name long: + its row, or -1, and its letter and the name of its value. *) +proc find(p: ^Parser; c: U8; long: []U8): (INT, U8, []U8); +var i: INT; +begin + for i := 0 to LEN(p^.table) - 1 do + match p^.table[i] of + | option(s, letter, lg, value, _help): + if (s = p^.scope) and ((c >= '!') and (letter = c) or (LEN(long) > 0) and (lg = long)) then + return i, letter, value; + end; + | else: + end; + end; + return -1, 0, ""; +end; + +(* Whether the scope has an option -h of its own, so that help is + --help alone there, as it is for grep, whose -h is "no file names". *) +proc ownsH(p: ^Parser): BOOL; +var r: INT; _l: U8; _v: []U8; +begin + r, _l, _v := find(p, 'h', ""); + return r >= 0; +end; + (* The scope's help: its synopsis lines, then its subcommands, its - options and the help itself, each with its line; and status 0. *) + options and the help itself, each with its line; and status 0. The + help's own row is made as the table's are, with no letter if the + scope has -h. *) proc printHelp(p: ^Parser); -var b: Buf.Buf; i, width: INT; r: ^Row; +var b: Buf.Buf; i, width: INT; r: ^Row; own: Row; h: U8; begin + h := 'h'; + if ownsH(p) then h := 0; end; + own := option(p^.scope, h, "help", "", "print this and exit"); Buf.init(ADR(b), Heap.heap); usageOf(p, ADR(b)); Buf.add(ADR(b), 10); - width := leftWidth(nil); + width := leftWidth(ADR(own)); for i := 0 to LEN(p^.table) - 1 do r := ADR(p^.table[i]); if inScope(p, r^) and (leftWidth(r) > width) then width := leftWidth(r); end; @@ -179,28 +207,11 @@ | else: end; end; - item(ADR(b), nil, width); + item(ADR(b), ADR(own), width); say(Sys.stdout, ADR(b)); Rt.exit(0); end; -(* The scope's option with the letter c, or with the long name long: - its row, or -1, and its letter and the name of its value. *) -proc find(p: ^Parser; c: U8; long: []U8): (INT, U8, []U8); -var i: INT; -begin - for i := 0 to LEN(p^.table) - 1 do - match p^.table[i] of - | option(s, letter, lg, value, _help): - if (s = p^.scope) and ((c >= '!') and (letter = c) or (LEN(long) > 0) and (lg = long)) then - return i, letter, value; - end; - | else: - end; - end; - return -1, 0, ""; -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. *) @@ -249,8 +260,8 @@ p^.group := p^.group[1..LEN(p^.group)]; p^.long := false; p^.letter := c; - if c = 'h' then p^.help := true; return read1; end; r, p^.letter, val := find(p, c, ""); + if (c = 'h') and (r < 0) then p^.help := true; return read1; end; if r < 0 then if quiet then return readSkip; end; mistake(p, "unknown option -{}", [Fmt.str(p^.spelt)]); @@ -306,7 +317,7 @@ end; if letter = '-' then badTable(p, i, "no option is -"); end; if Str.find(long, "=") >= 0 then badTable(p, i, "a long name has no ="); end; - if (letter = 'h') or (long = "help") then badTable(p, i, "-h and --help are Opt's"); end; + if long = "help" then badTable(p, i, "--help is Opt's"); end; for j := 0 to i - 1 do match p^.table[j] of | option(s2, letter2, long2, _v2, _help2): @@ -464,14 +475,16 @@ option("", 'v', "loud", "", "say it again")]); end; -(* Subcommands of subcommands, and no synopsis lines. *) +(* Subcommands of subcommands, no synopsis lines, and a -h of the + program's own. *) proc plain; var p: Parser; _w: []U8; _ok: BOOL; begin init(ADR(p), "plain", [ sub("", "remote", "the remotes"), sub("remote", "add", "add one"), - option("remote.add", 'f', "force", "", "even if it is there")]); + option("remote.add", 'f', "force", "", "even if it is there"), + option("remote.add", 'h', "here", "", "only here")]); _w, _ok := operand(ADR(p)); scope(ADR(p), "remote"); _w, _ok := operand(ADR(p)); @@ -565,6 +578,9 @@ {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 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", "add", "-hf"], out: "h h\nf f\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 --help print this and exit\n"}, {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 2f88867..bb40d78 100644 --- a/test/doc/Opt.expect +++ b/test/doc/Opt.expect @@ -7,8 +7,8 @@ Depends on Files for the words, and writes the help and the messages itself, then ends the program. - Leaves out: options after operands (GNU's reordering), a program - that keeps -h for itself, and completion for concha. + Leaves out: options after operands (GNU's reordering), and + completion for concha. type Row* = variant usage(scope, line: []U8); (* a synopsis, without "usage: " *)