| | @@ -108,7 +108,17 @@ |
| 108 | 108 | return "-"; |
| 109 | 109 | end; |
| 110 | 110 | |
| 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. *) |
| 112 | 122 | proc left(b: ^Buf.Buf; r: ^Row); |
| 113 | 123 | begin |
| 114 | 124 | match r^ of |
| | @@ -119,7 +129,10 @@ |
| 119 | 129 | if letter >= '!' then Buf.str(b, ", "); else Buf.str(b, " "); end; |
| 120 | 130 | Fmt.write(b, "--{}", [Fmt.str(long)]); |
| 121 | 131 | 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; |
| 123 | 136 | | else: |
| 124 | 137 | end; |
| 125 | 138 | end; |
| | @@ -243,6 +256,8 @@ |
| 243 | 256 | mistake(p, "--{} takes no value", [Fmt.str(p^.spelt)]); |
| 244 | 257 | elsif eq >= 0 then |
| 245 | 258 | p^.value := w[eq + 1..LEN(w)]; |
| 259 | + elsif optional(val) then |
| 260 | + (* left out: the next word is not its value *) |
| 246 | 261 | elsif LEN(val) > 0 then |
| 247 | 262 | if p^.at >= ARGC() then |
| 248 | 263 | if quiet then return readEnd; end; |
| | @@ -270,6 +285,8 @@ |
| 270 | 285 | if LEN(p^.group) > 0 then |
| 271 | 286 | p^.value := p^.group; |
| 272 | 287 | p^.group := ""; |
| 288 | + elsif optional(val) then |
| 289 | + (* left out *) |
| 273 | 290 | elsif p^.at < ARGC() then |
| 274 | 291 | p^.value := Files.arg(p^.at); |
| 275 | 292 | INC(p^.at); |
| | @@ -311,12 +328,15 @@ |
| 311 | 328 | match p^.table[i] of |
| 312 | 329 | | sub(_s, name, _help): |
| 313 | 330 | 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): |
| 315 | 332 | if (letter < '!') and (LEN(long) = 0) then |
| 316 | 333 | badTable(p, i, "an option needs a letter, or a key below ! and a long name"); |
| 317 | 334 | end; |
| 318 | 335 | if letter = '-' then badTable(p, i, "no option is -"); end; |
| 319 | 336 | 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; |
| 320 | 340 | if long = "help" then badTable(p, i, "--help is Opt's"); end; |
| 321 | 341 | for j := 0 to i - 1 do |
| 322 | 342 | match p^.table[j] of |
| | @@ -484,12 +504,13 @@ |
| 484 | 504 | sub("", "remote", "the remotes"), |
| 485 | 505 | sub("remote", "add", "add one"), |
| 486 | 506 | 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")]); |
| 488 | 509 | _w, _ok := operand(ADR(p)); |
| 489 | 510 | scope(ADR(p), "remote"); |
| 490 | 511 | _w, _ok := operand(ADR(p)); |
| 491 | 512 | 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; |
| 493 | 514 | end; |
| 494 | 515 | |
| 495 | 516 | type Case = record |
| | @@ -576,11 +597,15 @@ |
| 576 | 597 | {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}, |
| 577 | 598 | {mode: k, args: ["keep", "--jobs", "9223372036854775807", "start", "n", "c"], out: "job n: c\n"}, |
| 578 | 599 | {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"}, |
| 580 | 601 | {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"}, |
| 582 | 607 | {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"}, |
| 584 | 609 | {mode: "OPTTEST=plain", args: ["plain", "remote", "-h"], out: "usage: plain remote\n\n add add one\n -h, --help print this and exit\n"}]; |
| 585 | 610 | for i := 0 to LEN(cases) - 1 do run(cases[i], i); end; |
| 586 | 611 | end; |