| | @@ -225,6 +225,21 @@ |
| 225 | 225 | Rt.exit(0); |
| 226 | 226 | end; |
| 227 | 227 | |
| 228 | +(* Whether the scope has an option whose letter is a digit, as head's |
| 229 | + -5 is; if not, a word like -33.87 is an operand. *) |
| 230 | +proc hasDigitOption(const p: Parser): BOOL; |
| 231 | +var i: INT; |
| 232 | +begin |
| 233 | + for i := 0 to LEN(p.table) - 1 do |
| 234 | + match p.table[i] of |
| 235 | + | option(s, letter, _long, _v, _help): |
| 236 | + if (s = p.scope) and (letter >= '0') and (letter <= '9') then return true; end; |
| 237 | + | else: |
| 238 | + end; |
| 239 | + end; |
| 240 | + return false; |
| 241 | +end; |
| 242 | + |
| 228 | 243 | (* The next option, a word or a letter of one, into p: read1, or |
| 229 | 244 | readEnd at the end of the options. A mistake is refused, or, if |
| 230 | 245 | quiet, passed over, as readSkip. *) |
| | @@ -237,6 +252,10 @@ |
| 237 | 252 | if p.ended or (p.at >= ARGC()) then p.ended := true; return readEnd; end; |
| 238 | 253 | w := Files.arg(p.at); |
| 239 | 254 | if (LEN(w) < 2) or (w[0] # '-') then p.ended := true; return readEnd; end; (* an operand *) |
| 255 | + if (w[1] >= '0') and (w[1] <= '9') and not hasDigitOption(p) then (* a negative number *) |
| 256 | + p.ended := true; |
| 257 | + return readEnd; |
| 258 | + end; |
| 240 | 259 | INC(p.at); |
| 241 | 260 | if w = "--" then p.ended := true; return readEnd; end; |
| 242 | 261 | if w[1] # '-' then |
| | @@ -437,6 +456,13 @@ |
| 437 | 456 | Rt.exit(2); |
| 438 | 457 | end; |
| 439 | 458 | |
| 459 | +(* The program's own mistake, written as Opt's are, with the scope's |
| 460 | + synopsis lines after it, and status 2. *) |
| 461 | +proc misuse*(const p: Parser; text: []U8); |
| 462 | +begin |
| 463 | + mistake(p, "{}", [Fmt.str(text)]); |
| 464 | +end; |
| 465 | + |
| 440 | 466 | (* The module's test. main runs itself once for each case, with the |
| 441 | 467 | case's words and OPTTEST in its environment, which makes it keep, the |
| 442 | 468 | program of getopt's page, or a program with a bad table, or one with |
| | @@ -488,6 +514,30 @@ |
| 488 | 514 | Fmt.print("\n", []); |
| 489 | 515 | end; |
| 490 | 516 | |
| 517 | +(* A scope with no digit option, and one with -5. *) |
| 518 | +proc digits; |
| 519 | +var p: Parser; w: []U8; ok: BOOL; n: INT; |
| 520 | +begin |
| 521 | + init(p, "digits", [ |
| 522 | + usage("", "digits [-v] x y"), |
| 523 | + option("", 'v', "", "", "say more"), |
| 524 | + sub("", "five", "the scope with -5"), |
| 525 | + option("five", '5', "", "", "five")]); |
| 526 | + while next(p) do Fmt.print("{}\n", [Fmt.char(p.letter)]); end; |
| 527 | + w, ok := operand(p); |
| 528 | + if ok and (w = "five") then |
| 529 | + scope(p, "five"); |
| 530 | + while next(p) do Fmt.print("{}\n", [Fmt.char(p.letter)]); end; |
| 531 | + w, ok := operand(p); |
| 532 | + end; |
| 533 | + while ok do |
| 534 | + INC(n); |
| 535 | + Fmt.print("operand {}\n", [Fmt.str(w)]); |
| 536 | + w, ok := operand(p); |
| 537 | + end; |
| 538 | + if n = 0 then misuse(p, "no operands"); end; |
| 539 | +end; |
| 540 | + |
| 491 | 541 | proc badTableTest; |
| 492 | 542 | var p: Parser; |
| 493 | 543 | begin |
| | @@ -567,6 +617,7 @@ |
| 567 | 617 | if mode = "keep" then keep; return; |
| 568 | 618 | elsif mode = "bad" then badTableTest; return; |
| 569 | 619 | elsif mode = "plain" then plain; return; |
| 620 | + elsif mode = "digits" then digits; return; |
| 570 | 621 | end; |
| 571 | 622 | cases := [ |
| 572 | 623 | {mode: k, args: ["keep", "-v", "--wait=30", "start", "-b", "web", "site", "python", "-m", "http.server", "--port", "8000"], |
| | @@ -611,6 +662,11 @@ |
| 611 | 662 | {mode: "OPTTEST=plain", args: ["plain", "remote", "add", "--color", "always", "-f"], out: "c color=\n"}, |
| 612 | 663 | {mode: "OPTTEST=plain", args: ["plain", "remote", "add", "-f", "--help"], |
| 613 | 664 | 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"}, |
| 665 | + {mode: "OPTTEST=digits", args: ["digits", "-33.87", "151.21"], out: "operand -33.87\noperand 151.21\n"}, |
| 666 | + {mode: "OPTTEST=digits", args: ["digits", "-v", "-5", "-v"], out: "v\noperand -5\noperand -v\n"}, |
| 667 | + {mode: "OPTTEST=digits", args: ["digits", "five", "-5", "x"], out: "5\noperand x\n"}, |
| 668 | + {mode: "OPTTEST=digits", args: ["digits", "-x"], err: "digits: unknown option -x\nusage: digits [-v] x y\n", status: 2}, |
| 669 | + {mode: "OPTTEST=digits", args: ["digits"], err: "digits: no operands\nusage: digits [-v] x y\n", status: 2}, |
| 614 | 670 | {mode: "OPTTEST=plain", args: ["plain", "remote", "-h"], out: "usage: plain remote\n\n add add one\n -h, --help print this and exit\n"}]; |
| 615 | 671 | for i := 0 to LEN(cases) - 1 do run(cases[i], i); end; |
| 616 | 672 | end; |