| | @@ -50,25 +50,25 @@ |
| 50 | 50 | Buf.done(b); |
| 51 | 51 | end; |
| 52 | 52 | |
| 53 | | -proc inScope(p: ^Parser; r: Row): BOOL; |
| 53 | +proc inScope(const p: Parser; r: Row): BOOL; |
| 54 | 54 | begin |
| 55 | 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; |
| 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 | 59 | end; |
| 60 | 60 | end; |
| 61 | 61 | |
| 62 | 62 | (* The scope's synopsis lines, as the help and the mistakes begin: |
| 63 | 63 | "usage: " and the first, the others under it; or, for a scope with |
| 64 | 64 | none, "usage: keep start". *) |
| 65 | | -proc usageOf(p: ^Parser; var b: Buf.Buf); |
| 65 | +proc usageOf(const p: Parser; var b: Buf.Buf); |
| 66 | 66 | var i, lines: INT; c: U8; |
| 67 | 67 | begin |
| 68 | | - for i := 0 to LEN(p^.table) - 1 do |
| 69 | | - match p^.table[i] of |
| 68 | + for i := 0 to LEN(p.table) - 1 do |
| 69 | + match p.table[i] of |
| 70 | 70 | | usage(s, line): |
| 71 | | - if s = p^.scope then |
| 71 | + if s = p.scope then |
| 72 | 72 | if lines = 0 then Buf.str(b, "usage: "); else Buf.str(b, " "); end; |
| 73 | 73 | Fmt.write(b, "{}\n", [Fmt.str(line)]); |
| 74 | 74 | INC(lines); |
| | @@ -77,10 +77,10 @@ |
| 77 | 77 | end; |
| 78 | 78 | end; |
| 79 | 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]; |
| 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 | 84 | if c = '.' then c := ' '; end; |
| 85 | 85 | Buf.add(b, c); |
| 86 | 86 | end; |
| | @@ -90,11 +90,11 @@ |
| 90 | 90 | |
| 91 | 91 | (* A mistake on the command line: the sentence f, the scope's synopsis |
| 92 | 92 | lines under it, and status 2. *) |
| 93 | | -proc mistake(p: ^Parser; f: []U8; args: []Fmt.Arg); |
| 93 | +proc mistake(const p: Parser; f: []U8; args: []Fmt.Arg); |
| 94 | 94 | var b: Buf.Buf; |
| 95 | 95 | begin |
| 96 | 96 | Buf.init(b, Heap.heap); |
| 97 | | - Fmt.write(b, "{}: ", [Fmt.str(p^.name)]); |
| 97 | + Fmt.write(b, "{}: ", [Fmt.str(p.name)]); |
| 98 | 98 | Fmt.write(b, f, args); |
| 99 | 99 | Buf.add(b, 10); |
| 100 | 100 | usageOf(p, b); |
| | @@ -102,9 +102,9 @@ |
| 102 | 102 | Rt.exit(2); |
| 103 | 103 | end; |
| 104 | 104 | |
| 105 | | -proc dashes(p: ^Parser): []U8; |
| 105 | +proc dashes(const p: Parser): []U8; |
| 106 | 106 | begin |
| 107 | | - if p^.long then return "--"; end; |
| 107 | + if p.long then return "--"; end; |
| 108 | 108 | return "-"; |
| 109 | 109 | end; |
| 110 | 110 | |
| | @@ -164,13 +164,13 @@ |
| 164 | 164 | |
| 165 | 165 | (* The scope's option with the letter c, or with the long name long: |
| 166 | 166 | its row, or -1, and its letter and the name of its value. *) |
| 167 | | -proc find(p: ^Parser; c: U8; long: []U8): (INT, U8, []U8); |
| 167 | +proc find(const p: Parser; c: U8; long: []U8): (INT, U8, []U8); |
| 168 | 168 | var i: INT; |
| 169 | 169 | begin |
| 170 | | - for i := 0 to LEN(p^.table) - 1 do |
| 171 | | - match p^.table[i] of |
| 170 | + for i := 0 to LEN(p.table) - 1 do |
| 171 | + match p.table[i] of |
| 172 | 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 |
| 173 | + if (s = p.scope) and ((c >= '!') and (letter = c) or (LEN(long) > 0) and (lg = long)) then |
| 174 | 174 | return i, letter, value; |
| 175 | 175 | end; |
| 176 | 176 | | else: |
| | @@ -181,7 +181,7 @@ |
| 181 | 181 | |
| 182 | 182 | (* Whether the scope has an option -h of its own, so that help is |
| 183 | 183 | --help alone there, as it is for grep, whose -h is "no file names". *) |
| 184 | | -proc ownsH(p: ^Parser): BOOL; |
| 184 | +proc ownsH(const p: Parser): BOOL; |
| 185 | 185 | var r: INT; _l: U8; _v: []U8; |
| 186 | 186 | begin |
| 187 | 187 | r, _l, _v := find(p, 'h', ""); |
| | @@ -192,31 +192,31 @@ |
| 192 | 192 | options and the help itself, each with its line; and status 0. The |
| 193 | 193 | help's own row is made as the table's are, with no letter if the |
| 194 | 194 | scope has -h. *) |
| 195 | | -proc printHelp(p: ^Parser); |
| 195 | +proc printHelp(const p: Parser); |
| 196 | 196 | var b: Buf.Buf; i, width: INT; r: ^Row; own: Row; h: U8; |
| 197 | 197 | begin |
| 198 | 198 | h := 'h'; |
| 199 | 199 | if ownsH(p) then h := 0; end; |
| 200 | | - own := option(p^.scope, h, "help", "", "print this and exit"); |
| 200 | + own := option(p.scope, h, "help", "", "print this and exit"); |
| 201 | 201 | Buf.init(b, Heap.heap); |
| 202 | 202 | usageOf(p, b); |
| 203 | 203 | Buf.add(b, 10); |
| 204 | 204 | width := leftWidth(ADR(own)); |
| 205 | | - for i := 0 to LEN(p^.table) - 1 do |
| 206 | | - r := ADR(p^.table[i]); |
| 205 | + for i := 0 to LEN(p.table) - 1 do |
| 206 | + r := ADR(p.table[i]); |
| 207 | 207 | if inScope(p, r^) and (leftWidth(r) > width) then width := leftWidth(r); end; |
| 208 | 208 | end; |
| 209 | | - for i := 0 to LEN(p^.table) - 1 do |
| 210 | | - r := ADR(p^.table[i]); |
| 209 | + for i := 0 to LEN(p.table) - 1 do |
| 210 | + r := ADR(p.table[i]); |
| 211 | 211 | match r^ of |
| 212 | | - | sub(s, _name, _help): if s = p^.scope then item(b, r, width); end; |
| 212 | + | sub(s, _name, _help): if s = p.scope then item(b, r, width); end; |
| 213 | 213 | | else: |
| 214 | 214 | end; |
| 215 | 215 | end; |
| 216 | | - for i := 0 to LEN(p^.table) - 1 do |
| 217 | | - r := ADR(p^.table[i]); |
| 216 | + for i := 0 to LEN(p.table) - 1 do |
| 217 | + r := ADR(p.table[i]); |
| 218 | 218 | match r^ of |
| 219 | | - | option(s, _l, _long, _v, _help): if s = p^.scope then item(b, r, width); end; |
| 219 | + | option(s, _l, _long, _v, _help): if s = p.scope then item(b, r, width); end; |
| 220 | 220 | | else: |
| 221 | 221 | end; |
| 222 | 222 | end; |
| | @@ -228,71 +228,71 @@ |
| 228 | 228 | (* The next option, a word or a letter of one, into p: read1, or |
| 229 | 229 | readEnd at the end of the options. A mistake is refused, or, if |
| 230 | 230 | quiet, passed over, as readSkip. *) |
| 231 | | -proc read(p: ^Parser; quiet: BOOL): INT; |
| 231 | +proc read(var p: Parser; quiet: BOOL): INT; |
| 232 | 232 | var w, val: []U8; r, eq: INT; c: U8; |
| 233 | 233 | begin |
| 234 | | - p^.help := false; |
| 235 | | - p^.value := ""; |
| 236 | | - if LEN(p^.group) = 0 then |
| 237 | | - if p^.ended or (p^.at >= ARGC()) then p^.ended := true; return readEnd; end; |
| 238 | | - w := Files.arg(p^.at); |
| 239 | | - if (LEN(w) < 2) or (w[0] # '-') then p^.ended := true; return readEnd; end; (* an operand *) |
| 240 | | - INC(p^.at); |
| 241 | | - if w = "--" then p^.ended := true; return readEnd; end; |
| 234 | + p.help := false; |
| 235 | + p.value := ""; |
| 236 | + if LEN(p.group) = 0 then |
| 237 | + if p.ended or (p.at >= ARGC()) then p.ended := true; return readEnd; end; |
| 238 | + w := Files.arg(p.at); |
| 239 | + if (LEN(w) < 2) or (w[0] # '-') then p.ended := true; return readEnd; end; (* an operand *) |
| 240 | + INC(p.at); |
| 241 | + if w = "--" then p.ended := true; return readEnd; end; |
| 242 | 242 | if w[1] # '-' then |
| 243 | | - p^.group := w[1..LEN(w)]; |
| 243 | + p.group := w[1..LEN(w)]; |
| 244 | 244 | else |
| 245 | 245 | eq := Str.find(w, "="); |
| 246 | | - if eq < 0 then p^.spelt := w[2..LEN(w)]; else p^.spelt := w[2..eq]; end; |
| 247 | | - p^.long := true; |
| 248 | | - if p^.spelt = "help" then p^.help := true; return read1; end; |
| 249 | | - r, p^.letter, val := find(p, 0, p^.spelt); |
| 246 | + if eq < 0 then p.spelt := w[2..LEN(w)]; else p.spelt := w[2..eq]; end; |
| 247 | + p.long := true; |
| 248 | + if p.spelt = "help" then p.help := true; return read1; end; |
| 249 | + r, p.letter, val := find(p, 0, p.spelt); |
| 250 | 250 | if r < 0 then |
| 251 | 251 | if quiet then return readSkip; end; |
| 252 | | - mistake(p, "unknown option --{}", [Fmt.str(p^.spelt)]); |
| 252 | + mistake(p, "unknown option --{}", [Fmt.str(p.spelt)]); |
| 253 | 253 | end; |
| 254 | 254 | if (eq >= 0) and (LEN(val) = 0) then |
| 255 | 255 | if quiet then return readSkip; end; |
| 256 | | - mistake(p, "--{} takes no value", [Fmt.str(p^.spelt)]); |
| 256 | + mistake(p, "--{} takes no value", [Fmt.str(p.spelt)]); |
| 257 | 257 | elsif eq >= 0 then |
| 258 | | - p^.value := w[eq + 1..LEN(w)]; |
| 258 | + p.value := w[eq + 1..LEN(w)]; |
| 259 | 259 | elsif optional(val) then |
| 260 | 260 | (* left out: the next word is not its value *) |
| 261 | 261 | elsif LEN(val) > 0 then |
| 262 | | - if p^.at >= ARGC() then |
| 262 | + if p.at >= ARGC() then |
| 263 | 263 | if quiet then return readEnd; end; |
| 264 | | - mistake(p, "--{} needs a {}", [Fmt.str(p^.spelt), Fmt.str(val)]); |
| 264 | + mistake(p, "--{} needs a {}", [Fmt.str(p.spelt), Fmt.str(val)]); |
| 265 | 265 | end; |
| 266 | | - p^.value := Files.arg(p^.at); |
| 267 | | - INC(p^.at); |
| 266 | + p.value := Files.arg(p.at); |
| 267 | + INC(p.at); |
| 268 | 268 | end; |
| 269 | 269 | return read1; |
| 270 | 270 | end; |
| 271 | 271 | end; |
| 272 | 272 | (* a letter of a group *) |
| 273 | | - c := p^.group[0]; |
| 274 | | - p^.spelt := p^.group[0..1]; |
| 275 | | - p^.group := p^.group[1..LEN(p^.group)]; |
| 276 | | - p^.long := false; |
| 277 | | - p^.letter := c; |
| 278 | | - r, p^.letter, val := find(p, c, ""); |
| 279 | | - if (c = 'h') and (r < 0) then p^.help := true; return read1; end; |
| 273 | + c := p.group[0]; |
| 274 | + p.spelt := p.group[0..1]; |
| 275 | + p.group := p.group[1..LEN(p.group)]; |
| 276 | + p.long := false; |
| 277 | + p.letter := c; |
| 278 | + r, p.letter, val := find(p, c, ""); |
| 279 | + if (c = 'h') and (r < 0) then p.help := true; return read1; end; |
| 280 | 280 | if r < 0 then |
| 281 | 281 | if quiet then return readSkip; end; |
| 282 | | - mistake(p, "unknown option -{}", [Fmt.str(p^.spelt)]); |
| 282 | + mistake(p, "unknown option -{}", [Fmt.str(p.spelt)]); |
| 283 | 283 | end; |
| 284 | 284 | if LEN(val) > 0 then |
| 285 | | - if LEN(p^.group) > 0 then |
| 286 | | - p^.value := p^.group; |
| 287 | | - p^.group := ""; |
| 285 | + if LEN(p.group) > 0 then |
| 286 | + p.value := p.group; |
| 287 | + p.group := ""; |
| 288 | 288 | elsif optional(val) then |
| 289 | 289 | (* left out *) |
| 290 | | - elsif p^.at < ARGC() then |
| 291 | | - p^.value := Files.arg(p^.at); |
| 292 | | - INC(p^.at); |
| 290 | + elsif p.at < ARGC() then |
| 291 | + p.value := Files.arg(p.at); |
| 292 | + INC(p.at); |
| 293 | 293 | else |
| 294 | 294 | if quiet then return readEnd; end; |
| 295 | | - mistake(p, "-{} needs a {}", [Fmt.str(p^.spelt), Fmt.str(val)]); |
| 295 | + mistake(p, "-{} needs a {}", [Fmt.str(p.spelt), Fmt.str(val)]); |
| 296 | 296 | end; |
| 297 | 297 | end; |
| 298 | 298 | return read1; |
| | @@ -301,31 +301,31 @@ |
| 301 | 301 | (* -h or --help among the scope's options is answered before the |
| 302 | 302 | program sees one: a copy of the parser reads them, passing over |
| 303 | 303 | mistakes. *) |
| 304 | | -proc answerHelp(p: ^Parser); |
| 304 | +proc answerHelp(const p: Parser); |
| 305 | 305 | var q: Parser; k: INT; |
| 306 | 306 | begin |
| 307 | | - q := p^; |
| 307 | + q := p; |
| 308 | 308 | repeat |
| 309 | | - k := read(ADR(q), true); |
| 309 | + k := read(q, true); |
| 310 | 310 | if q.help and (k = read1) then printHelp(p); end; |
| 311 | 311 | until k = readEnd; |
| 312 | 312 | end; |
| 313 | 313 | |
| 314 | 314 | (* A mistake in the table: the program's, with status 1. *) |
| 315 | | -proc badTable(p: ^Parser; row: INT; why: []U8); |
| 315 | +proc badTable(const p: Parser; row: INT; why: []U8); |
| 316 | 316 | var b: Buf.Buf; |
| 317 | 317 | begin |
| 318 | 318 | Buf.init(b, Heap.heap); |
| 319 | | - Fmt.write(b, "{}: option table, row {}: {}\n", [Fmt.str(p^.name), Fmt.int(row), Fmt.str(why)]); |
| 319 | + Fmt.write(b, "{}: option table, row {}: {}\n", [Fmt.str(p.name), Fmt.int(row), Fmt.str(why)]); |
| 320 | 320 | say(Sys.stderr, b); |
| 321 | 321 | Rt.exit(1); |
| 322 | 322 | end; |
| 323 | 323 | |
| 324 | | -proc check(p: ^Parser); |
| 324 | +proc check(const p: Parser); |
| 325 | 325 | var i, j: INT; |
| 326 | 326 | begin |
| 327 | | - for i := 0 to LEN(p^.table) - 1 do |
| 328 | | - match p^.table[i] of |
| 327 | + for i := 0 to LEN(p.table) - 1 do |
| 328 | + match p.table[i] of |
| 329 | 329 | | sub(_s, name, _help): |
| 330 | 330 | if LEN(name) = 0 then badTable(p, i, "a subcommand needs a name"); end; |
| 331 | 331 | | option(s, letter, long, value, _help): |
| | @@ -339,7 +339,7 @@ |
| 339 | 339 | end; |
| 340 | 340 | if long = "help" then badTable(p, i, "--help is Opt's"); end; |
| 341 | 341 | for j := 0 to i - 1 do |
| 342 | | - match p^.table[j] of |
| 342 | + match p.table[j] of |
| 343 | 343 | | option(s2, letter2, long2, _v2, _help2): |
| 344 | 344 | if (s2 = s) and ((letter2 = letter) or (LEN(long) > 0) and (long2 = long)) then |
| 345 | 345 | badTable(p, i, "the option is in the scope already"); |
| | @@ -354,65 +354,65 @@ |
| 354 | 354 | |
| 355 | 355 | (* p reads the arguments from 1 on, by table, in the program's own |
| 356 | 356 | scope; name begins the messages. *) |
| 357 | | -proc init*(p: ^Parser; name: []U8; table: []Row); |
| 357 | +proc init*(var p: Parser; name: []U8; table: []Row); |
| 358 | 358 | begin |
| 359 | | - p^ := {name: name, table: table, at: 1}; |
| 359 | + p := {name: name, table: table, at: 1}; |
| 360 | 360 | check(p); |
| 361 | 361 | answerHelp(p); |
| 362 | 362 | end; |
| 363 | 363 | |
| 364 | 364 | (* The next option, in p.letter, p.value and p.long; false when the |
| 365 | 365 | scope's options end. *) |
| 366 | | -proc next*(p: ^Parser): BOOL; |
| 366 | +proc next*(var p: Parser): BOOL; |
| 367 | 367 | begin |
| 368 | 368 | if read(p, false) = readEnd then return false; end; |
| 369 | | - if p^.help then printHelp(p); end; |
| 369 | + if p.help then printHelp(p); end; |
| 370 | 370 | return true; |
| 371 | 371 | end; |
| 372 | 372 | |
| 373 | 373 | (* The next operand, after what options are left; false when there are |
| 374 | 374 | no more. *) |
| 375 | | -proc operand*(p: ^Parser): ([]U8, BOOL); |
| 375 | +proc operand*(var p: Parser): ([]U8, BOOL); |
| 376 | 376 | begin |
| 377 | 377 | while next(p) do end; |
| 378 | | - if p^.at >= ARGC() then return "", false; end; |
| 379 | | - INC(p^.at); |
| 380 | | - return Files.arg(p^.at - 1), true; |
| 378 | + if p.at >= ARGC() then return "", false; end; |
| 379 | + INC(p.at); |
| 380 | + return Files.arg(p.at - 1), true; |
| 381 | 381 | end; |
| 382 | 382 | |
| 383 | 383 | (* The number of the argument operand would give next, after what |
| 384 | 384 | options are left: the operands from there on are Files.arg(i), for |
| 385 | 385 | i up to ARGC() - 1, as an interpreter hands them to its script. *) |
| 386 | | -proc at*(p: ^Parser): INT; |
| 386 | +proc at*(var p: Parser): INT; |
| 387 | 387 | begin |
| 388 | 388 | while next(p) do end; |
| 389 | | - return p^.at; |
| 389 | + return p.at; |
| 390 | 390 | end; |
| 391 | 391 | |
| 392 | 392 | (* The options of the subcommand name, from the word after its own, |
| 393 | 393 | which operand has given. *) |
| 394 | | -proc scope*(p: ^Parser; name: []U8); |
| 394 | +proc scope*(var p: Parser; name: []U8); |
| 395 | 395 | begin |
| 396 | | - p^.scope := name; |
| 397 | | - p^.ended := false; |
| 398 | | - p^.group := ""; |
| 396 | + p.scope := name; |
| 397 | + p.ended := false; |
| 398 | + p.group := ""; |
| 399 | 399 | answerHelp(p); |
| 400 | 400 | end; |
| 401 | 401 | |
| 402 | 402 | (* The value of the option just read, as a whole number from lo to hi; |
| 403 | 403 | anything else is a mistake. A number with no top is given the |
| 404 | 404 | largest I64 for hi, and the mistake says "from lo up". *) |
| 405 | | -proc number*(p: ^Parser; lo, hi: I64): I64; |
| 405 | +proc number*(const p: Parser; lo, hi: I64): I64; |
| 406 | 406 | var n: I64; ok: BOOL; |
| 407 | 407 | begin |
| 408 | | - n, ok := Str.toInt(p^.value); |
| 408 | + n, ok := Str.toInt(p.value); |
| 409 | 409 | if not ok or (n < lo) or (n > hi) then |
| 410 | 410 | if hi = largest then |
| 411 | 411 | mistake(p, "{}{} wants a number from {} up, not '{}'", |
| 412 | | - [Fmt.str(dashes(p)), Fmt.str(p^.spelt), Fmt.int(lo), Fmt.str(p^.value)]); |
| 412 | + [Fmt.str(dashes(p)), Fmt.str(p.spelt), Fmt.int(lo), Fmt.str(p.value)]); |
| 413 | 413 | end; |
| 414 | 414 | mistake(p, "{}{} wants a number from {} to {}, not '{}'", |
| 415 | | - [Fmt.str(dashes(p)), Fmt.str(p^.spelt), Fmt.int(lo), Fmt.int(hi), Fmt.str(p^.value)]); |
| 415 | + [Fmt.str(dashes(p)), Fmt.str(p.spelt), Fmt.int(lo), Fmt.int(hi), Fmt.str(p.value)]); |
| 416 | 416 | end; |
| 417 | 417 | return n; |
| 418 | 418 | end; |
| | @@ -420,19 +420,19 @@ |
| 420 | 420 | (* The value of the option just read refused, as number refuses one: |
| 421 | 421 | "-b wants wants, not 'value'", the option as it was written, with |
| 422 | 422 | the scope's synopsis lines, and status 2. *) |
| 423 | | -proc refuse*(p: ^Parser; wants: []U8); |
| 423 | +proc refuse*(const p: Parser; wants: []U8); |
| 424 | 424 | begin |
| 425 | 425 | mistake(p, "{}{} wants {}, not '{}'", |
| 426 | | - [Fmt.str(dashes(p)), Fmt.str(p^.spelt), Fmt.str(wants), Fmt.str(p^.value)]); |
| 426 | + [Fmt.str(dashes(p)), Fmt.str(p.spelt), Fmt.str(wants), Fmt.str(p.value)]); |
| 427 | 427 | end; |
| 428 | 428 | |
| 429 | 429 | (* The program's own mistake, written as Opt's are but alone, and |
| 430 | 430 | status 2. *) |
| 431 | | -proc fail*(p: ^Parser; text: []U8); |
| 431 | +proc fail*(const p: Parser; text: []U8); |
| 432 | 432 | var b: Buf.Buf; |
| 433 | 433 | begin |
| 434 | 434 | Buf.init(b, Heap.heap); |
| 435 | | - Fmt.write(b, "{}: {}\n", [Fmt.str(p^.name), Fmt.str(text)]); |
| 435 | + Fmt.write(b, "{}: {}\n", [Fmt.str(p.name), Fmt.str(text)]); |
| 436 | 436 | say(Sys.stderr, b); |
| 437 | 437 | Rt.exit(2); |
| 438 | 438 | end; |
| | @@ -442,54 +442,56 @@ |
| 442 | 442 | program of getopt's page, or a program with a bad table, or one with |
| 443 | 443 | no synopsis lines; and holds each to what it writes, and its status. *) |
| 444 | 444 | proc keep; |
| 445 | | -const wait = 1; jobs = 2; |
| 446 | | -var p: Parser; verbose, ok: BOOL; secs: I64; bar, job, w: []U8; b: Buf.Buf; i: INT; |
| 447 | | -begin |
| 448 | | - secs := 10; |
| 449 | | - init(ADR(p), "keep", [ |
| 445 | +const |
| 446 | + wait = 1; jobs = 2; |
| 447 | + Rows: []Row = [ (* a constant table; badTableTest and plain write theirs in the call *) |
| 450 | 448 | usage("", "keep [-v] [--wait secs] start ..."), |
| 451 | 449 | sub("", "start", "start a job"), |
| 452 | 450 | option("", 'v', "verbose", "", "say more"), |
| 453 | 451 | option("", wait, "wait", "secs", "give the job secs to start"), |
| 454 | 452 | option("", jobs, "jobs", "n", "keep n jobs at most"), |
| 455 | 453 | usage("start", "keep start [-b label] name command [argument ...]"), |
| 456 | | - option("start", 'b', "bar", "label", "show the job in the bar as label")]); |
| 457 | | - while next(ADR(p)) do |
| 454 | + option("start", 'b', "bar", "label", "show the job in the bar as label")]; |
| 455 | +var p: Parser; verbose, ok: BOOL; secs: I64; bar, job, w: []U8; b: Buf.Buf; i: INT; |
| 456 | +begin |
| 457 | + secs := 10; |
| 458 | + init(p, "keep", Rows); |
| 459 | + while next(p) do |
| 458 | 460 | if p.letter = 'v' then verbose := true; |
| 459 | | - elsif p.letter = wait then secs := number(ADR(p), 1, 600); |
| 460 | | - elsif p.letter = jobs then _ := number(ADR(p), 1, largest); |
| 461 | + elsif p.letter = wait then secs := number(p, 1, 600); |
| 462 | + elsif p.letter = jobs then _ := number(p, 1, largest); |
| 461 | 463 | end; |
| 462 | 464 | end; |
| 463 | | - w, ok := operand(ADR(p)); |
| 464 | | - if not ok then fail(ADR(p), "no command; see keep -h"); end; |
| 465 | + w, ok := operand(p); |
| 466 | + if not ok then fail(p, "no command; see keep -h"); end; |
| 465 | 467 | if w # "start" then |
| 466 | 468 | Buf.init(b, Heap.heap); |
| 467 | 469 | Fmt.write(b, "no command {}; see keep -h", [Fmt.str(w)]); |
| 468 | | - fail(ADR(p), Buf.bytes(b)); |
| 470 | + fail(p, Buf.bytes(b)); |
| 469 | 471 | end; |
| 470 | | - scope(ADR(p), "start"); |
| 471 | | - while next(ADR(p)) do |
| 472 | | - if (p.letter = 'b') and (Str.find(p.value, " ") >= 0) then refuse(ADR(p), "a label with no spaces"); |
| 472 | + scope(p, "start"); |
| 473 | + while next(p) do |
| 474 | + if (p.letter = 'b') and (Str.find(p.value, " ") >= 0) then refuse(p, "a label with no spaces"); |
| 473 | 475 | elsif p.letter = 'b' then bar := p.value; |
| 474 | 476 | end; |
| 475 | 477 | end; |
| 476 | | - job, ok := operand(ADR(p)); |
| 478 | + job, ok := operand(p); |
| 477 | 479 | (* the command and its arguments, as they are, by at *) |
| 478 | | - if not ok or (at(ADR(p)) >= ARGC()) then |
| 479 | | - fail(ADR(p), "start needs a name and a command; see keep start -h"); |
| 480 | + if not ok or (at(p) >= ARGC()) then |
| 481 | + fail(p, "start needs a name and a command; see keep start -h"); |
| 480 | 482 | end; |
| 481 | 483 | if verbose then Fmt.print("waiting {}s\n", [Fmt.int(secs)]); end; |
| 482 | 484 | Fmt.print("job {}", [Fmt.str(job)]); |
| 483 | 485 | if LEN(bar) > 0 then Fmt.print(" [{}]", [Fmt.str(bar)]); end; |
| 484 | 486 | Fmt.print(":", []); |
| 485 | | - for i := at(ADR(p)) to ARGC() - 1 do Fmt.print(" {}", [Fmt.str(Files.arg(i))]); end; |
| 487 | + for i := at(p) to ARGC() - 1 do Fmt.print(" {}", [Fmt.str(Files.arg(i))]); end; |
| 486 | 488 | Fmt.print("\n", []); |
| 487 | 489 | end; |
| 488 | 490 | |
| 489 | 491 | proc badTableTest; |
| 490 | 492 | var p: Parser; |
| 491 | 493 | begin |
| 492 | | - init(ADR(p), "bad", [ |
| 494 | + init(p, "bad", [ |
| 493 | 495 | option("", 'v', "verbose", "", "say more"), |
| 494 | 496 | option("", 'q', "quiet", "", "say less"), |
| 495 | 497 | option("", 'v', "loud", "", "say it again")]); |
| | @@ -500,17 +502,17 @@ |
| 500 | 502 | proc plain; |
| 501 | 503 | var p: Parser; _w: []U8; _ok: BOOL; |
| 502 | 504 | begin |
| 503 | | - init(ADR(p), "plain", [ |
| 505 | + init(p, "plain", [ |
| 504 | 506 | sub("", "remote", "the remotes"), |
| 505 | 507 | sub("remote", "add", "add one"), |
| 506 | 508 | option("remote.add", 'f', "force", "", "even if it is there"), |
| 507 | 509 | option("remote.add", 'h', "here", "", "only here"), |
| 508 | 510 | option("remote.add", 'c', "color", "[when]", "colour it: always, never or auto")]); |
| 509 | | - _w, _ok := operand(ADR(p)); |
| 510 | | - scope(ADR(p), "remote"); |
| 511 | | - _w, _ok := operand(ADR(p)); |
| 512 | | - scope(ADR(p), "remote.add"); |
| 513 | | - while next(ADR(p)) do Fmt.print("{} {}={}\n", [Fmt.char(p.letter), Fmt.str(p.spelt), Fmt.str(p.value)]); end; |
| 511 | + _w, _ok := operand(p); |
| 512 | + scope(p, "remote"); |
| 513 | + _w, _ok := operand(p); |
| 514 | + scope(p, "remote.add"); |
| 515 | + while next(p) do Fmt.print("{} {}={}\n", [Fmt.char(p.letter), Fmt.str(p.spelt), Fmt.str(p.value)]); end; |
| 514 | 516 | end; |
| 515 | 517 | |
| 516 | 518 | type Case = record |