| 1 | (* A command line read through one table, by the rules of the C |
| 2 | library's getopt with GNU's long options (report, volume 2). The |
| 3 | program describes its synopsis lines, subcommands and options once, |
| 4 | in a slice of Row; the help and the messages for mistakes are made |
| 5 | from it, so they can't drift from it. |
| 6 | |
| 7 | Depends on Files for the words, and writes the help and the |
| 8 | messages itself, then ends the program. Its test uses Check. |
| 9 | Leaves out: options after operands (GNU's reordering), and |
| 10 | completion for concha. *) |
| 11 | module Opt; |
| 12 | import Files, Str, Buf, Fmt, Heap, Sys, Rt, Check; |
| 13 | |
| 14 | type |
| 15 | Row* = variant |
| 16 | usage(scope, line: []const U8); (* a synopsis, without "usage: " *) |
| 17 | sub(scope, name, help: []const U8); (* a subcommand *) |
| 18 | option(scope: []const U8; letter: U8; long, value, help: []const U8); |
| 19 | end; |
| 20 | Parser* = record |
| 21 | letter*: U8; (* the option just read *) |
| 22 | value*: []const U8; (* its value; "" for a flag *) |
| 23 | long*: BOOL; (* it was written by its long name *) |
| 24 | spelt*: []const U8; (* as it was written, no dashes: "b" or "bar"; the program's to read only *) |
| 25 | (* the rest are Opt's own *) |
| 26 | name: []const U8; (* the program's, as the messages begin *) |
| 27 | table: []const Row; |
| 28 | scope: []const U8; |
| 29 | at: INT; (* the next word *) |
| 30 | group: []const U8; (* the letters left of a word of them, as -lw *) |
| 31 | ended: BOOL; (* the scope's options have ended *) |
| 32 | help: BOOL; (* the option just read is -h or --help *) |
| 33 | end; |
| 34 | |
| 35 | (* What read makes of a word or a letter. *) |
| 36 | const read1 = 1; readEnd = 0; readSkip = -1; |
| 37 | |
| 38 | const largest = 9223372036854775807; (* the largest I64, a number with no top *) |
| 39 | |
| 40 | (* b written whole to fd, then freed. *) |
| 41 | proc say(fd: INT; var b: Buf.Buf); |
| 42 | var s: []const U8; n: INT; |
| 43 | begin |
| 44 | s := Buf.bytes(b); |
| 45 | while LEN(s) > 0 do |
| 46 | n := Sys.write(fd, s); |
| 47 | if n <= 0 then break; end; |
| 48 | s := s[n..LEN(s)]; |
| 49 | end; |
| 50 | Buf.done(b); |
| 51 | end; |
| 52 | |
| 53 | proc inScope(const p: Parser; r: Row): BOOL; |
| 54 | begin |
| 55 | match r of |
| 56 | | usage(s, _line): return s = p.scope; |
| 57 | | sub(s, _name, _help): return s = p.scope; |
| 58 | | option(s, _l, _long, _v, _help): return s = p.scope; |
| 59 | end; |
| 60 | end; |
| 61 | |
| 62 | (* The scope's synopsis lines, as the help and the mistakes begin: |
| 63 | "usage: " and the first, the others under it; or, for a scope with |
| 64 | none, "usage: keep start". *) |
| 65 | proc usageOf(const p: Parser; var b: Buf.Buf); |
| 66 | var i, lines: INT; c: U8; |
| 67 | begin |
| 68 | for i := 0 to LEN(p.table) - 1 do |
| 69 | match p.table[i] of |
| 70 | | usage(s, line): |
| 71 | if s = p.scope then |
| 72 | if lines = 0 then Buf.str(b, "usage: "); else Buf.str(b, " "); end; |
| 73 | Fmt.write(b, "{}\n", [Fmt.str(line)]); |
| 74 | INC(lines); |
| 75 | end; |
| 76 | | else: |
| 77 | end; |
| 78 | end; |
| 79 | if lines = 0 then |
| 80 | Fmt.write(b, "usage: {}", [Fmt.str(p.name)]); |
| 81 | if LEN(p.scope) > 0 then Buf.add(b, ' '); end; |
| 82 | for i := 0 to LEN(p.scope) - 1 do |
| 83 | c := p.scope[i]; |
| 84 | if c = '.' then c := ' '; end; |
| 85 | Buf.add(b, c); |
| 86 | end; |
| 87 | Buf.add(b, 10); |
| 88 | end; |
| 89 | end; |
| 90 | |
| 91 | (* A mistake on the command line: the sentence f, the scope's synopsis |
| 92 | lines under it, and status 2. *) |
| 93 | proc mistake(const p: Parser; f: []const U8; args: []const Fmt.Arg); |
| 94 | var b: Buf.Buf; |
| 95 | begin |
| 96 | Buf.init(b, Heap.heap); |
| 97 | Fmt.write(b, "{}: ", [Fmt.str(p.name)]); |
| 98 | Fmt.write(b, f, args); |
| 99 | Buf.add(b, 10); |
| 100 | usageOf(p, b); |
| 101 | say(Sys.stderr, b); |
| 102 | Rt.exit(2); |
| 103 | end; |
| 104 | |
| 105 | proc dashes(const p: Parser): []const U8; |
| 106 | begin |
| 107 | if p.long then return "--"; end; |
| 108 | return "-"; |
| 109 | end; |
| 110 | |
| 111 | (* Whether an option's value may be left out, written [name] in its |
| 112 | row: given only in the option's own word, --color=when or -cwhen, |
| 113 | never as the next word, as getopt's :: has it. *) |
| 114 | proc optional(value: []const U8): BOOL; |
| 115 | begin |
| 116 | return (LEN(value) > 2) and (value[0] = '[') and (value[LEN(value) - 1] = ']'); |
| 117 | end; |
| 118 | |
| 119 | (* The left column of a row in the help, as "-b, --bar label", or, for |
| 120 | a value that may be left out, "-c, --color[=when]", as grep's help |
| 121 | has it. *) |
| 122 | proc left(var b: Buf.Buf; const r: Row); |
| 123 | begin |
| 124 | match r of |
| 125 | | sub(_s, name, _help): Buf.str(b, name); |
| 126 | | option(_s, letter, long, value, _help): |
| 127 | if letter >= '!' then Fmt.write(b, "-{}", [Fmt.char(letter)]); else Buf.str(b, " "); end; |
| 128 | if LEN(long) > 0 then |
| 129 | if letter >= '!' then Buf.str(b, ", "); else Buf.str(b, " "); end; |
| 130 | Fmt.write(b, "--{}", [Fmt.str(long)]); |
| 131 | end; |
| 132 | if optional(value) and (LEN(long) > 0) then Fmt.write(b, "[={}", [Fmt.str(value[1..LEN(value)])]); |
| 133 | elsif optional(value) then Buf.str(b, value); |
| 134 | elsif LEN(value) > 0 then Fmt.write(b, " {}", [Fmt.str(value)]); |
| 135 | end; |
| 136 | | else: |
| 137 | end; |
| 138 | end; |
| 139 | |
| 140 | proc leftWidth(const r: Row): INT; |
| 141 | var b: Buf.Buf; n: INT; |
| 142 | begin |
| 143 | Buf.init(b, Heap.heap); |
| 144 | left(b, r); |
| 145 | n := LEN(Buf.bytes(b)); |
| 146 | Buf.done(b); |
| 147 | return n; |
| 148 | end; |
| 149 | |
| 150 | (* A line of the list: the left column, padded to width and three |
| 151 | spaces more, and the line of help. *) |
| 152 | proc item(var b: Buf.Buf; const r: Row; width: INT); |
| 153 | var i: INT; |
| 154 | begin |
| 155 | Buf.str(b, " "); |
| 156 | left(b, r); |
| 157 | for i := leftWidth(r) to width + 2 do Buf.add(b, ' '); end; |
| 158 | match r of |
| 159 | | sub(_s, _name, help): Fmt.write(b, "{}\n", [Fmt.str(help)]); |
| 160 | | option(_s, _l, _long, _v, help): Fmt.write(b, "{}\n", [Fmt.str(help)]); |
| 161 | | else: |
| 162 | end; |
| 163 | end; |
| 164 | |
| 165 | (* The scope's option with the letter c, or with the long name long: |
| 166 | its row, or -1, and its letter and the name of its value. *) |
| 167 | proc find(const p: Parser; c: U8; long: []const U8): (INT, U8, []const U8); |
| 168 | var i: INT; |
| 169 | begin |
| 170 | for i := 0 to LEN(p.table) - 1 do |
| 171 | match p.table[i] of |
| 172 | | option(s, letter, lg, value, _help): |
| 173 | if (s = p.scope) and ((c >= '!') and (letter = c) or (LEN(long) > 0) and (lg = long)) then |
| 174 | return i, letter, value; |
| 175 | end; |
| 176 | | else: |
| 177 | end; |
| 178 | end; |
| 179 | return -1, 0, ""; |
| 180 | end; |
| 181 | |
| 182 | (* Whether the scope has an option -h of its own, so that help is |
| 183 | --help alone there, as it is for grep, whose -h is "no file names". *) |
| 184 | proc ownsH(const p: Parser): BOOL; |
| 185 | var r: INT; _l: U8; _v: []const U8; |
| 186 | begin |
| 187 | r, _l, _v := find(p, 'h', ""); |
| 188 | return r >= 0; |
| 189 | end; |
| 190 | |
| 191 | (* The scope's help: its synopsis lines, then its subcommands, its |
| 192 | options and the help itself, each with its line; and status 0. The |
| 193 | help's own row is made as the table's are, with no letter if the |
| 194 | scope has -h. *) |
| 195 | proc printHelp(const p: Parser); |
| 196 | var b: Buf.Buf; i, width: INT; own: Row; h: U8; |
| 197 | begin |
| 198 | h := 'h'; |
| 199 | if ownsH(p) then h := 0; end; |
| 200 | own := option(p.scope, h, "help", "", "print this and exit"); |
| 201 | Buf.init(b, Heap.heap); |
| 202 | usageOf(p, b); |
| 203 | Buf.add(b, 10); |
| 204 | width := leftWidth(own); |
| 205 | for i := 0 to LEN(p.table) - 1 do |
| 206 | if inScope(p, p.table[i]) and (leftWidth(p.table[i]) > width) then width := leftWidth(p.table[i]); end; |
| 207 | end; |
| 208 | for i := 0 to LEN(p.table) - 1 do |
| 209 | match p.table[i] of |
| 210 | | sub(s, _name, _help): if s = p.scope then item(b, p.table[i], width); end; |
| 211 | | else: |
| 212 | end; |
| 213 | end; |
| 214 | for i := 0 to LEN(p.table) - 1 do |
| 215 | match p.table[i] of |
| 216 | | option(s, _l, _long, _v, _help): if s = p.scope then item(b, p.table[i], width); end; |
| 217 | | else: |
| 218 | end; |
| 219 | end; |
| 220 | item(b, own, width); |
| 221 | say(Sys.stdout, b); |
| 222 | Rt.exit(0); |
| 223 | end; |
| 224 | |
| 225 | (* Whether the scope has an option whose letter is a digit, as head's |
| 226 | -5 is; if not, a word like -33.87 is an operand. *) |
| 227 | proc hasDigitOption(const p: Parser): BOOL; |
| 228 | var i: INT; |
| 229 | begin |
| 230 | for i := 0 to LEN(p.table) - 1 do |
| 231 | match p.table[i] of |
| 232 | | option(s, letter, _long, _v, _help): |
| 233 | if (s = p.scope) and (letter >= '0') and (letter <= '9') then return true; end; |
| 234 | | else: |
| 235 | end; |
| 236 | end; |
| 237 | return false; |
| 238 | end; |
| 239 | |
| 240 | (* The next option, a word or a letter of one, into p: read1, or |
| 241 | readEnd at the end of the options. A mistake is refused, or, if |
| 242 | quiet, passed over, as readSkip. *) |
| 243 | proc read(var p: Parser; quiet: BOOL): INT; |
| 244 | var w, val: []const U8; r, eq: INT; c: U8; |
| 245 | begin |
| 246 | p.help := false; |
| 247 | p.value := ""; |
| 248 | if LEN(p.group) = 0 then |
| 249 | if p.ended or (p.at >= ARGC()) then p.ended := true; return readEnd; end; |
| 250 | w := Files.arg(p.at); |
| 251 | if (LEN(w) < 2) or (w[0] # '-') then p.ended := true; return readEnd; end; (* an operand *) |
| 252 | if (w[1] >= '0') and (w[1] <= '9') and not hasDigitOption(p) then (* a negative number *) |
| 253 | p.ended := true; |
| 254 | return readEnd; |
| 255 | end; |
| 256 | INC(p.at); |
| 257 | if w = "--" then p.ended := true; return readEnd; end; |
| 258 | if w[1] # '-' then |
| 259 | p.group := w[1..LEN(w)]; |
| 260 | else |
| 261 | eq := Str.find(w, "="); |
| 262 | if eq < 0 then p.spelt := w[2..LEN(w)]; else p.spelt := w[2..eq]; end; |
| 263 | p.long := true; |
| 264 | if p.spelt = "help" then p.help := true; return read1; end; |
| 265 | r, p.letter, val := find(p, 0, p.spelt); |
| 266 | if r < 0 then |
| 267 | if quiet then return readSkip; end; |
| 268 | mistake(p, "unknown option --{}", [Fmt.str(p.spelt)]); |
| 269 | end; |
| 270 | if (eq >= 0) and (LEN(val) = 0) then |
| 271 | if quiet then return readSkip; end; |
| 272 | mistake(p, "--{} takes no value", [Fmt.str(p.spelt)]); |
| 273 | elsif eq >= 0 then |
| 274 | p.value := w[eq + 1..LEN(w)]; |
| 275 | elsif optional(val) then |
| 276 | (* left out: the next word is not its value *) |
| 277 | elsif LEN(val) > 0 then |
| 278 | if p.at >= ARGC() then |
| 279 | if quiet then return readEnd; end; |
| 280 | mistake(p, "--{} needs a {}", [Fmt.str(p.spelt), Fmt.str(val)]); |
| 281 | end; |
| 282 | p.value := Files.arg(p.at); |
| 283 | INC(p.at); |
| 284 | end; |
| 285 | return read1; |
| 286 | end; |
| 287 | end; |
| 288 | (* a letter of a group *) |
| 289 | c := p.group[0]; |
| 290 | p.spelt := p.group[0..1]; |
| 291 | p.group := p.group[1..LEN(p.group)]; |
| 292 | p.long := false; |
| 293 | p.letter := c; |
| 294 | r, p.letter, val := find(p, c, ""); |
| 295 | if (c = 'h') and (r < 0) then p.help := true; return read1; end; |
| 296 | if r < 0 then |
| 297 | if quiet then return readSkip; end; |
| 298 | mistake(p, "unknown option -{}", [Fmt.str(p.spelt)]); |
| 299 | end; |
| 300 | if LEN(val) > 0 then |
| 301 | if LEN(p.group) > 0 then |
| 302 | p.value := p.group; |
| 303 | p.group := ""; |
| 304 | elsif optional(val) then |
| 305 | (* left out *) |
| 306 | elsif p.at < ARGC() then |
| 307 | p.value := Files.arg(p.at); |
| 308 | INC(p.at); |
| 309 | else |
| 310 | if quiet then return readEnd; end; |
| 311 | mistake(p, "-{} needs a {}", [Fmt.str(p.spelt), Fmt.str(val)]); |
| 312 | end; |
| 313 | end; |
| 314 | return read1; |
| 315 | end; |
| 316 | |
| 317 | (* -h or --help among the scope's options is answered before the |
| 318 | program sees one: a copy of the parser reads them, passing over |
| 319 | mistakes. *) |
| 320 | proc answerHelp(const p: Parser); |
| 321 | var q: Parser; k: INT; |
| 322 | begin |
| 323 | q := p; |
| 324 | repeat |
| 325 | k := read(q, true); |
| 326 | if q.help and (k = read1) then printHelp(p); end; |
| 327 | until k = readEnd; |
| 328 | end; |
| 329 | |
| 330 | (* A mistake in the table: the program's, with status 1. *) |
| 331 | proc badTable(const p: Parser; row: INT; why: []const U8); |
| 332 | var b: Buf.Buf; |
| 333 | begin |
| 334 | Buf.init(b, Heap.heap); |
| 335 | Fmt.write(b, "{}: option table, row {}: {}\n", [Fmt.str(p.name), Fmt.int(row), Fmt.str(why)]); |
| 336 | say(Sys.stderr, b); |
| 337 | Rt.exit(1); |
| 338 | end; |
| 339 | |
| 340 | proc check(const p: Parser); |
| 341 | var i, j: INT; |
| 342 | begin |
| 343 | for i := 0 to LEN(p.table) - 1 do |
| 344 | match p.table[i] of |
| 345 | | sub(_s, name, _help): |
| 346 | if LEN(name) = 0 then badTable(p, i, "a subcommand needs a name"); end; |
| 347 | | option(s, letter, long, value, _help): |
| 348 | if (letter < '!') and (LEN(long) = 0) then |
| 349 | badTable(p, i, "an option needs a letter, or a key below ! and a long name"); |
| 350 | end; |
| 351 | if letter = '-' then badTable(p, i, "no option is -"); end; |
| 352 | if Str.find(long, "=") >= 0 then badTable(p, i, "a long name has no ="); end; |
| 353 | if (LEN(value) > 0) and ((value[0] = '[') or (value[LEN(value) - 1] = ']')) and not optional(value) then |
| 354 | badTable(p, i, "a value that may be left out is written [name]"); |
| 355 | end; |
| 356 | if long = "help" then badTable(p, i, "--help is Opt's"); end; |
| 357 | for j := 0 to i - 1 do |
| 358 | match p.table[j] of |
| 359 | | option(s2, letter2, long2, _v2, _help2): |
| 360 | if (s2 = s) and ((letter2 = letter) or (LEN(long) > 0) and (long2 = long)) then |
| 361 | badTable(p, i, "the option is in the scope already"); |
| 362 | end; |
| 363 | | else: |
| 364 | end; |
| 365 | end; |
| 366 | | else: |
| 367 | end; |
| 368 | end; |
| 369 | end; |
| 370 | |
| 371 | (* p reads the arguments from 1 on, by table, in the program's own |
| 372 | scope; name begins the messages. *) |
| 373 | proc init*(var p: Parser; name: []const U8; table: []const Row); |
| 374 | begin |
| 375 | p := {name: name, table: table, at: 1}; |
| 376 | check(p); |
| 377 | answerHelp(p); |
| 378 | end; |
| 379 | |
| 380 | (* The next option, in p.letter, p.value and p.long; false when the |
| 381 | scope's options end. *) |
| 382 | proc next*(var p: Parser): BOOL; |
| 383 | begin |
| 384 | if read(p, false) = readEnd then return false; end; |
| 385 | if p.help then printHelp(p); end; |
| 386 | return true; |
| 387 | end; |
| 388 | |
| 389 | (* The next operand, after what options are left; false when there are |
| 390 | no more. *) |
| 391 | proc operand*(var p: Parser): ([]const U8, BOOL); |
| 392 | begin |
| 393 | while next(p) do end; |
| 394 | if p.at >= ARGC() then return "", false; end; |
| 395 | INC(p.at); |
| 396 | return Files.arg(p.at - 1), true; |
| 397 | end; |
| 398 | |
| 399 | (* The number of the argument operand would give next, after what |
| 400 | options are left: the operands from there on are Files.arg(i), for |
| 401 | i up to ARGC() - 1, as an interpreter hands them to its script. *) |
| 402 | proc at*(var p: Parser): INT; |
| 403 | begin |
| 404 | while next(p) do end; |
| 405 | return p.at; |
| 406 | end; |
| 407 | |
| 408 | (* The options of the subcommand name, from the word after its own, |
| 409 | which operand has given. *) |
| 410 | proc scope*(var p: Parser; name: []const U8); |
| 411 | begin |
| 412 | p.scope := name; |
| 413 | p.ended := false; |
| 414 | p.group := ""; |
| 415 | answerHelp(p); |
| 416 | end; |
| 417 | |
| 418 | (* The value of the option just read, as a whole number from lo to hi; |
| 419 | anything else is a mistake. A number with no top is given the |
| 420 | largest I64 for hi, and the mistake says "from lo up". *) |
| 421 | proc number*(const p: Parser; lo, hi: I64): I64; |
| 422 | var n: I64; ok: BOOL; |
| 423 | begin |
| 424 | n, ok := Str.toInt(p.value); |
| 425 | if not ok or (n < lo) or (n > hi) then |
| 426 | if hi = largest then |
| 427 | mistake(p, "{}{} wants a number from {} up, not '{}'", |
| 428 | [Fmt.str(dashes(p)), Fmt.str(p.spelt), Fmt.int(lo), Fmt.str(p.value)]); |
| 429 | end; |
| 430 | mistake(p, "{}{} wants a number from {} to {}, not '{}'", |
| 431 | [Fmt.str(dashes(p)), Fmt.str(p.spelt), Fmt.int(lo), Fmt.int(hi), Fmt.str(p.value)]); |
| 432 | end; |
| 433 | return n; |
| 434 | end; |
| 435 | |
| 436 | (* The value of the option just read refused, as number refuses one: |
| 437 | "-b wants wants, not 'value'", the option as it was written, with |
| 438 | the scope's synopsis lines, and status 2. *) |
| 439 | proc refuse*(const p: Parser; wants: []const U8); |
| 440 | begin |
| 441 | mistake(p, "{}{} wants {}, not '{}'", |
| 442 | [Fmt.str(dashes(p)), Fmt.str(p.spelt), Fmt.str(wants), Fmt.str(p.value)]); |
| 443 | end; |
| 444 | |
| 445 | (* The program's own mistake, written as Opt's are but alone, and |
| 446 | status 2. *) |
| 447 | proc fail*(const p: Parser; text: []const U8); |
| 448 | var b: Buf.Buf; |
| 449 | begin |
| 450 | Buf.init(b, Heap.heap); |
| 451 | Fmt.write(b, "{}: {}\n", [Fmt.str(p.name), Fmt.str(text)]); |
| 452 | say(Sys.stderr, b); |
| 453 | Rt.exit(2); |
| 454 | end; |
| 455 | |
| 456 | (* The program's own mistake, written as Opt's are, with the scope's |
| 457 | synopsis lines after it, and status 2. *) |
| 458 | proc misuse*(const p: Parser; text: []const U8); |
| 459 | begin |
| 460 | mistake(p, "{}", [Fmt.str(text)]); |
| 461 | end; |
| 462 | |
| 463 | (* The module's test. main runs itself once for each case, with the |
| 464 | case's words and OPTTEST in its environment, which makes it keep, the |
| 465 | program of getopt's page, or a program with a bad table, or one with |
| 466 | no synopsis lines; and holds each to what it writes, and its status. *) |
| 467 | proc keep; |
| 468 | const |
| 469 | wait = 1; jobs = 2; |
| 470 | Rows: []Row = [ (* a constant table; badTableTest and plain write theirs in the call *) |
| 471 | usage("", "keep [-v] [--wait secs] start ..."), |
| 472 | sub("", "start", "start a job"), |
| 473 | option("", 'v', "verbose", "", "say more"), |
| 474 | option("", wait, "wait", "secs", "give the job secs to start"), |
| 475 | option("", jobs, "jobs", "n", "keep n jobs at most"), |
| 476 | usage("start", "keep start [-b label] name command [argument ...]"), |
| 477 | option("start", 'b', "bar", "label", "show the job in the bar as label")]; |
| 478 | var p: Parser; verbose, ok: BOOL; secs: I64; bar, job, w: []const U8; b: Buf.Buf; i: INT; |
| 479 | begin |
| 480 | secs := 10; |
| 481 | init(p, "keep", Rows); |
| 482 | while next(p) do |
| 483 | if p.letter = 'v' then verbose := true; |
| 484 | elsif p.letter = wait then secs := number(p, 1, 600); |
| 485 | elsif p.letter = jobs then _ := number(p, 1, largest); |
| 486 | end; |
| 487 | end; |
| 488 | w, ok := operand(p); |
| 489 | if not ok then fail(p, "no command; see keep -h"); end; |
| 490 | if w # "start" then |
| 491 | Buf.init(b, Heap.heap); |
| 492 | Fmt.write(b, "no command {}; see keep -h", [Fmt.str(w)]); |
| 493 | fail(p, Buf.bytes(b)); |
| 494 | end; |
| 495 | scope(p, "start"); |
| 496 | while next(p) do |
| 497 | if (p.letter = 'b') and (Str.find(p.value, " ") >= 0) then refuse(p, "a label with no spaces"); |
| 498 | elsif p.letter = 'b' then bar := p.value; |
| 499 | end; |
| 500 | end; |
| 501 | job, ok := operand(p); |
| 502 | (* the command and its arguments, as they are, by at *) |
| 503 | if not ok or (at(p) >= ARGC()) then |
| 504 | fail(p, "start needs a name and a command; see keep start -h"); |
| 505 | end; |
| 506 | if verbose then Fmt.print("waiting {}s\n", [Fmt.int(secs)]); end; |
| 507 | Fmt.print("job {}", [Fmt.str(job)]); |
| 508 | if LEN(bar) > 0 then Fmt.print(" [{}]", [Fmt.str(bar)]); end; |
| 509 | Fmt.print(":", []); |
| 510 | for i := at(p) to ARGC() - 1 do Fmt.print(" {}", [Fmt.str(Files.arg(i))]); end; |
| 511 | Fmt.print("\n", []); |
| 512 | end; |
| 513 | |
| 514 | (* A scope with no digit option, and one with -5. *) |
| 515 | proc digits; |
| 516 | var p: Parser; w: []const U8; ok: BOOL; n: INT; |
| 517 | begin |
| 518 | init(p, "digits", [ |
| 519 | usage("", "digits [-v] x y"), |
| 520 | option("", 'v', "", "", "say more"), |
| 521 | sub("", "five", "the scope with -5"), |
| 522 | option("five", '5', "", "", "five")]); |
| 523 | while next(p) do Fmt.print("{}\n", [Fmt.char(p.letter)]); end; |
| 524 | w, ok := operand(p); |
| 525 | if ok and (w = "five") then |
| 526 | scope(p, "five"); |
| 527 | while next(p) do Fmt.print("{}\n", [Fmt.char(p.letter)]); end; |
| 528 | w, ok := operand(p); |
| 529 | end; |
| 530 | while ok do |
| 531 | INC(n); |
| 532 | Fmt.print("operand {}\n", [Fmt.str(w)]); |
| 533 | w, ok := operand(p); |
| 534 | end; |
| 535 | if n = 0 then misuse(p, "no operands"); end; |
| 536 | end; |
| 537 | |
| 538 | proc badTableTest; |
| 539 | var p: Parser; |
| 540 | begin |
| 541 | init(p, "bad", [ |
| 542 | option("", 'v', "verbose", "", "say more"), |
| 543 | option("", 'q', "quiet", "", "say less"), |
| 544 | option("", 'v', "loud", "", "say it again")]); |
| 545 | end; |
| 546 | |
| 547 | (* Subcommands of subcommands, no synopsis lines, and a -h of the |
| 548 | program's own. *) |
| 549 | proc plain; |
| 550 | var p: Parser; _w: []const U8; _ok: BOOL; |
| 551 | begin |
| 552 | init(p, "plain", [ |
| 553 | sub("", "remote", "the remotes"), |
| 554 | sub("remote", "add", "add one"), |
| 555 | option("remote.add", 'f', "force", "", "even if it is there"), |
| 556 | option("remote.add", 'h', "here", "", "only here"), |
| 557 | option("remote.add", 'c', "color", "[when]", "colour it: always, never or auto")]); |
| 558 | _w, _ok := operand(p); |
| 559 | scope(p, "remote"); |
| 560 | _w, _ok := operand(p); |
| 561 | scope(p, "remote.add"); |
| 562 | while next(p) do Fmt.print("{} {}={}\n", [Fmt.char(p.letter), Fmt.str(p.spelt), Fmt.str(p.value)]); end; |
| 563 | end; |
| 564 | |
| 565 | type Case = record |
| 566 | mode: []const U8; (* OPTTEST=... *) |
| 567 | args: [][]const U8; |
| 568 | out, err: []const U8; |
| 569 | status: INT; |
| 570 | end; |
| 571 | |
| 572 | (* Everything left to read on fd, into buf: how much. *) |
| 573 | proc readAll(fd: INT; buf: []U8): INT; |
| 574 | var n, k: INT; |
| 575 | begin |
| 576 | repeat |
| 577 | k := Sys.read(fd, buf[n..LEN(buf)]); |
| 578 | if k > 0 then INC(n, k); end; |
| 579 | until k <= 0; |
| 580 | Sys.close(fd); |
| 581 | return n; |
| 582 | end; |
| 583 | |
| 584 | (* Runs one case in a child and checks how it ended and what it wrote. *) |
| 585 | proc run(c: Case; n: INT); |
| 586 | var outR, outW, errR, errW, pid, status, no, ne: INT; st: Sys.Status; out, err: [4096]U8; what: Buf.Buf; |
| 587 | begin |
| 588 | outR, outW := Sys.pipe(); |
| 589 | errR, errW := Sys.pipe(); |
| 590 | (* this program again, by the path it was run by, which has a slash in |
| 591 | it: raztest gives one, and Linux's /proc/self/exe is no Mac's *) |
| 592 | pid := Sys.spawn({path: Files.arg(0), args: c.args, env: [c.mode], fds: [0, outW, errW]}); |
| 593 | Sys.close(outW); |
| 594 | Sys.close(errW); |
| 595 | if pid < 0 then Rt.stop("Opt: can't run the test's cases"); end; |
| 596 | pid, st, status := Sys.wait(pid, true); |
| 597 | no := readAll(outR, out); |
| 598 | ne := readAll(errR, err); |
| 599 | Buf.init(what, Heap.heap); |
| 600 | Fmt.write(what, "Opt case {}", [Fmt.int(n)]); |
| 601 | Check.ok(st = Sys.exited, "Opt: the child did not exit"); |
| 602 | Check.int(Buf.bytes(what), status, c.status); |
| 603 | Check.str(Buf.bytes(what), out[0..no], c.out); |
| 604 | Check.str(Buf.bytes(what), err[0..ne], c.err); |
| 605 | Buf.done(what); |
| 606 | end; |
| 607 | |
| 608 | proc main*; |
| 609 | const |
| 610 | 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"; |
| 611 | 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"; |
| 612 | k = "OPTTEST=keep"; |
| 613 | var cases: []Case; i: INT; mode: []const U8; |
| 614 | begin |
| 615 | mode := Sys.env("OPTTEST"); |
| 616 | if mode = "keep" then keep; return; |
| 617 | elsif mode = "bad" then badTableTest; return; |
| 618 | elsif mode = "plain" then plain; return; |
| 619 | elsif mode = "digits" then digits; return; |
| 620 | end; |
| 621 | cases := [ |
| 622 | {mode: k, args: ["keep", "-v", "--wait=30", "start", "-b", "web", "site", "python", "-m", "http.server", "--port", "8000"], |
| 623 | out: "waiting 30s\njob site [web]: python -m http.server --port 8000\n"}, |
| 624 | {mode: k, args: ["keep", "--wait", "30", "-vv", "start", "--bar=web", "site", "x"], out: "waiting 30s\njob site [web]: x\n"}, |
| 625 | {mode: k, args: ["keep", "start", "-bweb", "n", "c"], out: "job n [web]: c\n"}, |
| 626 | {mode: k, args: ["keep", "start", "-b", "", "n", "c"], out: "job n: c\n"}, |
| 627 | {mode: k, args: ["keep", "start", "-b", "-x", "n", "c"], out: "job n [-x]: c\n"}, |
| 628 | {mode: k, args: ["keep", "start", "--", "-n", "ls", "-l"], out: "job -n: ls -l\n"}, |
| 629 | {mode: k, args: ["keep", "start", "-", "c", "--", "-h"], out: "job -: c -- -h\n"}, |
| 630 | {mode: k, args: ["keep", "start", "n", "c", "-b", "x"], out: "job n: c -b x\n"}, |
| 631 | {mode: k, args: ["keep", "start", "-bh", "n", "c"], out: "job n [h]: c\n"}, |
| 632 | {mode: k, args: ["keep", "start", "-b", "x", "n"], err: "keep: start needs a name and a command; see keep start -h\n", status: 2}, |
| 633 | {mode: k, args: ["keep", "-h"], out: keepHelp}, |
| 634 | {mode: k, args: ["keep", "-v", "--help", "-x"], out: keepHelp}, |
| 635 | {mode: k, args: ["keep", "-vh"], out: keepHelp}, |
| 636 | {mode: k, args: ["keep", "start", "-x", "-h"], out: startHelp}, |
| 637 | {mode: k, args: ["keep"], err: "keep: no command; see keep -h\n", status: 2}, |
| 638 | {mode: k, args: ["keep", "stop"], err: "keep: no command stop; see keep -h\n", status: 2}, |
| 639 | {mode: k, args: ["keep", "start", "-x"], err: "keep: unknown option -x\nusage: keep start [-b label] name command [argument ...]\n", status: 2}, |
| 640 | {mode: k, args: ["keep", "--verb", "start"], err: "keep: unknown option --verb\nusage: keep [-v] [--wait secs] start ...\n", status: 2}, |
| 641 | {mode: k, args: ["keep", "--bar=x", "start"], err: "keep: unknown option --bar\nusage: keep [-v] [--wait secs] start ...\n", status: 2}, |
| 642 | {mode: k, args: ["keep", "--verbose=x"], err: "keep: --verbose takes no value\nusage: keep [-v] [--wait secs] start ...\n", status: 2}, |
| 643 | {mode: k, args: ["keep", "start", "-b"], err: "keep: -b needs a label\nusage: keep start [-b label] name command [argument ...]\n", status: 2}, |
| 644 | {mode: k, args: ["keep", "--wait"], err: "keep: --wait needs a secs\nusage: keep [-v] [--wait secs] start ...\n", status: 2}, |
| 645 | {mode: k, args: ["keep", "--wait", "0", "start", "a", "b"], |
| 646 | err: "keep: --wait wants a number from 1 to 600, not '0'\nusage: keep [-v] [--wait secs] start ...\n", status: 2}, |
| 647 | {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}, |
| 648 | {mode: k, args: ["keep", "start", "-b", "a b", "n", "c"], |
| 649 | err: "keep: -b wants a label with no spaces, not 'a b'\nusage: keep start [-b label] name command [argument ...]\n", status: 2}, |
| 650 | {mode: k, args: ["keep", "start", "--bar=a b", "n", "c"], |
| 651 | err: "keep: --bar wants a label with no spaces, not 'a b'\nusage: keep start [-b label] name command [argument ...]\n", status: 2}, |
| 652 | {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}, |
| 653 | {mode: k, args: ["keep", "--jobs", "9223372036854775807", "start", "n", "c"], out: "job n: c\n"}, |
| 654 | {mode: "OPTTEST=bad", args: ["bad", "-v"], err: "bad: option table, row 2: the option is in the scope already\n", status: 1}, |
| 655 | {mode: "OPTTEST=plain", args: ["plain", "remote", "add", "-f", "--force"], out: "f f=\nf force=\n"}, |
| 656 | {mode: "OPTTEST=plain", args: ["plain", "remote", "add", "-x"], err: "plain: unknown option -x\nusage: plain remote add\n", status: 2}, |
| 657 | {mode: "OPTTEST=plain", args: ["plain", "remote", "add", "-hf"], out: "h h=\nf f=\n"}, |
| 658 | {mode: "OPTTEST=plain", args: ["plain", "remote", "add", "-c", "-f"], out: "c c=\nf f=\n"}, |
| 659 | {mode: "OPTTEST=plain", args: ["plain", "remote", "add", "-calways", "-fcnever"], out: "c c=always\nf f=\nc c=never\n"}, |
| 660 | {mode: "OPTTEST=plain", args: ["plain", "remote", "add", "--color", "-f", "--color=auto"], out: "c color=\nf f=\nc color=auto\n"}, |
| 661 | {mode: "OPTTEST=plain", args: ["plain", "remote", "add", "--color", "always", "-f"], out: "c color=\n"}, |
| 662 | {mode: "OPTTEST=plain", args: ["plain", "remote", "add", "-f", "--help"], |
| 663 | 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"}, |
| 664 | {mode: "OPTTEST=digits", args: ["digits", "-33.87", "151.21"], out: "operand -33.87\noperand 151.21\n"}, |
| 665 | {mode: "OPTTEST=digits", args: ["digits", "-v", "-5", "-v"], out: "v\noperand -5\noperand -v\n"}, |
| 666 | {mode: "OPTTEST=digits", args: ["digits", "five", "-5", "x"], out: "5\noperand x\n"}, |
| 667 | {mode: "OPTTEST=digits", args: ["digits", "-x"], err: "digits: unknown option -x\nusage: digits [-v] x y\n", status: 2}, |
| 668 | {mode: "OPTTEST=digits", args: ["digits"], err: "digits: no operands\nusage: digits [-v] x y\n", status: 2}, |
| 669 | {mode: "OPTTEST=plain", args: ["plain", "remote", "-h"], out: "usage: plain remote\n\n add add one\n -h, --help print this and exit\n"}]; |
| 670 | for i := 0 to LEN(cases) - 1 do run(cases[i], i); end; |
| 671 | end; |
| 672 | |
| 673 | end Opt. |