| | @@ -6,8 +6,8 @@ |
| 6 | 6 | |
| 7 | 7 | Depends on Files for the words, and writes the help and the |
| 8 | 8 | 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. *) |
| 11 | 11 | module Opt; |
| 12 | 12 | import Files, Str, Buf, Fmt, Heap, Out, Sys, Rt; |
| 13 | 13 | |
| | @@ -108,11 +108,9 @@ |
| 108 | 108 | return "-"; |
| 109 | 109 | end; |
| 110 | 110 | |
| 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". *) |
| 113 | 112 | proc left(b: ^Buf.Buf; r: ^Row); |
| 114 | 113 | begin |
| 115 | | - if r = nil then Buf.str(b, "-h, --help"); return; end; |
| 116 | 114 | match r^ of |
| 117 | 115 | | sub(_s, name, _help): Buf.str(b, name); |
| 118 | 116 | | option(_s, letter, long, value, _help): |
| | @@ -144,7 +142,6 @@ |
| 144 | 142 | Buf.str(b, " "); |
| 145 | 143 | left(b, r); |
| 146 | 144 | 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; |
| 148 | 145 | match r^ of |
| 149 | 146 | | sub(_s, _name, help): Fmt.write(b, "{}\n", [Fmt.str(help)]); |
| 150 | 147 | | option(_s, _l, _long, _v, help): Fmt.write(b, "{}\n", [Fmt.str(help)]); |
| | @@ -152,15 +149,46 @@ |
| 152 | 149 | end; |
| 153 | 150 | end; |
| 154 | 151 | |
| 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 | + |
| 155 | 178 | (* 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. *) |
| 157 | 182 | 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; |
| 159 | 184 | begin |
| 185 | + h := 'h'; |
| 186 | + if ownsH(p) then h := 0; end; |
| 187 | + own := option(p^.scope, h, "help", "", "print this and exit"); |
| 160 | 188 | Buf.init(ADR(b), Heap.heap); |
| 161 | 189 | usageOf(p, ADR(b)); |
| 162 | 190 | Buf.add(ADR(b), 10); |
| 163 | | - width := leftWidth(nil); |
| 191 | + width := leftWidth(ADR(own)); |
| 164 | 192 | for i := 0 to LEN(p^.table) - 1 do |
| 165 | 193 | r := ADR(p^.table[i]); |
| 166 | 194 | if inScope(p, r^) and (leftWidth(r) > width) then width := leftWidth(r); end; |
| | @@ -179,28 +207,11 @@ |
| 179 | 207 | | else: |
| 180 | 208 | end; |
| 181 | 209 | end; |
| 182 | | - item(ADR(b), nil, width); |
| 210 | + item(ADR(b), ADR(own), width); |
| 183 | 211 | say(Sys.stdout, ADR(b)); |
| 184 | 212 | Rt.exit(0); |
| 185 | 213 | end; |
| 186 | 214 | |
| 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 | | - |
| 204 | 215 | (* The next option, a word or a letter of one, into p: read1, or |
| 205 | 216 | readEnd at the end of the options. A mistake is refused, or, if |
| 206 | 217 | quiet, passed over, as readSkip. *) |
| | @@ -249,8 +260,8 @@ |
| 249 | 260 | p^.group := p^.group[1..LEN(p^.group)]; |
| 250 | 261 | p^.long := false; |
| 251 | 262 | p^.letter := c; |
| 252 | | - if c = 'h' then p^.help := true; return read1; end; |
| 253 | 263 | r, p^.letter, val := find(p, c, ""); |
| 264 | + if (c = 'h') and (r < 0) then p^.help := true; return read1; end; |
| 254 | 265 | if r < 0 then |
| 255 | 266 | if quiet then return readSkip; end; |
| 256 | 267 | mistake(p, "unknown option -{}", [Fmt.str(p^.spelt)]); |
| | @@ -306,7 +317,7 @@ |
| 306 | 317 | end; |
| 307 | 318 | if letter = '-' then badTable(p, i, "no option is -"); end; |
| 308 | 319 | 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; |
| 310 | 321 | for j := 0 to i - 1 do |
| 311 | 322 | match p^.table[j] of |
| 312 | 323 | | option(s2, letter2, long2, _v2, _help2): |
| | @@ -464,14 +475,16 @@ |
| 464 | 475 | option("", 'v', "loud", "", "say it again")]); |
| 465 | 476 | end; |
| 466 | 477 | |
| 467 | | -(* Subcommands of subcommands, and no synopsis lines. *) |
| 478 | +(* Subcommands of subcommands, no synopsis lines, and a -h of the |
| 479 | + program's own. *) |
| 468 | 480 | proc plain; |
| 469 | 481 | var p: Parser; _w: []U8; _ok: BOOL; |
| 470 | 482 | begin |
| 471 | 483 | init(ADR(p), "plain", [ |
| 472 | 484 | sub("", "remote", "the remotes"), |
| 473 | 485 | 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")]); |
| 475 | 488 | _w, _ok := operand(ADR(p)); |
| 476 | 489 | scope(ADR(p), "remote"); |
| 477 | 490 | _w, _ok := operand(ADR(p)); |
| | @@ -565,6 +578,9 @@ |
| 565 | 578 | {mode: "OPTTEST=bad", args: ["bad", "-v"], err: "bad: option table, row 2: the option is in the scope already\n", status: 1}, |
| 566 | 579 | {mode: "OPTTEST=plain", args: ["plain", "remote", "add", "-f", "--force"], out: "f f\nf force\n"}, |
| 567 | 580 | {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"}, |
| 568 | 584 | {mode: "OPTTEST=plain", args: ["plain", "remote", "-h"], out: "usage: plain remote\n\n add add one\n -h, --help print this and exit\n"}]; |
| 569 | 585 | for i := 0 to LEN(cases) - 1 do run(cases[i], i); end; |
| 570 | 586 | end; |