| | @@ -21,13 +21,13 @@ |
| 21 | 21 | letter*: U8; (* the option just read *) |
| 22 | 22 | value*: []U8; (* its value; "" for a flag *) |
| 23 | 23 | long*: BOOL; (* it was written by its long name *) |
| 24 | + spelt*: []U8; (* as it was written, no dashes: "b" or "bar"; the program's to read only *) |
| 24 | 25 | (* the rest are Opt's own *) |
| 25 | 26 | name: []U8; (* the program's, as the messages begin *) |
| 26 | 27 | table: []Row; |
| 27 | 28 | scope: []U8; |
| 28 | 29 | at: INT; (* the next word *) |
| 29 | 30 | group: []U8; (* the letters left of a word of them, as -lw *) |
| 30 | | - spelt: []U8; (* the option just read, as written, no dashes *) |
| 31 | 31 | ended: BOOL; (* the scope's options have ended *) |
| 32 | 32 | help: BOOL; (* the option just read is -h or --help *) |
| 33 | 33 | end; |
| | @@ -90,7 +90,7 @@ |
| 90 | 90 | |
| 91 | 91 | (* A mistake on the command line: the sentence f, the scope's synopsis |
| 92 | 92 | lines under it, and status 2. *) |
| 93 | | -proc refuse(p: ^Parser; f: []U8; args: []Fmt.Arg); |
| 93 | +proc mistake(p: ^Parser; f: []U8; args: []Fmt.Arg); |
| 94 | 94 | var b: Buf.Buf; |
| 95 | 95 | begin |
| 96 | 96 | Buf.init(ADR(b), Heap.heap); |
| | @@ -225,17 +225,17 @@ |
| 225 | 225 | r, p^.letter, val := find(p, 0, p^.spelt); |
| 226 | 226 | if r < 0 then |
| 227 | 227 | if quiet then return readSkip; end; |
| 228 | | - refuse(p, "unknown option --{}", [Fmt.str(p^.spelt)]); |
| 228 | + mistake(p, "unknown option --{}", [Fmt.str(p^.spelt)]); |
| 229 | 229 | end; |
| 230 | 230 | if (eq >= 0) and (LEN(val) = 0) then |
| 231 | 231 | if quiet then return readSkip; end; |
| 232 | | - refuse(p, "--{} takes no value", [Fmt.str(p^.spelt)]); |
| 232 | + mistake(p, "--{} takes no value", [Fmt.str(p^.spelt)]); |
| 233 | 233 | elsif eq >= 0 then |
| 234 | 234 | p^.value := w[eq + 1..LEN(w)]; |
| 235 | 235 | elsif LEN(val) > 0 then |
| 236 | 236 | if p^.at >= ARGC() then |
| 237 | 237 | if quiet then return readEnd; end; |
| 238 | | - refuse(p, "--{} needs a {}", [Fmt.str(p^.spelt), Fmt.str(val)]); |
| 238 | + mistake(p, "--{} needs a {}", [Fmt.str(p^.spelt), Fmt.str(val)]); |
| 239 | 239 | end; |
| 240 | 240 | p^.value := Files.arg(p^.at); |
| 241 | 241 | INC(p^.at); |
| | @@ -253,7 +253,7 @@ |
| 253 | 253 | r, p^.letter, val := find(p, c, ""); |
| 254 | 254 | if r < 0 then |
| 255 | 255 | if quiet then return readSkip; end; |
| 256 | | - refuse(p, "unknown option -{}", [Fmt.str(p^.spelt)]); |
| 256 | + mistake(p, "unknown option -{}", [Fmt.str(p^.spelt)]); |
| 257 | 257 | end; |
| 258 | 258 | if LEN(val) > 0 then |
| 259 | 259 | if LEN(p^.group) > 0 then |
| | @@ -264,7 +264,7 @@ |
| 264 | 264 | INC(p^.at); |
| 265 | 265 | else |
| 266 | 266 | if quiet then return readEnd; end; |
| 267 | | - refuse(p, "-{} needs a {}", [Fmt.str(p^.spelt), Fmt.str(val)]); |
| 267 | + mistake(p, "-{} needs a {}", [Fmt.str(p^.spelt), Fmt.str(val)]); |
| 268 | 268 | end; |
| 269 | 269 | end; |
| 270 | 270 | return read1; |
| | @@ -377,15 +377,24 @@ |
| 377 | 377 | n, ok := Str.toInt(p^.value); |
| 378 | 378 | if not ok or (n < lo) or (n > hi) then |
| 379 | 379 | if hi = largest then |
| 380 | | - refuse(p, "{}{} wants a number from {} up, not '{}'", |
| 380 | + mistake(p, "{}{} wants a number from {} up, not '{}'", |
| 381 | 381 | [Fmt.str(dashes(p)), Fmt.str(p^.spelt), Fmt.int(lo), Fmt.str(p^.value)]); |
| 382 | 382 | end; |
| 383 | | - refuse(p, "{}{} wants a number from {} to {}, not '{}'", |
| 383 | + mistake(p, "{}{} wants a number from {} to {}, not '{}'", |
| 384 | 384 | [Fmt.str(dashes(p)), Fmt.str(p^.spelt), Fmt.int(lo), Fmt.int(hi), Fmt.str(p^.value)]); |
| 385 | 385 | end; |
| 386 | 386 | return n; |
| 387 | 387 | end; |
| 388 | 388 | |
| 389 | +(* The value of the option just read refused, as number refuses one: |
| 390 | + "-b wants wants, not 'value'", the option as it was written, with |
| 391 | + the scope's synopsis lines, and status 2. *) |
| 392 | +proc refuse*(p: ^Parser; wants: []U8); |
| 393 | +begin |
| 394 | + mistake(p, "{}{} wants {}, not '{}'", |
| 395 | + [Fmt.str(dashes(p)), Fmt.str(p^.spelt), Fmt.str(wants), Fmt.str(p^.value)]); |
| 396 | +end; |
| 397 | + |
| 389 | 398 | (* The program's own mistake, written as Opt's are but alone, and |
| 390 | 399 | status 2. *) |
| 391 | 400 | proc fail*(p: ^Parser; text: []U8); |
| | @@ -429,7 +438,9 @@ |
| 429 | 438 | end; |
| 430 | 439 | scope(ADR(p), "start"); |
| 431 | 440 | while next(ADR(p)) do |
| 432 | | - if p.letter = 'b' then bar := p.value; end; |
| 441 | + if (p.letter = 'b') and (Str.find(p.value, " ") >= 0) then refuse(ADR(p), "a label with no spaces"); |
| 442 | + elsif p.letter = 'b' then bar := p.value; |
| 443 | + end; |
| 433 | 444 | end; |
| 434 | 445 | job, ok := operand(ADR(p)); |
| 435 | 446 | (* the command and its arguments, as they are, by at *) |
| | @@ -465,7 +476,7 @@ |
| 465 | 476 | scope(ADR(p), "remote"); |
| 466 | 477 | _w, _ok := operand(ADR(p)); |
| 467 | 478 | scope(ADR(p), "remote.add"); |
| 468 | | - while next(ADR(p)) do Fmt.print("{}\n", [Fmt.char(p.letter)]); end; |
| 479 | + while next(ADR(p)) do Fmt.print("{} {}\n", [Fmt.char(p.letter), Fmt.str(p.spelt)]); end; |
| 469 | 480 | end; |
| 470 | 481 | |
| 471 | 482 | type Case = record |
| | @@ -545,10 +556,14 @@ |
| 545 | 556 | {mode: k, args: ["keep", "--wait", "0", "start", "a", "b"], |
| 546 | 557 | err: "keep: --wait wants a number from 1 to 600, not '0'\nusage: keep [-v] [--wait secs] start ...\n", status: 2}, |
| 547 | 558 | {mode: k, args: ["keep", "--wait=9x", "start"], err: "keep: --wait wants a number from 1 to 600, not '9x'\nusage: keep [-v] [--wait secs] start ...\n", status: 2}, |
| 559 | + {mode: k, args: ["keep", "start", "-b", "a b", "n", "c"], |
| 560 | + err: "keep: -b wants a label with no spaces, not 'a b'\nusage: keep start [-b label] name command [argument ...]\n", status: 2}, |
| 561 | + {mode: k, args: ["keep", "start", "--bar=a b", "n", "c"], |
| 562 | + err: "keep: --bar wants a label with no spaces, not 'a b'\nusage: keep start [-b label] name command [argument ...]\n", status: 2}, |
| 548 | 563 | {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}, |
| 549 | 564 | {mode: k, args: ["keep", "--jobs", "9223372036854775807", "start", "n", "c"], out: "job n: c\n"}, |
| 550 | 565 | {mode: "OPTTEST=bad", args: ["bad", "-v"], err: "bad: option table, row 2: the option is in the scope already\n", status: 1}, |
| 551 | | - {mode: "OPTTEST=plain", args: ["plain", "remote", "add", "-f", "--force"], out: "f\nf\n"}, |
| 566 | + {mode: "OPTTEST=plain", args: ["plain", "remote", "add", "-f", "--force"], out: "f f\nf force\n"}, |
| 552 | 567 | {mode: "OPTTEST=plain", args: ["plain", "remote", "add", "-x"], err: "plain: unknown option -x\nusage: plain remote add\n", status: 2}, |
| 553 | 568 | {mode: "OPTTEST=plain", args: ["plain", "remote", "-h"], out: "usage: plain remote\n\n add add one\n -h, --help print this and exit\n"}]; |
| 554 | 569 | for i := 0 to LEN(cases) - 1 do run(cases[i], i); end; |