(* A command line read through one table, by the rules of the C library's getopt with GNU's long options (report, volume 2). The program describes its synopsis lines, subcommands and options once, in a slice of Row; the help and the messages for mistakes are made from it, so they can't drift from it. Depends on Files for the words, and writes the help and the messages itself, then ends the program. Its test uses Check. Leaves out: options after operands (GNU's reordering), and completion for concha. *) module Opt; import Files, Str, Buf, Fmt, Heap, Sys, Rt, Check; type Row* = variant usage(scope, line: []const U8); (* a synopsis, without "usage: " *) sub(scope, name, help: []const U8); (* a subcommand *) option(scope: []const U8; letter: U8; long, value, help: []const U8); end; Parser* = record letter*: U8; (* the option just read *) value*: []const U8; (* its value; "" for a flag *) long*: BOOL; (* it was written by its long name *) spelt*: []const U8; (* as it was written, no dashes: "b" or "bar"; the program's to read only *) (* the rest are Opt's own *) name: []const U8; (* the program's, as the messages begin *) table: []const Row; scope: []const U8; at: INT; (* the next word *) group: []const U8; (* the letters left of a word of them, as -lw *) ended: BOOL; (* the scope's options have ended *) help: BOOL; (* the option just read is -h or --help *) end; (* What read makes of a word or a letter. *) const read1 = 1; readEnd = 0; readSkip = -1; const largest = 9223372036854775807; (* the largest I64, a number with no top *) (* b written whole to fd, then freed. *) proc say(fd: INT; var b: Buf.Buf); var s: []const U8; n: INT; begin s := Buf.bytes(b); while LEN(s) > 0 do n := Sys.write(fd, s); if n <= 0 then break; end; s := s[n..LEN(s)]; end; Buf.done(b); end; proc inScope(const p: Parser; r: Row): BOOL; begin match r of | usage(s, _line): return s = p.scope; | sub(s, _name, _help): return s = p.scope; | option(s, _l, _long, _v, _help): return s = p.scope; end; end; (* The scope's synopsis lines, as the help and the mistakes begin: "usage: " and the first, the others under it; or, for a scope with none, "usage: keep start". *) proc usageOf(const p: Parser; var b: Buf.Buf); var i, lines: INT; c: U8; begin for i := 0 to LEN(p.table) - 1 do match p.table[i] of | usage(s, line): if s = p.scope then if lines = 0 then Buf.str(b, "usage: "); else Buf.str(b, " "); end; Fmt.write(b, "{}\n", [Fmt.str(line)]); INC(lines); end; | else: end; end; if lines = 0 then Fmt.write(b, "usage: {}", [Fmt.str(p.name)]); if LEN(p.scope) > 0 then Buf.add(b, ' '); end; for i := 0 to LEN(p.scope) - 1 do c := p.scope[i]; if c = '.' then c := ' '; end; Buf.add(b, c); end; Buf.add(b, 10); end; end; (* A mistake on the command line: the sentence f, the scope's synopsis lines under it, and status 2. *) proc mistake(const p: Parser; f: []const U8; args: []const Fmt.Arg); var b: Buf.Buf; begin Buf.init(b, Heap.heap); Fmt.write(b, "{}: ", [Fmt.str(p.name)]); Fmt.write(b, f, args); Buf.add(b, 10); usageOf(p, b); say(Sys.stderr, b); Rt.exit(2); end; proc dashes(const p: Parser): []const U8; begin if p.long then return "--"; end; return "-"; end; (* Whether an option's value may be left out, written [name] in its row: given only in the option's own word, --color=when or -cwhen, never as the next word, as getopt's :: has it. *) proc optional(value: []const U8): BOOL; begin return (LEN(value) > 2) and (value[0] = '[') and (value[LEN(value) - 1] = ']'); end; (* The left column of a row in the help, as "-b, --bar label", or, for a value that may be left out, "-c, --color[=when]", as grep's help has it. *) proc left(var b: Buf.Buf; const r: Row); begin match r of | sub(_s, name, _help): Buf.str(b, name); | option(_s, letter, long, value, _help): if letter >= '!' then Fmt.write(b, "-{}", [Fmt.char(letter)]); else Buf.str(b, " "); end; if LEN(long) > 0 then if letter >= '!' then Buf.str(b, ", "); else Buf.str(b, " "); end; Fmt.write(b, "--{}", [Fmt.str(long)]); end; if optional(value) and (LEN(long) > 0) then Fmt.write(b, "[={}", [Fmt.str(value[1..LEN(value)])]); elsif optional(value) then Buf.str(b, value); elsif LEN(value) > 0 then Fmt.write(b, " {}", [Fmt.str(value)]); end; | else: end; end; proc leftWidth(const r: Row): INT; var b: Buf.Buf; n: INT; begin Buf.init(b, Heap.heap); left(b, r); n := LEN(Buf.bytes(b)); Buf.done(b); return n; end; (* A line of the list: the left column, padded to width and three spaces more, and the line of help. *) proc item(var b: Buf.Buf; const r: Row; width: INT); var i: INT; begin Buf.str(b, " "); left(b, r); for i := leftWidth(r) to width + 2 do Buf.add(b, ' '); 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)]); | else: 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(const p: Parser; c: U8; long: []const U8): (INT, U8, []const 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(const p: Parser): BOOL; var r: INT; _l: U8; _v: []const 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. The help's own row is made as the table's are, with no letter if the scope has -h. *) proc printHelp(const p: Parser); var b: Buf.Buf; i, width: INT; 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(b, Heap.heap); usageOf(p, b); Buf.add(b, 10); width := leftWidth(own); for i := 0 to LEN(p.table) - 1 do if inScope(p, p.table[i]) and (leftWidth(p.table[i]) > width) then width := leftWidth(p.table[i]); end; end; for i := 0 to LEN(p.table) - 1 do match p.table[i] of | sub(s, _name, _help): if s = p.scope then item(b, p.table[i], width); end; | else: end; end; for i := 0 to LEN(p.table) - 1 do match p.table[i] of | option(s, _l, _long, _v, _help): if s = p.scope then item(b, p.table[i], width); end; | else: end; end; item(b, own, width); say(Sys.stdout, b); Rt.exit(0); end; (* Whether the scope has an option whose letter is a digit, as head's -5 is; if not, a word like -33.87 is an operand. *) proc hasDigitOption(const p: Parser): BOOL; var i: INT; begin for i := 0 to LEN(p.table) - 1 do match p.table[i] of | option(s, letter, _long, _v, _help): if (s = p.scope) and (letter >= '0') and (letter <= '9') then return true; end; | else: end; end; return false; 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. *) proc read(var p: Parser; quiet: BOOL): INT; var w, val: []const U8; r, eq: INT; c: U8; begin p.help := false; p.value := ""; if LEN(p.group) = 0 then if p.ended or (p.at >= ARGC()) then p.ended := true; return readEnd; end; w := Files.arg(p.at); if (LEN(w) < 2) or (w[0] # '-') then p.ended := true; return readEnd; end; (* an operand *) if (w[1] >= '0') and (w[1] <= '9') and not hasDigitOption(p) then (* a negative number *) p.ended := true; return readEnd; end; INC(p.at); if w = "--" then p.ended := true; return readEnd; end; if w[1] # '-' then p.group := w[1..LEN(w)]; else eq := Str.find(w, "="); if eq < 0 then p.spelt := w[2..LEN(w)]; else p.spelt := w[2..eq]; end; p.long := true; if p.spelt = "help" then p.help := true; return read1; end; r, p.letter, val := find(p, 0, p.spelt); if r < 0 then if quiet then return readSkip; end; mistake(p, "unknown option --{}", [Fmt.str(p.spelt)]); end; if (eq >= 0) and (LEN(val) = 0) then if quiet then return readSkip; end; mistake(p, "--{} takes no value", [Fmt.str(p.spelt)]); elsif eq >= 0 then p.value := w[eq + 1..LEN(w)]; elsif optional(val) then (* left out: the next word is not its value *) elsif LEN(val) > 0 then if p.at >= ARGC() then if quiet then return readEnd; end; mistake(p, "--{} needs a {}", [Fmt.str(p.spelt), Fmt.str(val)]); end; p.value := Files.arg(p.at); INC(p.at); end; return read1; end; end; (* a letter of a group *) c := p.group[0]; p.spelt := p.group[0..1]; p.group := p.group[1..LEN(p.group)]; p.long := false; p.letter := c; 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)]); end; if LEN(val) > 0 then if LEN(p.group) > 0 then p.value := p.group; p.group := ""; elsif optional(val) then (* left out *) elsif p.at < ARGC() then p.value := Files.arg(p.at); INC(p.at); else if quiet then return readEnd; end; mistake(p, "-{} needs a {}", [Fmt.str(p.spelt), Fmt.str(val)]); end; end; return read1; end; (* -h or --help among the scope's options is answered before the program sees one: a copy of the parser reads them, passing over mistakes. *) proc answerHelp(const p: Parser); var q: Parser; k: INT; begin q := p; repeat k := read(q, true); if q.help and (k = read1) then printHelp(p); end; until k = readEnd; end; (* A mistake in the table: the program's, with status 1. *) proc badTable(const p: Parser; row: INT; why: []const U8); var b: Buf.Buf; begin Buf.init(b, Heap.heap); Fmt.write(b, "{}: option table, row {}: {}\n", [Fmt.str(p.name), Fmt.int(row), Fmt.str(why)]); say(Sys.stderr, b); Rt.exit(1); end; proc check(const p: Parser); var i, j: INT; begin for i := 0 to LEN(p.table) - 1 do match p.table[i] of | sub(_s, name, _help): if LEN(name) = 0 then badTable(p, i, "a subcommand needs a name"); end; | option(s, letter, long, value, _help): if (letter < '!') and (LEN(long) = 0) then badTable(p, i, "an option needs a letter, or a key below ! and a long name"); 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 (LEN(value) > 0) and ((value[0] = '[') or (value[LEN(value) - 1] = ']')) and not optional(value) then badTable(p, i, "a value that may be left out is written [name]"); 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): if (s2 = s) and ((letter2 = letter) or (LEN(long) > 0) and (long2 = long)) then badTable(p, i, "the option is in the scope already"); end; | else: end; end; | else: end; end; end; (* p reads the arguments from 1 on, by table, in the program's own scope; name begins the messages. *) proc init*(var p: Parser; name: []const U8; table: []const Row); begin p := {name: name, table: table, at: 1}; check(p); answerHelp(p); end; (* The next option, in p.letter, p.value and p.long; false when the scope's options end. *) proc next*(var p: Parser): BOOL; begin if read(p, false) = readEnd then return false; end; if p.help then printHelp(p); end; return true; end; (* The next operand, after what options are left; false when there are no more. *) proc operand*(var p: Parser): ([]const U8, BOOL); begin while next(p) do end; if p.at >= ARGC() then return "", false; end; INC(p.at); return Files.arg(p.at - 1), true; end; (* The number of the argument operand would give next, after what options are left: the operands from there on are Files.arg(i), for i up to ARGC() - 1, as an interpreter hands them to its script. *) proc at*(var p: Parser): INT; begin while next(p) do end; return p.at; end; (* The options of the subcommand name, from the word after its own, which operand has given. *) proc scope*(var p: Parser; name: []const U8); begin p.scope := name; p.ended := false; p.group := ""; answerHelp(p); end; (* The value of the option just read, as a whole number from lo to hi; anything else is a mistake. A number with no top is given the largest I64 for hi, and the mistake says "from lo up". *) proc number*(const p: Parser; lo, hi: I64): I64; var n: I64; ok: BOOL; begin n, ok := Str.toInt(p.value); if not ok or (n < lo) or (n > hi) then if hi = largest then mistake(p, "{}{} wants a number from {} up, not '{}'", [Fmt.str(dashes(p)), Fmt.str(p.spelt), Fmt.int(lo), Fmt.str(p.value)]); end; mistake(p, "{}{} wants a number from {} to {}, not '{}'", [Fmt.str(dashes(p)), Fmt.str(p.spelt), Fmt.int(lo), Fmt.int(hi), Fmt.str(p.value)]); end; return n; end; (* The value of the option just read refused, as number refuses one: "-b wants wants, not 'value'", the option as it was written, with the scope's synopsis lines, and status 2. *) proc refuse*(const p: Parser; wants: []const U8); begin mistake(p, "{}{} wants {}, not '{}'", [Fmt.str(dashes(p)), Fmt.str(p.spelt), Fmt.str(wants), Fmt.str(p.value)]); end; (* The program's own mistake, written as Opt's are but alone, and status 2. *) proc fail*(const p: Parser; text: []const U8); var b: Buf.Buf; begin Buf.init(b, Heap.heap); Fmt.write(b, "{}: {}\n", [Fmt.str(p.name), Fmt.str(text)]); say(Sys.stderr, b); Rt.exit(2); end; (* The program's own mistake, written as Opt's are, with the scope's synopsis lines after it, and status 2. *) proc misuse*(const p: Parser; text: []const U8); begin mistake(p, "{}", [Fmt.str(text)]); end; (* The module's test. main runs itself once for each case, with the case's words and OPTTEST in its environment, which makes it keep, the program of getopt's page, or a program with a bad table, or one with no synopsis lines; and holds each to what it writes, and its status. *) proc keep; const wait = 1; jobs = 2; Rows: []Row = [ (* a constant table; badTableTest and plain write theirs in the call *) usage("", "keep [-v] [--wait secs] start ..."), sub("", "start", "start a job"), option("", 'v', "verbose", "", "say more"), option("", wait, "wait", "secs", "give the job secs to start"), option("", jobs, "jobs", "n", "keep n jobs at most"), usage("start", "keep start [-b label] name command [argument ...]"), option("start", 'b', "bar", "label", "show the job in the bar as label")]; var p: Parser; verbose, ok: BOOL; secs: I64; bar, job, w: []const U8; b: Buf.Buf; i: INT; begin secs := 10; init(p, "keep", Rows); while next(p) do if p.letter = 'v' then verbose := true; elsif p.letter = wait then secs := number(p, 1, 600); elsif p.letter = jobs then _ := number(p, 1, largest); end; end; w, ok := operand(p); if not ok then fail(p, "no command; see keep -h"); end; if w # "start" then Buf.init(b, Heap.heap); Fmt.write(b, "no command {}; see keep -h", [Fmt.str(w)]); fail(p, Buf.bytes(b)); end; scope(p, "start"); while next(p) do if (p.letter = 'b') and (Str.find(p.value, " ") >= 0) then refuse(p, "a label with no spaces"); elsif p.letter = 'b' then bar := p.value; end; end; job, ok := operand(p); (* the command and its arguments, as they are, by at *) if not ok or (at(p) >= ARGC()) then fail(p, "start needs a name and a command; see keep start -h"); end; if verbose then Fmt.print("waiting {}s\n", [Fmt.int(secs)]); end; Fmt.print("job {}", [Fmt.str(job)]); if LEN(bar) > 0 then Fmt.print(" [{}]", [Fmt.str(bar)]); end; Fmt.print(":", []); for i := at(p) to ARGC() - 1 do Fmt.print(" {}", [Fmt.str(Files.arg(i))]); end; Fmt.print("\n", []); end; (* A scope with no digit option, and one with -5. *) proc digits; var p: Parser; w: []const U8; ok: BOOL; n: INT; begin init(p, "digits", [ usage("", "digits [-v] x y"), option("", 'v', "", "", "say more"), sub("", "five", "the scope with -5"), option("five", '5', "", "", "five")]); while next(p) do Fmt.print("{}\n", [Fmt.char(p.letter)]); end; w, ok := operand(p); if ok and (w = "five") then scope(p, "five"); while next(p) do Fmt.print("{}\n", [Fmt.char(p.letter)]); end; w, ok := operand(p); end; while ok do INC(n); Fmt.print("operand {}\n", [Fmt.str(w)]); w, ok := operand(p); end; if n = 0 then misuse(p, "no operands"); end; end; proc badTableTest; var p: Parser; begin init(p, "bad", [ option("", 'v', "verbose", "", "say more"), option("", 'q', "quiet", "", "say less"), option("", 'v', "loud", "", "say it again")]); end; (* Subcommands of subcommands, no synopsis lines, and a -h of the program's own. *) proc plain; var p: Parser; _w: []const U8; _ok: BOOL; begin init(p, "plain", [ sub("", "remote", "the remotes"), sub("remote", "add", "add one"), option("remote.add", 'f', "force", "", "even if it is there"), option("remote.add", 'h', "here", "", "only here"), option("remote.add", 'c', "color", "[when]", "colour it: always, never or auto")]); _w, _ok := operand(p); scope(p, "remote"); _w, _ok := operand(p); scope(p, "remote.add"); while next(p) do Fmt.print("{} {}={}\n", [Fmt.char(p.letter), Fmt.str(p.spelt), Fmt.str(p.value)]); end; end; type Case = record mode: []const U8; (* OPTTEST=... *) args: [][]const U8; out, err: []const U8; status: INT; end; (* Everything left to read on fd, into buf: how much. *) proc readAll(fd: INT; buf: []U8): INT; var n, k: INT; begin repeat k := Sys.read(fd, buf[n..LEN(buf)]); if k > 0 then INC(n, k); end; until k <= 0; Sys.close(fd); return n; end; (* Runs one case in a child and checks how it ended and what it wrote. *) proc run(c: Case; n: INT); var outR, outW, errR, errW, pid, status, no, ne: INT; st: Sys.Status; out, err: [4096]U8; what: Buf.Buf; begin outR, outW := Sys.pipe(); errR, errW := Sys.pipe(); (* this program again, by the path it was run by, which has a slash in it: raztest gives one, and Linux's /proc/self/exe is no Mac's *) pid := Sys.spawn({path: Files.arg(0), args: c.args, env: [c.mode], fds: [0, outW, errW]}); Sys.close(outW); Sys.close(errW); if pid < 0 then Rt.stop("Opt: can't run the test's cases"); end; pid, st, status := Sys.wait(pid, true); no := readAll(outR, out); ne := readAll(errR, err); Buf.init(what, Heap.heap); Fmt.write(what, "Opt case {}", [Fmt.int(n)]); Check.ok(st = Sys.exited, "Opt: the child did not exit"); Check.int(Buf.bytes(what), status, c.status); Check.str(Buf.bytes(what), out[0..no], c.out); Check.str(Buf.bytes(what), err[0..ne], c.err); Buf.done(what); end; proc main*; const keepHelp = "usage: keep [-v] [--wait secs] start ...\n\n start start a job\n -v, --verbose say more\n --wait secs give the job secs to start\n --jobs n keep n jobs at most\n -h, --help print this and exit\n"; startHelp = "usage: keep start [-b label] name command [argument ...]\n\n -b, --bar label show the job in the bar as label\n -h, --help print this and exit\n"; k = "OPTTEST=keep"; var cases: []Case; i: INT; mode: []const U8; begin mode := Sys.env("OPTTEST"); if mode = "keep" then keep; return; elsif mode = "bad" then badTableTest; return; elsif mode = "plain" then plain; return; elsif mode = "digits" then digits; return; end; cases := [ {mode: k, args: ["keep", "-v", "--wait=30", "start", "-b", "web", "site", "python", "-m", "http.server", "--port", "8000"], out: "waiting 30s\njob site [web]: python -m http.server --port 8000\n"}, {mode: k, args: ["keep", "--wait", "30", "-vv", "start", "--bar=web", "site", "x"], out: "waiting 30s\njob site [web]: x\n"}, {mode: k, args: ["keep", "start", "-bweb", "n", "c"], out: "job n [web]: c\n"}, {mode: k, args: ["keep", "start", "-b", "", "n", "c"], out: "job n: c\n"}, {mode: k, args: ["keep", "start", "-b", "-x", "n", "c"], out: "job n [-x]: c\n"}, {mode: k, args: ["keep", "start", "--", "-n", "ls", "-l"], out: "job -n: ls -l\n"}, {mode: k, args: ["keep", "start", "-", "c", "--", "-h"], out: "job -: c -- -h\n"}, {mode: k, args: ["keep", "start", "n", "c", "-b", "x"], out: "job n: c -b x\n"}, {mode: k, args: ["keep", "start", "-bh", "n", "c"], out: "job n [h]: c\n"}, {mode: k, args: ["keep", "start", "-b", "x", "n"], err: "keep: start needs a name and a command; see keep start -h\n", status: 2}, {mode: k, args: ["keep", "-h"], out: keepHelp}, {mode: k, args: ["keep", "-v", "--help", "-x"], out: keepHelp}, {mode: k, args: ["keep", "-vh"], out: keepHelp}, {mode: k, args: ["keep", "start", "-x", "-h"], out: startHelp}, {mode: k, args: ["keep"], err: "keep: no command; see keep -h\n", status: 2}, {mode: k, args: ["keep", "stop"], err: "keep: no command stop; see keep -h\n", status: 2}, {mode: k, args: ["keep", "start", "-x"], err: "keep: unknown option -x\nusage: keep start [-b label] name command [argument ...]\n", status: 2}, {mode: k, args: ["keep", "--verb", "start"], err: "keep: unknown option --verb\nusage: keep [-v] [--wait secs] start ...\n", status: 2}, {mode: k, args: ["keep", "--bar=x", "start"], err: "keep: unknown option --bar\nusage: keep [-v] [--wait secs] start ...\n", status: 2}, {mode: k, args: ["keep", "--verbose=x"], err: "keep: --verbose takes no value\nusage: keep [-v] [--wait secs] start ...\n", status: 2}, {mode: k, args: ["keep", "start", "-b"], err: "keep: -b needs a label\nusage: keep start [-b label] name command [argument ...]\n", status: 2}, {mode: k, args: ["keep", "--wait"], err: "keep: --wait needs a secs\nusage: keep [-v] [--wait secs] start ...\n", status: 2}, {mode: k, args: ["keep", "--wait", "0", "start", "a", "b"], err: "keep: --wait wants a number from 1 to 600, not '0'\nusage: keep [-v] [--wait secs] start ...\n", status: 2}, {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}, {mode: k, args: ["keep", "start", "-b", "a b", "n", "c"], err: "keep: -b wants a label with no spaces, not 'a b'\nusage: keep start [-b label] name command [argument ...]\n", status: 2}, {mode: k, args: ["keep", "start", "--bar=a b", "n", "c"], err: "keep: --bar wants a label with no spaces, not 'a b'\nusage: keep start [-b label] name command [argument ...]\n", status: 2}, {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}, {mode: k, args: ["keep", "--jobs", "9223372036854775807", "start", "n", "c"], out: "job n: c\n"}, {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", "-c", "-f"], out: "c c=\nf f=\n"}, {mode: "OPTTEST=plain", args: ["plain", "remote", "add", "-calways", "-fcnever"], out: "c c=always\nf f=\nc c=never\n"}, {mode: "OPTTEST=plain", args: ["plain", "remote", "add", "--color", "-f", "--color=auto"], out: "c color=\nf f=\nc color=auto\n"}, {mode: "OPTTEST=plain", args: ["plain", "remote", "add", "--color", "always", "-f"], out: "c color=\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 -c, --color[=when] colour it: always, never or auto\n --help print this and exit\n"}, {mode: "OPTTEST=digits", args: ["digits", "-33.87", "151.21"], out: "operand -33.87\noperand 151.21\n"}, {mode: "OPTTEST=digits", args: ["digits", "-v", "-5", "-v"], out: "v\noperand -5\noperand -v\n"}, {mode: "OPTTEST=digits", args: ["digits", "five", "-5", "x"], out: "5\noperand x\n"}, {mode: "OPTTEST=digits", args: ["digits", "-x"], err: "digits: unknown option -x\nusage: digits [-v] x y\n", status: 2}, {mode: "OPTTEST=digits", args: ["digits"], err: "digits: no operands\nusage: digits [-v] x y\n", status: 2}, {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; end Opt.