module Opt; 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. 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; type 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; proc init*(var p: Parser; name: []const U8; table: []const Row); p reads the arguments from 1 on, by table, in the program's own scope; name begins the messages. proc next*(var p: Parser): BOOL; The next option, in p.letter, p.value and p.long; false when the scope's options end. proc operand*(var p: Parser): ([]const U8, BOOL); The next operand, after what options are left; false when there are no more. proc at*(var p: Parser): INT; 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 scope*(var p: Parser; name: []const U8); The options of the subcommand name, from the word after its own, which operand has given. proc number*(const p: Parser; lo, hi: I64): I64; 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 refuse*(const p: Parser; wants: []const U8); 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 fail*(const p: Parser; text: []const U8); The program's own mistake, written as Opt's are but alone, and status 2. proc misuse*(const p: Parser; text: []const U8); The program's own mistake, written as Opt's are, with the scope's synopsis lines after it, and status 2. proc main*;