raz / test / doc / Opt.expect

73 lines · 3.0 KiB · raw · history · last change e0d008cd

1module Opt;
2 A command line read through one table, by the rules of the C
3 library's getopt with GNU's long options (report, volume 2). The
4 program describes its synopsis lines, subcommands and options once,
5 in a slice of Row; the help and the messages for mistakes are made
6 from it, so they can't drift from it.
7
8 Depends on Files for the words, and writes the help and the
9 messages itself, then ends the program. Its test uses Check.
10 Leaves out: options after operands (GNU's reordering), and
11 completion for concha.
12
13type Row* = variant
14 usage(scope, line: []const U8); (* a synopsis, without "usage: " *)
15 sub(scope, name, help: []const U8); (* a subcommand *)
16 option(scope: []const U8; letter: U8; long, value, help: []const U8);
17end;
18
19type Parser* = record
20 letter*: U8; (* the option just read *)
21 value*: []const U8; (* its value; "" for a flag *)
22 long*: BOOL; (* it was written by its long name *)
23 spelt*: []const U8; (* as it was written, no dashes: "b" or "bar"; the program's to read only *)
24 (* the rest are Opt's own *)
25 name: []const U8; (* the program's, as the messages begin *)
26 table: []const Row;
27 scope: []const U8;
28 at: INT; (* the next word *)
29 group: []const U8; (* the letters left of a word of them, as -lw *)
30 ended: BOOL; (* the scope's options have ended *)
31 help: BOOL; (* the option just read is -h or --help *)
32end;
33
34proc init*(var p: Parser; name: []const U8; table: []const Row);
35 p reads the arguments from 1 on, by table, in the program's own
36 scope; name begins the messages.
37
38proc next*(var p: Parser): BOOL;
39 The next option, in p.letter, p.value and p.long; false when the
40 scope's options end.
41
42proc operand*(var p: Parser): ([]const U8, BOOL);
43 The next operand, after what options are left; false when there are
44 no more.
45
46proc at*(var p: Parser): INT;
47 The number of the argument operand would give next, after what
48 options are left: the operands from there on are Files.arg(i), for
49 i up to ARGC() - 1, as an interpreter hands them to its script.
50
51proc scope*(var p: Parser; name: []const U8);
52 The options of the subcommand name, from the word after its own,
53 which operand has given.
54
55proc number*(const p: Parser; lo, hi: I64): I64;
56 The value of the option just read, as a whole number from lo to hi;
57 anything else is a mistake. A number with no top is given the
58 largest I64 for hi, and the mistake says "from lo up".
59
60proc refuse*(const p: Parser; wants: []const U8);
61 The value of the option just read refused, as number refuses one:
62 "-b wants wants, not 'value'", the option as it was written, with
63 the scope's synopsis lines, and status 2.
64
65proc fail*(const p: Parser; text: []const U8);
66 The program's own mistake, written as Opt's are but alone, and
67 status 2.
68
69proc misuse*(const p: Parser; text: []const U8);
70 The program's own mistake, written as Opt's are, with the scope's
71 synopsis lines after it, and status 2.
72
73proc main*;