@amritk/nish-x86_64-linux 0.44.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.
- package/bin/nish +0 -0
- package/docs/AI.md +18 -16
- package/package.json +1 -1
- package/runtime/core/numbers.mjs +3 -1
- package/runtime/core/strings.mjs +90 -38
- package/runtime/core.d.ts +8 -0
- package/runtime/core.mjs +16 -0
- package/runtime/nish.d.ts +47 -5
- package/runtime/nish.mjs +4 -0
- package/std/README.md +3 -3
- package/std/array.ts +10 -2
- package/std/bytes.ts +239 -26
- package/std/cli-args.ts +1072 -606
- package/std/cli-flags.ts +180 -98
- package/std/cli.ts +21 -13
- package/std/collections.ts +245 -127
- package/std/compress/checksum.ts +32 -37
- package/std/compress/deflate.ts +1102 -542
- package/std/compress/gzip.ts +290 -145
- package/std/compress/zlib.ts +57 -21
- package/std/crypto/aes.ts +153 -75
- package/std/crypto/base64url.ts +84 -61
- package/std/crypto/bcrypt-batch.ts +164 -90
- package/std/crypto/bcrypt.ts +185 -107
- package/std/crypto/chacha20poly1305.ts +84 -68
- package/std/crypto/ct.ts +9 -7
- package/std/crypto/ed25519.ts +64 -27
- package/std/crypto/hkdf.ts +105 -63
- package/std/crypto/hmac.ts +95 -89
- package/std/crypto/jwt.ts +248 -126
- package/std/crypto/p256.ts +758 -444
- package/std/crypto/p384.ts +20 -13
- package/std/crypto/pki-roots.ts +95 -38
- package/std/crypto/pki.ts +164 -60
- package/std/crypto/rsa.ts +266 -223
- package/std/crypto/scram.ts +3 -4
- package/std/crypto/sha1.ts +107 -65
- package/std/crypto/sha256.ts +502 -422
- package/std/crypto/sha512.ts +204 -135
- package/std/crypto/x509-random.ts +9 -3
- package/std/crypto/x509.ts +331 -171
- package/std/json-equal.ts +35 -19
- package/std/json-format.ts +215 -108
- package/std/json-number.ts +126 -72
- package/std/json-pointer.ts +42 -13
- package/std/json-tokenizer.ts +498 -307
- package/std/json-typed.ts +115 -36
- package/std/json-value.ts +341 -152
- package/std/json.ts +300 -158
- package/std/net/buffer-pool.ts +17 -7
- package/std/net/dns-config.ts +427 -236
- package/std/net/dns-message.ts +114 -47
- package/std/net/dns-resolver.ts +736 -237
- package/std/net/dns.ts +205 -52
- package/std/net/fetch.ts +320 -184
- package/std/net/grpc.ts +948 -0
- package/std/net/handler.ts +1275 -217
- package/std/net/hpack.ts +395 -180
- package/std/net/http-compress.ts +502 -65
- package/std/net/http-fields.ts +85 -49
- package/std/net/http-server.ts +1764 -602
- package/std/net/http1-client-codec.ts +372 -161
- package/std/net/http1-codec.ts +665 -331
- package/std/net/http1-server.ts +660 -278
- package/std/net/http2-cleartext.ts +165 -95
- package/std/net/http2-frame.ts +232 -89
- package/std/net/http2-tls.ts +42 -12
- package/std/net/http2.ts +327 -101
- package/std/net/http3-frame.ts +68 -33
- package/std/net/http3-server.ts +647 -192
- package/std/net/http3.ts +667 -189
- package/std/net/oidc.ts +63 -24
- package/std/net/outbound.ts +207 -142
- package/std/net/postgres-async.ts +1165 -443
- package/std/net/postgres-carrier.ts +575 -372
- package/std/net/postgres.ts +1459 -559
- package/std/net/prefork.ts +254 -98
- package/std/net/protobuf.ts +1017 -0
- package/std/net/qpack.ts +143 -92
- package/std/net/quic-conn-ack.ts +50 -18
- package/std/net/quic-conn-cid.ts +66 -26
- package/std/net/quic-conn-params.ts +13 -7
- package/std/net/quic-datagram.ts +90 -27
- package/std/net/quic-frame.ts +225 -124
- package/std/net/quic-listener.ts +197 -89
- package/std/net/quic-packet.ts +289 -163
- package/std/net/quic-recovery.ts +279 -128
- package/std/net/quic-stream.ts +466 -167
- package/std/net/quic.ts +568 -257
- package/std/net/router.ts +450 -99
- package/std/net/serve.ts +651 -232
- package/std/net/static.ts +1458 -0
- package/std/net/tls/client.ts +140 -45
- package/std/net/tls/codec.ts +510 -260
- package/std/net/tls/record-client.ts +268 -76
- package/std/net/tls/record-server.ts +236 -79
- package/std/net/tls/record.ts +173 -47
- package/std/net/tls/schedule.ts +58 -39
- package/std/net/tls/ticket.ts +295 -103
- package/std/net/tls/verify.ts +1 -1
- package/std/net/tls-tcp.ts +218 -75
- package/std/net/tls.ts +260 -152
- package/std/net/upstream.ts +283 -86
- package/std/net/url.ts +277 -102
- package/std/net/websocket.ts +370 -186
- package/std/net/webtransport.ts +755 -436
- package/std/regex.ts +1659 -829
- package/std/sync/ring.ts +320 -156
- package/std/sync/shm.ts +53 -6
- package/std/text.ts +205 -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
|
-
|
|
163
|
-
|
|
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 =
|
|
170
|
+
exponent = true
|
|
169
171
|
state = 4
|
|
170
172
|
} else {
|
|
171
|
-
mantissa =
|
|
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
|
-
|
|
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
|
|
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
|
|
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
|
-
/**
|
|
230
|
-
|
|
231
|
-
for (
|
|
232
|
-
if (
|
|
233
|
-
return
|
|
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
|
|
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.
|
|
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
|
|
251
|
-
const
|
|
252
|
-
|
|
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
|
|
350
|
-
*
|
|
351
|
-
*
|
|
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[]):
|
|
354
|
-
let best:
|
|
357
|
+
private matchCommand(words: string[]): CommandSpec | null {
|
|
358
|
+
let best: CommandSpec | null = null
|
|
355
359
|
let bestLength: i32 = 0
|
|
356
|
-
for (
|
|
357
|
-
const
|
|
358
|
-
|
|
359
|
-
|
|
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:
|
|
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
|
|
432
|
+
} else if (pending !== null) {
|
|
430
433
|
got.take(pending, pendingFlag, arg, pendingArg)
|
|
431
|
-
pending =
|
|
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
|
-
|
|
442
|
-
|
|
443
|
-
let
|
|
444
|
-
let
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
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
|
-
|
|
456
|
+
const found = got.lookup(flag)
|
|
457
|
+
if (found === null) {
|
|
453
458
|
got.fail(FLAG_ERR_UNKNOWN, `unknown flag ${flag}`, arg)
|
|
454
|
-
} else {
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
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
|
-
|
|
469
|
-
|
|
470
|
-
|
|
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
|
|
476
|
-
const 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
|
|
482
|
-
if (
|
|
483
|
-
got.
|
|
484
|
-
got.command =
|
|
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
|
-
|
|
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.
|
|
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
|
-
|
|
593
|
-
|
|
594
|
-
this.
|
|
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
|
|
611
|
-
take(
|
|
612
|
-
const 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
|
-
|
|
615
|
-
|
|
690
|
+
into.list.push(value)
|
|
691
|
+
into.given = true
|
|
616
692
|
return
|
|
617
693
|
}
|
|
618
|
-
if (
|
|
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
|
-
|
|
707
|
+
into.number = n
|
|
632
708
|
}
|
|
633
|
-
|
|
634
|
-
|
|
709
|
+
into.text = value
|
|
710
|
+
into.given = true
|
|
635
711
|
}
|
|
636
712
|
|
|
637
|
-
/**
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
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
|
-
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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
|
-
|
|
707
|
-
|
|
708
|
-
|
|
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
|
-
|
|
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
|
-
|
|
721
|
-
|
|
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 `
|
|
899
|
-
* when that would leave fewer, so a long flag still keeps
|
|
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 = (
|
|
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
|
|
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.
|
|
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.
|
|
990
|
-
|
|
991
|
-
const
|
|
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.
|
|
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
|
-
|
|
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
|
/**
|