@amritk/nish-x86_64-linux 0.42.0 → 0.45.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (124) hide show
  1. package/bin/nish +0 -0
  2. package/docs/AI.md +287 -129
  3. package/package.json +1 -1
  4. package/runtime/core/numbers.mjs +3 -1
  5. package/runtime/core/strings.mjs +90 -38
  6. package/runtime/core.d.ts +24 -0
  7. package/runtime/core.mjs +21 -0
  8. package/runtime/nish.d.ts +167 -24
  9. package/runtime/nish.mjs +7 -1
  10. package/runtime/runtime-cross.c +9 -0
  11. package/runtime/runtime-net.c +51 -13
  12. package/runtime/runtime-region.c +13 -0
  13. package/runtime/runtime-text.c +11 -0
  14. package/runtime/runtime-uring.c +83 -25
  15. package/runtime/runtime-wasm.c +3 -0
  16. package/runtime/runtime.c +329 -6
  17. package/runtime/sqlite/README.md +16 -3
  18. package/runtime/sqlite/nish-sqlite.c +940 -29
  19. package/scripts/build.sh +111 -29
  20. package/std/README.md +6 -6
  21. package/std/array.ts +10 -2
  22. package/std/bytes.ts +239 -26
  23. package/std/cli-args.ts +1072 -606
  24. package/std/cli-flags.ts +180 -98
  25. package/std/cli.ts +21 -13
  26. package/std/collections.ts +245 -127
  27. package/std/compress/checksum.ts +32 -37
  28. package/std/compress/deflate.ts +1102 -542
  29. package/std/compress/gzip.ts +290 -145
  30. package/std/compress/zlib.ts +57 -21
  31. package/std/crypto/aes.ts +153 -75
  32. package/std/crypto/base64url.ts +163 -39
  33. package/std/crypto/bcrypt-batch.ts +164 -90
  34. package/std/crypto/bcrypt.ts +185 -107
  35. package/std/crypto/chacha20poly1305.ts +84 -68
  36. package/std/crypto/ct.ts +9 -7
  37. package/std/crypto/ed25519.ts +64 -27
  38. package/std/crypto/hkdf.ts +105 -63
  39. package/std/crypto/hmac.ts +95 -89
  40. package/std/crypto/jwt.ts +248 -126
  41. package/std/crypto/p256.ts +758 -444
  42. package/std/crypto/p384.ts +20 -13
  43. package/std/crypto/pki-roots.ts +95 -38
  44. package/std/crypto/pki.ts +164 -60
  45. package/std/crypto/rsa.ts +266 -223
  46. package/std/crypto/scram.ts +141 -0
  47. package/std/crypto/sha1.ts +157 -57
  48. package/std/crypto/sha256.ts +502 -422
  49. package/std/crypto/sha512.ts +204 -135
  50. package/std/crypto/x509-random.ts +9 -3
  51. package/std/crypto/x509.ts +331 -171
  52. package/std/host/ring.ts +1 -1
  53. package/std/host/text.ts +1 -1
  54. package/std/json-equal.ts +35 -19
  55. package/std/json-format.ts +215 -108
  56. package/std/json-number.ts +126 -72
  57. package/std/json-pointer.ts +42 -13
  58. package/std/json-tokenizer.ts +498 -307
  59. package/std/json-typed.ts +115 -36
  60. package/std/json-value.ts +341 -152
  61. package/std/json.ts +300 -158
  62. package/std/net/buffer-pool.ts +17 -7
  63. package/std/net/dns-config.ts +427 -236
  64. package/std/net/dns-message.ts +114 -47
  65. package/std/net/dns-resolver.ts +736 -237
  66. package/std/net/dns.ts +205 -52
  67. package/std/net/fetch.ts +320 -184
  68. package/std/net/grpc.ts +948 -0
  69. package/std/net/handler.ts +1276 -218
  70. package/std/net/hpack.ts +395 -180
  71. package/std/net/http-compress.ts +502 -65
  72. package/std/net/http-fields.ts +85 -49
  73. package/std/net/http-server.ts +1764 -603
  74. package/std/net/http1-client-codec.ts +372 -161
  75. package/std/net/http1-codec.ts +665 -331
  76. package/std/net/http1-server.ts +881 -269
  77. package/std/net/http2-cleartext.ts +165 -95
  78. package/std/net/http2-frame.ts +232 -89
  79. package/std/net/http2-tls.ts +42 -12
  80. package/std/net/http2.ts +327 -101
  81. package/std/net/http3-frame.ts +68 -33
  82. package/std/net/http3-server.ts +672 -206
  83. package/std/net/http3.ts +669 -191
  84. package/std/net/oidc.ts +63 -24
  85. package/std/net/outbound.ts +207 -142
  86. package/std/net/postgres-async.ts +1223 -470
  87. package/std/net/postgres-carrier.ts +575 -372
  88. package/std/net/postgres.ts +1521 -632
  89. package/std/net/prefork.ts +254 -98
  90. package/std/net/protobuf.ts +1017 -0
  91. package/std/net/qpack.ts +143 -92
  92. package/std/net/quic-conn-ack.ts +104 -22
  93. package/std/net/quic-conn-cid.ts +66 -26
  94. package/std/net/quic-conn-params.ts +13 -7
  95. package/std/net/quic-datagram.ts +90 -27
  96. package/std/net/quic-frame.ts +324 -121
  97. package/std/net/quic-listener.ts +206 -91
  98. package/std/net/quic-packet.ts +289 -163
  99. package/std/net/quic-recovery.ts +279 -128
  100. package/std/net/quic-stream.ts +466 -167
  101. package/std/net/quic.ts +584 -258
  102. package/std/net/router.ts +450 -99
  103. package/std/net/serve.ts +661 -240
  104. package/std/net/static.ts +1458 -0
  105. package/std/net/tls/client.ts +140 -45
  106. package/std/net/tls/codec.ts +510 -260
  107. package/std/net/tls/record-client.ts +268 -76
  108. package/std/net/tls/record-server.ts +236 -79
  109. package/std/net/tls/record.ts +173 -47
  110. package/std/net/tls/schedule.ts +58 -39
  111. package/std/net/tls/ticket.ts +295 -103
  112. package/std/net/tls/verify.ts +1 -1
  113. package/std/net/tls-tcp.ts +218 -75
  114. package/std/net/tls.ts +260 -152
  115. package/std/net/upstream.ts +283 -86
  116. package/std/net/url.ts +277 -102
  117. package/std/net/websocket.ts +532 -209
  118. package/std/net/webtransport.ts +755 -436
  119. package/std/regex.ts +1659 -829
  120. package/std/sqlite.ts +325 -0
  121. package/std/sync/ring.ts +320 -156
  122. package/std/sync/shm.ts +53 -6
  123. package/std/text.ts +285 -135
  124. package/std/threads.ts +22 -2
package/std/cli-flags.ts CHANGED
@@ -159,29 +159,32 @@ const isDecimal = (text: string): boolean => {
159
159
  // 0 before anything, 1 in the digits, 2 in the fraction, 3 just after
160
160
  // the `e`, 4 in the exponent.
161
161
  let state: i32 = 0
162
- let mantissa: i32 = 0
163
- let exponent: i32 = 0
162
+ // Whether a digit came before the exponent, and one after the `e`: only
163
+ // their presence matters, so no count that could grow.
164
+ let mantissa = false
165
+ let exponent = false
164
166
  for (let i: i32 = 0; i < toI32(text.length); i++) {
165
167
  const c = toI32(text.charCodeAt(i))
166
168
  if (c >= 48 && c <= 57) {
167
169
  if (state >= 3) {
168
- exponent = exponent + 1
170
+ exponent = true
169
171
  state = 4
170
172
  } else {
171
- mantissa = mantissa + 1
173
+ mantissa = true
172
174
  state = state === 0 ? toI32(1) : state
173
175
  }
174
176
  } else if ((c === 43 || c === 45) && (state === 0 || state === 3)) {
175
- state = state + 1
177
+ // A sign leads the number (0 to 1) or the exponent (3 to 4).
178
+ state = state === 0 ? toI32(1) : toI32(4)
176
179
  } else if (c === 46 && state <= 1) {
177
180
  state = 2
178
- } else if ((c === 101 || c === 69) && (state === 1 || state === 2) && mantissa > 0) {
181
+ } else if ((c === 101 || c === 69) && (state === 1 || state === 2) && mantissa) {
179
182
  state = 3
180
183
  } else {
181
184
  return false
182
185
  }
183
186
  }
184
- return mantissa > 0 && (state < 3 || exponent > 0)
187
+ return mantissa && (state < 3 || exponent)
185
188
  }
186
189
 
187
190
  /** Whether `name` can follow `--` and be matched: not empty, no leading `-`, and no `=`, space or control byte. */
@@ -226,14 +229,14 @@ export class FlagTable {
226
229
  this.commands = []
227
230
  }
228
231
 
229
- /** @internal Called by `ParsedFlags`. The index of the flag `name` (without `--`), or `-1`. */
230
- indexOf(name: string): i32 {
231
- for (let i: i32 = 0; i < toI32(this.flags.length); i++) {
232
- if (this.flags[i].name === name) {
233
- return i
232
+ /** The flag `name` (without `--`) as declared, or `null` when the table declares none. */
233
+ private find(name: string): FlagSpec | null {
234
+ for (const f of this.flags) {
235
+ if (f.name === name) {
236
+ return f
234
237
  }
235
238
  }
236
- return -1
239
+ return null
237
240
  }
238
241
 
239
242
  /** A flag of `kind`, after the checks every declaring method shares. */
@@ -243,13 +246,14 @@ export class FlagTable {
243
246
  `nish/cli-flags: a flag name is not empty and has no leading '-', '=' or whitespace, got "${name}"`
244
247
  )
245
248
  }
246
- if (this.indexOf(name) >= 0) {
249
+ if (this.find(name) !== null) {
247
250
  panic(`nish/cli-flags: --${name} is declared twice`)
248
251
  }
249
252
  // `--no-x` cannot be both a flag of its own and the clearing of a boolean `--x`.
250
- const clears = kind === FLAG_BOOLEAN ? this.indexOf(`no-${name}`) : toI32(-1)
251
- const cleared = name.startsWith("no-") ? this.indexOf(name.substring(3)) : toI32(-1)
252
- if (clears >= 0 || (cleared >= 0 && this.flags[cleared].kind === FLAG_BOOLEAN)) {
253
+ const clears = kind === FLAG_BOOLEAN && this.find(`no-${name}`) !== null
254
+ const base: FlagSpec | null = name.startsWith("no-") ? this.find(name.substring(3)) : null
255
+ const cleared = base !== null && base.kind === FLAG_BOOLEAN
256
+ if (clears || cleared) {
253
257
  panic(
254
258
  `nish/cli-flags: --${kind === FLAG_BOOLEAN ? `no-${name}` : name} would both be a flag and clear a boolean`
255
259
  )
@@ -346,18 +350,17 @@ export class FlagTable {
346
350
  }
347
351
 
348
352
  /**
349
- * The index of the command `words` spell the longest declared path of, or
350
- * `-1`. A path spelled by a prefix of `words` counts; the words after it
351
- * are the positionals.
353
+ * The command `words` spell the longest declared path of, or `null`. A
354
+ * path spelled by a prefix of `words` counts; the words after it are the
355
+ * positionals.
352
356
  */
353
- private matchCommand(words: string[]): i32 {
354
- let best: i32 = -1
357
+ private matchCommand(words: string[]): CommandSpec | null {
358
+ let best: CommandSpec | null = null
355
359
  let bestLength: i32 = 0
356
- for (let i: i32 = 0; i < toI32(this.commands.length); i++) {
357
- const path = this.commands[i].words
358
- const length = toI32(path.length)
359
- if (length <= toI32(words.length) && length > bestLength && agrees(path, words, length)) {
360
- best = i
360
+ for (const c of this.commands) {
361
+ const length = toI32(c.words.length)
362
+ if (length <= toI32(words.length) && length > bestLength && agrees(c.words, words, length)) {
363
+ best = c
361
364
  bestLength = length
362
365
  }
363
366
  }
@@ -419,16 +422,16 @@ export class FlagTable {
419
422
  // A value flag written without `=` waits here for the next argument,
420
423
  // which is its value whatever it looks like, so that `--jobs -1` and
421
424
  // `--grep --x` mean what they say.
422
- let pending: i32 = -1
425
+ let pending: FlagValue | null = null
423
426
  let pendingFlag = ""
424
427
  let pendingArg = ""
425
428
  for (let i: i32 = 0; i < toI32(args.length) && got.ok; i++) {
426
429
  const arg = args[i]
427
430
  if (i < first) {
428
431
  // Before `first`, so not read at all.
429
- } else if (pending >= 0) {
432
+ } else if (pending !== null) {
430
433
  got.take(pending, pendingFlag, arg, pendingArg)
431
- pending = -1
434
+ pending = null
432
435
  } else if (ended) {
433
436
  trailing.push(arg)
434
437
  } else if (arg === "--") {
@@ -438,56 +441,58 @@ export class FlagTable {
438
441
  } else if (!arg.startsWith("--")) {
439
442
  got.fail(FLAG_ERR_UNKNOWN, `unknown flag ${arg}`, arg)
440
443
  } else {
441
- const eq = toI32(arg.indexOf("="))
442
- const flag = eq < 0 ? arg : arg.slice(0, eq)
443
- let at = this.indexOf(flag.substring(2))
444
- let negated = false
445
- if (at < 0 && flag.startsWith("--no-")) {
446
- const cleared = this.indexOf(flag.substring(5))
447
- if (cleared >= 0 && this.flags[cleared].kind === FLAG_BOOLEAN) {
448
- at = cleared
449
- negated = true
444
+ // `--name=value` splits at its first `=`; the `--` holds none.
445
+ let flag = arg
446
+ let value = ""
447
+ let joined = false
448
+ for (let k: i32 = 2; k < toI32(arg.length); k++) {
449
+ if (arg.charCodeAt(k) === 61) {
450
+ flag = arg.substring(0, k)
451
+ value = arg.substring(k + 1)
452
+ joined = true
453
+ break
450
454
  }
451
455
  }
452
- if (at < 0) {
456
+ const found = got.lookup(flag)
457
+ if (found === null) {
453
458
  got.fail(FLAG_ERR_UNKNOWN, `unknown flag ${flag}`, arg)
454
- } else {
455
- const spec = this.flags[at]
456
- if (spec.kind === FLAG_BOOLEAN) {
457
- if (eq >= 0) {
458
- got.fail(FLAG_ERR_BOOLEAN_VALUE, `${flag} takes no value`, arg)
459
- } else if (got.counts[at] > 0) {
460
- got.fail(FLAG_ERR_REPEATED, `--${spec.name} is given twice`, arg)
461
- } else {
462
- got.bools[at] = !negated
463
- got.counts[at] = 1
464
- }
465
- } else if (eq >= 0) {
466
- got.take(at, flag, arg.substring(eq + 1), arg)
459
+ } else if (found.spec.kind === FLAG_BOOLEAN) {
460
+ if (joined) {
461
+ got.fail(FLAG_ERR_BOOLEAN_VALUE, `${flag} takes no value`, arg)
462
+ } else if (found.given) {
463
+ got.fail(FLAG_ERR_REPEATED, `--${found.spec.name} is given twice`, arg)
467
464
  } else {
468
- pending = at
469
- pendingFlag = flag
470
- pendingArg = arg
465
+ // `lookup` matched the base name of a `--no-x` exactly when the
466
+ // name it found is not the one written.
467
+ found.bool = found.spec.name === flag.substring(2)
468
+ found.given = true
471
469
  }
470
+ } else if (joined) {
471
+ got.take(found, flag, value, arg)
472
+ } else {
473
+ pending = found
474
+ pendingFlag = flag
475
+ pendingArg = arg
472
476
  }
473
477
  }
474
478
  }
475
- if (pending >= 0 && got.ok) {
476
- const meta = this.flags[pending].meta
479
+ if (pending !== null && got.ok) {
480
+ const meta = pending.spec.meta
477
481
  const wanted = meta.length > 0 ? meta : "a value"
478
482
  got.fail(FLAG_ERR_MISSING_VALUE, `${pendingFlag} needs ${wanted}`, pendingArg)
479
483
  }
484
+ let skip: i32 = 0
480
485
  if (this.commands.length > 0) {
481
- const at = this.matchCommand(words)
482
- if (at >= 0) {
483
- got.commandAt = at
484
- got.command = this.commands[at].path
486
+ const chosen = this.matchCommand(words)
487
+ if (chosen !== null) {
488
+ got.chosen = chosen
489
+ got.command = chosen.path
490
+ skip = toI32(chosen.words.length)
485
491
  } else if (words.length > 0 && got.ok) {
486
492
  const shown = this.unknownCommand(words)
487
493
  got.fail(FLAG_ERR_UNKNOWN_COMMAND, `unknown command "${shown}"`, shown)
488
494
  }
489
495
  }
490
- const skip: i32 = got.commandAt >= 0 ? toI32(this.commands[got.commandAt].words.length) : 0
491
496
  for (let w: i32 = 0; w < toI32(words.length); w++) {
492
497
  if (w >= skip) {
493
498
  got.positionals.push(words[w])
@@ -496,6 +501,7 @@ export class FlagTable {
496
501
  for (const t of trailing) {
497
502
  got.positionals.push(t)
498
503
  }
504
+ got.publish()
499
505
  return got
500
506
  }
501
507
 
@@ -524,6 +530,34 @@ export class FlagTable {
524
530
  }
525
531
  }
526
532
 
533
+ /**
534
+ * One flag's answer, beside the declaration it answers for, so that one
535
+ * lookup by name reaches the kind and every value without an index.
536
+ */
537
+ class FlagValue {
538
+ /** The declaration, shared with the table. */
539
+ spec: FlagSpec
540
+ /** A string or number flag's value as written, `""` when not given. */
541
+ text: string
542
+ /** A number flag's value, `0` when not given. */
543
+ number: f64
544
+ /** A repeatable flag's values in the order given; empty when not given. */
545
+ list: string[]
546
+ /** Whether the command line gave the flag; for a boolean, in either spelling. */
547
+ given: boolean
548
+ /** A boolean flag's value, `false` when not given. */
549
+ bool: boolean
550
+
551
+ constructor(spec: FlagSpec) {
552
+ this.spec = spec
553
+ this.text = ""
554
+ this.number = toF64(0)
555
+ this.list = []
556
+ this.given = false
557
+ this.bool = false
558
+ }
559
+ }
560
+
527
561
  /**
528
562
  * What `parse` answered: a usage error, or every flag's value. Values sit in
529
563
  * arrays parallel to the table's `flags`: `counts` says how often each was
@@ -549,8 +583,8 @@ export class ParsedFlags {
549
583
  argument: string
550
584
  /** The path of the command the leading words chose, or `""` for none. */
551
585
  command: string
552
- /** @internal Used by `FlagTable`. */
553
- commandAt: i32
586
+ /** @internal Used by `FlagTable`. The command the leading words chose, or `null`. */
587
+ chosen: CommandSpec | null
554
588
  /**
555
589
  * The words that are not flags, values or the command's path, in order,
556
590
  * then every argument after `--`. On a usage error, only those among the
@@ -567,6 +601,12 @@ export class ParsedFlags {
567
601
  bools: boolean[]
568
602
  /** Each repeatable flag's values in the order given; empty when not given. */
569
603
  lists: string[][]
604
+ /**
605
+ * One answer per flag the table declared when the parse began, in its
606
+ * order: what the readers look up, and what `publish` copies into the
607
+ * arrays above.
608
+ */
609
+ private values: FlagValue[]
570
610
 
571
611
  /**
572
612
  * An answer with no error and every flag unset, which `FlagTable.parseFrom`
@@ -582,21 +622,57 @@ export class ParsedFlags {
582
622
  this.message = ""
583
623
  this.argument = ""
584
624
  this.command = ""
585
- this.commandAt = -1
625
+ this.chosen = null
586
626
  this.positionals = []
587
627
  this.counts = []
588
628
  this.texts = []
589
629
  this.numbers = []
590
630
  this.bools = []
591
631
  this.lists = []
592
- for (let i: i32 = 0; i < toI32(table.flags.length); i++) {
593
- this.counts.push(toI32(0))
594
- this.texts.push("")
595
- this.numbers.push(toF64(0))
596
- this.bools.push(false)
597
- const none: string[] = []
598
- this.lists.push(none)
632
+ this.values = []
633
+ for (const f of table.flags) {
634
+ this.values.push(new FlagValue(f))
599
635
  }
636
+ this.publish()
637
+ }
638
+
639
+ /**
640
+ * @internal Called by `FlagTable`. Rewrites `counts`, `texts`, `numbers`,
641
+ * `bools` and `lists` from the answers, one element per flag in the
642
+ * table's order; a repeatable flag's count is how many values it holds.
643
+ */
644
+ publish(): void {
645
+ this.counts = []
646
+ this.texts = []
647
+ this.numbers = []
648
+ this.bools = []
649
+ this.lists = []
650
+ for (const v of this.values) {
651
+ const once: i32 = v.given ? 1 : 0
652
+ this.counts.push(v.spec.kind === FLAG_REPEATABLE ? toI32(v.list.length) : once)
653
+ this.texts.push(v.text)
654
+ this.numbers.push(v.number)
655
+ this.bools.push(v.bool)
656
+ this.lists.push(v.list)
657
+ }
658
+ }
659
+
660
+ /**
661
+ * @internal Called by `FlagTable`. The answer for `flag` as written
662
+ * (`--name`, without any `=value`): the flag of that name, or for
663
+ * `--no-x` the boolean `x`, or `null` for neither. One pass: the table
664
+ * never holds both a `no-x` and a boolean `x`, so at most one answer
665
+ * matches.
666
+ */
667
+ lookup(flag: string): FlagValue | null {
668
+ const name = flag.substring(2)
669
+ const base = flag.startsWith("--no-") ? flag.substring(5) : ""
670
+ for (const v of this.values) {
671
+ if (v.spec.name === name || (base.length > 0 && v.spec.kind === FLAG_BOOLEAN && v.spec.name === base)) {
672
+ return v
673
+ }
674
+ }
675
+ return null
600
676
  }
601
677
 
602
678
  /** @internal Called by `FlagTable`. Records the first usage error; a parse stops at it. */
@@ -607,15 +683,15 @@ export class ParsedFlags {
607
683
  this.argument = argument
608
684
  }
609
685
 
610
- /** @internal Called by `FlagTable`. Stores `value` for the flag at `at`, spelled `flag` in `arg`, or fails as its kind requires. */
611
- take(at: i32, flag: string, value: string, arg: string): void {
612
- const kind = this.table.flags[at].kind
686
+ /** @internal Called by `FlagTable`. Stores `value` for the flag `into`, spelled `flag` in `arg`, or fails as its kind requires. */
687
+ take(into: FlagValue, flag: string, value: string, arg: string): void {
688
+ const kind = into.spec.kind
613
689
  if (kind === FLAG_REPEATABLE) {
614
- this.lists[at].push(value)
615
- this.counts[at] = this.counts[at] + 1
690
+ into.list.push(value)
691
+ into.given = true
616
692
  return
617
693
  }
618
- if (this.counts[at] > 0) {
694
+ if (into.given) {
619
695
  this.fail(FLAG_ERR_REPEATED, `${flag} is given twice`, arg)
620
696
  return
621
697
  }
@@ -628,19 +704,23 @@ export class ParsedFlags {
628
704
  this.fail(FLAG_ERR_NOT_A_NUMBER, `${flag} needs a number, not "${value}"`, value)
629
705
  return
630
706
  }
631
- this.numbers[at] = n
707
+ into.number = n
632
708
  }
633
- this.texts[at] = value
634
- this.counts[at] = 1
709
+ into.text = value
710
+ into.given = true
635
711
  }
636
712
 
637
- /** The index of the flag `name` of `kind`; a name the table does not declare so is a bug, and panics. */
638
- private slot(name: string, kind: i32): i32 {
639
- const at = this.table.indexOf(name)
640
- if (at < 0 || this.table.flags[at].kind !== kind) {
641
- panic(`nish/cli-flags: ${this.table.program} declares no such flag --${name} of that kind`)
713
+ /**
714
+ * The answer for the flag `name` of `kind`; a name the table does not
715
+ * declare so, or declared after this parse, is a bug, and panics.
716
+ */
717
+ private slot(name: string, kind: i32): FlagValue {
718
+ for (const v of this.values) {
719
+ if (v.spec.name === name && v.spec.kind === kind) {
720
+ return v
721
+ }
642
722
  }
643
- return at
723
+ panic(`nish/cli-flags: ${this.table.program} declares no such flag --${name} of that kind`)
644
724
  }
645
725
 
646
726
  /**
@@ -651,7 +731,7 @@ export class ParsedFlags {
651
731
  * @returns the value as it appeared on the command line
652
732
  */
653
733
  str(name: string): string {
654
- return this.texts[this.slot(name, FLAG_STRING)]
734
+ return this.slot(name, FLAG_STRING).text
655
735
  }
656
736
 
657
737
  /**
@@ -662,7 +742,7 @@ export class ParsedFlags {
662
742
  * @returns the finite value the command line gave
663
743
  */
664
744
  num(name: string): f64 {
665
- return this.numbers[this.slot(name, FLAG_NUMBER)]
745
+ return this.slot(name, FLAG_NUMBER).number
666
746
  }
667
747
 
668
748
  /**
@@ -674,7 +754,7 @@ export class ParsedFlags {
674
754
  * @returns whether the flag is set
675
755
  */
676
756
  bool(name: string): boolean {
677
- return this.bools[this.slot(name, FLAG_BOOLEAN)]
757
+ return this.slot(name, FLAG_BOOLEAN).bool
678
758
  }
679
759
 
680
760
  /**
@@ -688,7 +768,7 @@ export class ParsedFlags {
688
768
  */
689
769
  all(name: string): string[] {
690
770
  const out: string[] = []
691
- for (const v of this.lists[this.slot(name, FLAG_REPEATABLE)]) {
771
+ for (const v of this.slot(name, FLAG_REPEATABLE).list) {
692
772
  out.push(v)
693
773
  }
694
774
  return out
@@ -703,11 +783,13 @@ export class ParsedFlags {
703
783
  * @returns `true` when the command line gave it at least once
704
784
  */
705
785
  has(name: string): boolean {
706
- const at = this.table.indexOf(name)
707
- if (at < 0) {
708
- panic(`nish/cli-flags: ${this.table.program} declares no flag --${name}`)
786
+ // A flag declared after this parse has no answer in it.
787
+ for (const v of this.values) {
788
+ if (v.spec.name === name) {
789
+ return v.given
790
+ }
709
791
  }
710
- return this.counts[at] > 0
792
+ panic(`nish/cli-flags: ${this.table.program} declares no flag --${name}`)
711
793
  }
712
794
 
713
795
  /**
@@ -717,8 +799,8 @@ export class ParsedFlags {
717
799
  */
718
800
  private usageLine(): string {
719
801
  const table = this.table
720
- if (this.commandAt >= 0) {
721
- const c = table.commands[this.commandAt]
802
+ const c = this.chosen
803
+ if (c !== null) {
722
804
  const shape = c.shape.length > 0 ? ` ${c.shape}` : ""
723
805
  return `usage: ${table.program} ${c.path}${shape} [flags]`
724
806
  }
package/std/cli.ts CHANGED
@@ -895,10 +895,18 @@ const spaces = (count: i32): string => {
895
895
  }
896
896
 
897
897
  /**
898
- * The spaces that take plain text `width` columns wide to `column`, or two
899
- * when that would leave fewer, so a long flag still keeps apart from its text.
898
+ * The spaces that take plain text `fixed + first + second` columns wide to
899
+ * `column`, or two when that would leave fewer, so a long flag still keeps
900
+ * apart from its text. The width comes in parts, each an `i32`, and is summed
901
+ * as an `i64`, where three `i32`s and a constant cannot overflow; the spaces
902
+ * counted are then at most `column`, so they fit an `i32` again.
900
903
  */
901
- const padTo = (width: i32, column: i32): string => spaces(width + 2 <= column ? column - width : 2)
904
+ const padTo = (fixed: i32, first: i32, second: i32, column: i32): string =>
905
+ spaces(
906
+ toI64(fixed) + toI64(first) + toI64(second) + toI64(2) <= toI64(column)
907
+ ? toI32(toI64(column) - toI64(fixed) - toI64(first) - toI64(second))
908
+ : 2
909
+ )
902
910
 
903
911
  /**
904
912
  * A help menu, built a line at a time and answered whole by `text`. Rows are
@@ -913,7 +921,7 @@ export class HelpMenu {
913
921
  private column: i32
914
922
  private usageColumn: i32
915
923
  private lines: string[]
916
- private usages: i32
924
+ private hasUsage: boolean
917
925
 
918
926
  /**
919
927
  * An empty menu; nothing is written until `text` is asked for.
@@ -935,7 +943,7 @@ export class HelpMenu {
935
943
  this.column = column
936
944
  this.usageColumn = usageColumn
937
945
  this.lines = []
938
- this.usages = 0
946
+ this.hasUsage = false
939
947
  }
940
948
 
941
949
  /**
@@ -986,11 +994,12 @@ export class HelpMenu {
986
994
  */
987
995
  usage(shape: string, description: string): void {
988
996
  const style = this.style
989
- const lead = this.usages === 0 ? `${style.heading("usage:")} ` : spaces(7)
990
- const width: i32 = 8 + toI32(this.program.length) + toI32(shape.length)
991
- const text = description.length > 0 ? `${padTo(width, this.usageColumn)}${style.dim(description)}` : ""
997
+ const lead = this.hasUsage ? spaces(7) : `${style.heading("usage:")} `
998
+ // `usage: ` or its indent is seven columns, and a space follows the name.
999
+ const pad = padTo(8, toI32(this.program.length), toI32(shape.length), this.usageColumn)
1000
+ const text = description.length > 0 ? `${pad}${style.dim(description)}` : ""
992
1001
  this.lines.push(`${lead}${style.name(this.program)} ${style.flag(shape)}${text}`)
993
- this.usages = this.usages + 1
1002
+ this.hasUsage = true
994
1003
  }
995
1004
 
996
1005
  /**
@@ -1015,11 +1024,10 @@ export class HelpMenu {
1015
1024
  */
1016
1025
  row(flag: string, meta: string, description: string): void {
1017
1026
  const style = this.style
1018
- const width: i32 = 2 + toI32(flag.length) + (meta.length > 0 ? 1 + toI32(meta.length) : 0)
1027
+ // The indent is two columns, and a placeholder is one more for its space.
1028
+ const pad = padTo(meta.length > 0 ? 3 : 2, toI32(flag.length), toI32(meta.length), this.column)
1019
1029
  const placeholder = meta.length > 0 ? ` ${style.meta(meta)}` : ""
1020
- this.lines.push(
1021
- ` ${style.flag(flag)}${placeholder}${padTo(width, this.column)}${style.dim(description)}`
1022
- )
1030
+ this.lines.push(` ${style.flag(flag)}${placeholder}${pad}${style.dim(description)}`)
1023
1031
  }
1024
1032
 
1025
1033
  /**