@amritk/nish-x86_64-linux 0.50.0 → 0.51.0

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 (54) hide show
  1. package/bin/nish +0 -0
  2. package/docs/AI.md +4 -1
  3. package/package.json +1 -1
  4. package/runtime/nish.d.ts +82 -2
  5. package/runtime/nish.h +5 -0
  6. package/runtime/runtime-host.c +21 -5
  7. package/std/README.md +7 -7
  8. package/std/base64.ts +61 -95
  9. package/std/bytes.ts +6 -4
  10. package/std/cli-args.ts +150 -107
  11. package/std/compress/checksum.ts +68 -10
  12. package/std/compress/deflate.ts +193 -100
  13. package/std/compress/gzip.ts +124 -49
  14. package/std/compress/zlib.ts +57 -15
  15. package/std/crypto/aes.ts +51 -9
  16. package/std/crypto/ed25519.ts +12 -10
  17. package/std/crypto/hkdf.ts +405 -283
  18. package/std/crypto/hmac.ts +483 -148
  19. package/std/crypto/jwt.ts +19 -21
  20. package/std/crypto/p256.ts +5 -7
  21. package/std/crypto/pki-roots.ts +28 -8
  22. package/std/crypto/rsa-sign.ts +5 -2
  23. package/std/crypto/sha256.ts +81 -26
  24. package/std/crypto/sha512.ts +135 -49
  25. package/std/json-value.ts +14 -15
  26. package/std/net/buffer-pool.ts +25 -14
  27. package/std/net/fetch.ts +108 -27
  28. package/std/net/handler.ts +11 -22
  29. package/std/net/hpack.ts +38 -19
  30. package/std/net/http-compress.ts +21 -12
  31. package/std/net/http-server.ts +9 -4
  32. package/std/net/http1-client-codec.ts +22 -15
  33. package/std/net/http1-codec.ts +37 -36
  34. package/std/net/http3-server.ts +127 -45
  35. package/std/net/http3.ts +99 -50
  36. package/std/net/postgres-async.ts +5 -10
  37. package/std/net/qpack.ts +83 -17
  38. package/std/net/quic-listener.ts +42 -27
  39. package/std/net/quic-packet.ts +91 -50
  40. package/std/net/quic-stream.ts +29 -22
  41. package/std/net/quic.ts +48 -23
  42. package/std/net/static.ts +5 -3
  43. package/std/net/tls/client.ts +81 -34
  44. package/std/net/tls/codec.ts +3 -4
  45. package/std/net/tls/record.ts +54 -22
  46. package/std/net/tls/schedule.ts +339 -149
  47. package/std/net/tls.ts +42 -12
  48. package/std/net/upstream.ts +18 -19
  49. package/std/net/url.ts +78 -21
  50. package/std/net/websocket.ts +3 -11
  51. package/std/net/webtransport.ts +95 -21
  52. package/std/raster.ts +12 -2
  53. package/std/term-pixels.ts +18 -2
  54. package/std/text.ts +5 -5
package/std/bytes.ts CHANGED
@@ -71,9 +71,9 @@
71
71
  * };
72
72
  *
73
73
  * **The four runs clamp their range and never panic; `copyBytes` panics on a
74
- * range outside its array.** Natively each is a runtime kernel
75
- * (docs/LANGUAGE.md, "Byte runs"), and the runs are variable-time: they are
76
- * for text, never for secret data.
74
+ * range outside its array, a contract its caller proves (or `tryCopyBytes`).**
75
+ * Natively each is a runtime kernel (docs/LANGUAGE.md, "Byte runs"), and the
76
+ * runs are variable-time: they are for text, never for secret data.
77
77
  */
78
78
 
79
79
  /**
@@ -630,9 +630,11 @@ export const skipJsonPlain = (b: u8[], from: i32, to: i32): i32 => {
630
630
  * @param count How many bytes to copy; zero copies nothing.
631
631
  */
632
632
  export const copyBytes = (dst: u8[], at: i32, src: readonly u8[], from: i32, count: i32): void => {
633
- if (!tryCopyBytes(dst, at, src, from, count)) {
633
+ // A caller contract (`src/caller-contract.ts`): each call proves the range.
634
+ if (count < 0 || at < 0 || from < 0 || count > toI32(dst.length) - at || count > toI32(src.length) - from) {
634
635
  panic("copyBytes: the range lies outside an array")
635
636
  }
637
+ copyBytesWalk(dst, at, src, from, count)
636
638
  }
637
639
 
638
640
  /**
package/std/cli-args.ts CHANGED
@@ -18,12 +18,17 @@
18
18
  * **A builder, not a table.** Nish has no function values, so a command
19
19
  * cannot carry its handler; what a builder gives instead is a handle for
20
20
  * every command, flag and argument, so the program reads a value by the
21
- * handle it declared rather than by a name a typo can miss, and every
22
- * declaration mistake panics at the call that made it. Each declaration has
23
- * a `try` twin (`tryCommand`, `tryFlag`, ...) that answers the same mistake
24
- * as an `Err` instead, and the twins that declare a flag or an argument
25
- * answer a `CliFlag` or `CliArgument` that `ParseResult`'s `read` methods
26
- * take, so a program that uses them reaches no panic.
21
+ * handle it declared rather than by a name a typo can miss. A declaration
22
+ * mistake never panics: the call that made it answers `NO_HANDLE` (or
23
+ * nothing), the tree keeps the first such mistake, and every `parse` answers
24
+ * it as `PARSE_ERROR`, so `finish` prints it and exits 2 on every run of the
25
+ * program, whatever its arguments. A read by a handle no declaration answered
26
+ * answers what an unset flag or argument does: false, null or none. Each
27
+ * declaration has a `try` twin (`tryCommand`, `tryFlag`, ...) that answers
28
+ * the same mistake as an `Err` at the call instead, and the twins that
29
+ * declare a flag or an argument answer a `CliFlag` or `CliArgument` that
30
+ * `ParseResult`'s `read` methods take, so no handle can be one no
31
+ * declaration answered.
27
32
  *
28
33
  * **What it costs.** Declaring is linear in the declarations and builds no
29
34
  * help text. Parsing walks the arguments once, with one map lookup per
@@ -39,6 +44,12 @@ import { HelpMenu, Style } from "nish/cli"
39
44
  /** The handle of the tree's root, the program itself. */
40
45
  export const ROOT: i32 = 0
41
46
 
47
+ /**
48
+ * What a declaration answers for a mistake, which no declaration's handle
49
+ * is: the tree keeps the mistake, and `Cli.parse` answers it as `PARSE_ERROR`.
50
+ */
51
+ export const NO_HANDLE: i32 = -1
52
+
42
53
  /** `ParseResult.kind`: the arguments name a command to run. */
43
54
  export const PARSE_OK: i32 = 0
44
55
 
@@ -475,6 +486,8 @@ export class Cli {
475
486
  /** @internal Used by `ParseResult`. Every required flag, in the order `required` made it so. */
476
487
  requiredFlags: CliFlag[]
477
488
  private argList: CliArgument[]
489
+ /** The first declaration mistake, which every `parse` answers; empty for none. */
490
+ private mistake: string
478
491
 
479
492
  /**
480
493
  * A tree with only its root, the program `name` at `version`, for
@@ -500,6 +513,26 @@ export class Cli {
500
513
  this.negations = new Map<string, CliFlag>()
501
514
  this.requiredFlags = []
502
515
  this.argList = []
516
+ this.mistake = ""
517
+ }
518
+
519
+ /**
520
+ * The first declaration mistake the tree has kept, which every `parse`
521
+ * answers as `PARSE_ERROR`.
522
+ *
523
+ * @returns the error a `try` twin would have answered for it, such as
524
+ * `"cli-args: flag(\"--x <\"): bad flag spec: ..."`; empty when every
525
+ * declaration so far was right
526
+ */
527
+ declarationError(): string {
528
+ return this.mistake
529
+ }
530
+
531
+ /** Keeps `text` as the tree's mistake, unless one is already kept: the first wins. */
532
+ private keep(text: string): void {
533
+ if (this.mistake.length === 0) {
534
+ this.mistake = text
535
+ }
503
536
  }
504
537
 
505
538
  /** The command `handle`, or why it is none; `call` names the declaration in the error. */
@@ -518,23 +551,20 @@ export class Cli {
518
551
  return Err(`cli-args: ${call}: ${handle} is not a flag handle`)
519
552
  }
520
553
 
521
- /** @internal Called by `ParseResult`. The flag `handle`, or a panic naming `call` when there is none. */
522
- checkFlag(call: string, handle: i32): CliFlag {
523
- const f = this.flagAt(call, handle)
524
- if (f.isErr()) {
525
- // A handle no declaration answered: the program's own mistake.
526
- panic(f.error)
554
+ /** @internal Called by `ParseResult`. The flag `handle`, or null when no declaration answered it. */
555
+ checkFlag(handle: i32): CliFlag | null {
556
+ if (handle >= 0 && handle < toI32(this.flagList.length)) {
557
+ return this.flagList[handle]
527
558
  }
528
- return f.value
559
+ return null
529
560
  }
530
561
 
531
- /** @internal Called by `ParseResult`. The argument `handle`, or a panic naming `call` when there is none. */
532
- checkArgument(call: string, handle: i32): CliArgument {
533
- if (!(handle >= 0 && handle < toI32(this.argList.length))) {
534
- // A handle no declaration answered: the program's own mistake.
535
- panic(`cli-args: ${call}: ${handle} is not an argument handle`)
562
+ /** @internal Called by `ParseResult`. The argument `handle`, or null when no declaration answered it. */
563
+ checkArgument(handle: i32): CliArgument | null {
564
+ if (handle >= 0 && handle < toI32(this.argList.length)) {
565
+ return this.argList[handle]
536
566
  }
537
- return this.argList[handle]
567
+ return null
538
568
  }
539
569
 
540
570
  /** A new child of `parent`, or why it cannot be; `call` names the declaration in the error. */
@@ -584,12 +614,12 @@ export class Cli {
584
614
  }
585
615
 
586
616
  /**
587
- * `group`, answering a mistake rather than panicking on it.
617
+ * `group`, answering a mistake as an `Err` at the call.
588
618
  *
589
619
  * @param parent the handle of the group (or `ROOT`) the group goes in
590
620
  * @param name the word that names the group on the command line
591
621
  * @param description the group's line in its parent's help, and in its own
592
- * @returns the group's handle, or the error `group` would panic with, and
622
+ * @returns the group's handle, or the error `group` would keep, and
593
623
  * then the tree is unchanged
594
624
  */
595
625
  tryGroup(parent: i32, name: string, description: string): Result<i32, string> {
@@ -605,25 +635,25 @@ export class Cli {
605
635
  * an error.
606
636
  *
607
637
  * @param parent the handle of the group (or `ROOT`) the group goes in;
608
- * panics on a command, or on one that takes arguments or a required flag
609
- * @param name the word that names the group on the command line; panics
638
+ * a mistake on a command, or on one that takes arguments or a required flag
639
+ * @param name the word that names the group on the command line; a mistake
610
640
  * when it is empty, has a space or `=`, starts with `-`, or is already a
611
641
  * name in `parent`
612
642
  * @param description the group's line in its parent's help, and in its own
613
643
  * @returns the group's handle, for `command`, `group`, `flag`, `alias` and
614
- * `epilog`
644
+ * `epilog`, or `NO_HANDLE` for a mistake
615
645
  */
616
646
  group(parent: i32, name: string, description: string): i32 {
617
647
  const c = this.tryGroup(parent, name, description)
618
648
  if (c.isErr()) {
619
- // A declaration mistake, which the program's own code makes.
620
- panic(c.error)
649
+ this.keep(c.error)
650
+ return NO_HANDLE
621
651
  }
622
652
  return c.value
623
653
  }
624
654
 
625
655
  /**
626
- * `command`, answering a mistake rather than panicking on it.
656
+ * `command`, answering a mistake as an `Err` at the call.
627
657
  *
628
658
  * @param parent the handle of the group (or `ROOT`) the command goes in
629
659
  * @param name the word that names the command on the command line
@@ -631,7 +661,7 @@ export class Cli {
631
661
  * command: not negative, and no other command's
632
662
  * @param description the command's line in its parent's help, and in its
633
663
  * own usage line
634
- * @returns the command's handle, or the error `command` would panic with,
664
+ * @returns the command's handle, or the error `command` would keep,
635
665
  * and then the tree is unchanged
636
666
  */
637
667
  tryCommand(parent: i32, name: string, id: i32, description: string): Result<i32, string> {
@@ -657,34 +687,34 @@ export class Cli {
657
687
  * `ParseResult.id`: not negative, which is a group's, and no other command's.
658
688
  *
659
689
  * @param parent the handle of the group (or `ROOT`) the command goes in;
660
- * panics on a command, or on one that takes arguments or a required flag
661
- * @param name the word that names the command on the command line; panics
690
+ * a mistake on a command, or on one that takes arguments or a required flag
691
+ * @param name the word that names the command on the command line; a mistake
662
692
  * when it is empty, has a space or `=`, starts with `-`, or is already a
663
693
  * name in `parent`
664
694
  * @param id what `ParseResult.id` answers when the arguments name this
665
- * command, so the program can switch on it; panics when negative or
695
+ * command, so the program can switch on it; a mistake when negative or
666
696
  * already another command's
667
697
  * @param description the command's line in its parent's help, and in its
668
698
  * own usage line
669
699
  * @returns the command's handle, for `flag`, `argument`, `alias` and
670
- * `epilog`
700
+ * `epilog`, or `NO_HANDLE` for a mistake
671
701
  */
672
702
  command(parent: i32, name: string, id: i32, description: string): i32 {
673
703
  const c = this.tryCommand(parent, name, id, description)
674
704
  if (c.isErr()) {
675
- // A declaration mistake, which the program's own code makes.
676
- panic(c.error)
705
+ this.keep(c.error)
706
+ return NO_HANDLE
677
707
  }
678
708
  return c.value
679
709
  }
680
710
 
681
711
  /**
682
- * `alias`, answering a mistake rather than panicking on it.
712
+ * `alias`, answering a mistake as an `Err` at the call.
683
713
  *
684
714
  * @param handle the command or group the alias names
685
715
  * @param name the other word, which the parent's help lists beside the
686
716
  * first
687
- * @returns nothing, or the error `alias` would panic with, and then the
717
+ * @returns nothing, or the error `alias` would keep, and then the
688
718
  * tree is unchanged
689
719
  */
690
720
  tryAlias(handle: i32, name: string): Result<void, string> {
@@ -713,17 +743,16 @@ export class Cli {
713
743
  /**
714
744
  * Another name for the command or group `handle`, in the same scope.
715
745
  *
716
- * @param handle the command or group the alias names; panics on `ROOT`
746
+ * @param handle the command or group the alias names; a mistake on `ROOT`
717
747
  * or a handle no declaration answered
718
748
  * @param name the other word, which the parent's help lists beside the
719
- * first; panics when it is not a command name or is already a name in
749
+ * first; a mistake when it is not a command name or is already a name in
720
750
  * that scope
721
751
  */
722
752
  alias(handle: i32, name: string): void {
723
753
  const r = this.tryAlias(handle, name)
724
754
  if (r.isErr()) {
725
- // A declaration mistake, which the program's own code makes.
726
- panic(r.error)
755
+ this.keep(r.error)
727
756
  }
728
757
  }
729
758
 
@@ -788,13 +817,13 @@ export class Cli {
788
817
  }
789
818
 
790
819
  /**
791
- * `globalFlag`, answering a mistake rather than panicking on it.
820
+ * `globalFlag`, answering a mistake as an `Err` at the call.
792
821
  *
793
822
  * @param spec the flag as the help writes it, in `globalFlag`'s syntax
794
823
  * @param description the flag's line in the `Global flags` help section
795
824
  * @returns the flag, for `ParseResult`'s `read` methods and, by its
796
825
  * `handle`, for the methods that take one; or the error `globalFlag`
797
- * would panic with, and then the tree is unchanged
826
+ * would keep, and then the tree is unchanged
798
827
  */
799
828
  tryGlobalFlag(spec: string, description: string): Result<CliFlag, string> {
800
829
  return this.addFlag(`globalFlag("${spec}")`, GLOBAL, this.root, spec, description)
@@ -806,24 +835,24 @@ export class Cli {
806
835
  * @param spec the flag as the help writes it: `--name`, optionally after
807
836
  * `-x, ` for a one-letter short form, optionally followed by ` <value>`
808
837
  * (required) or ` [value]` (optional); `--no-x` with no value is a flag
809
- * that is on until given. Panics on a malformed spec, on `-h`, `--help`,
838
+ * that is on until given. A mistake on a malformed spec, on `-h`, `--help`,
810
839
  * `-v` or `--version`, and on a name any flag already answers to
811
840
  * @param description the flag's line in the `Global flags` help section
812
841
  * @returns the flag's handle, for `ParseResult.flag`, `value`, `values`
813
842
  * and `source`, and for `defaultValue`, `required`, `repeatable` and
814
- * `negatable`
843
+ * `negatable`, or `NO_HANDLE` for a mistake
815
844
  */
816
845
  globalFlag(spec: string, description: string): i32 {
817
846
  const f = this.tryGlobalFlag(spec, description)
818
847
  if (f.isErr()) {
819
- // A declaration mistake, which the program's own code makes.
820
- panic(f.error)
848
+ this.keep(f.error)
849
+ return NO_HANDLE
821
850
  }
822
851
  return f.value.handle
823
852
  }
824
853
 
825
854
  /**
826
- * `flag`, answering a mistake rather than panicking on it.
855
+ * `flag`, answering a mistake as an `Err` at the call.
827
856
  *
828
857
  * @param cmd the handle of the command or group (or `ROOT`) that owns the
829
858
  * flag
@@ -831,7 +860,7 @@ export class Cli {
831
860
  * @param description the flag's line in `cmd`'s `Flags` help section
832
861
  * @returns the flag, for `ParseResult`'s `read` methods and, by its
833
862
  * `handle`, for the methods that take one; or the error `flag` would
834
- * panic with, and then the tree is unchanged
863
+ * keep, and then the tree is unchanged
835
864
  */
836
865
  tryFlag(cmd: i32, spec: string, description: string): Result<CliFlag, string> {
837
866
  const call = `flag("${spec}")`
@@ -848,29 +877,29 @@ export class Cli {
848
877
  * @param cmd the handle of the command or group (or `ROOT`) that owns the
849
878
  * flag; the flag is unknown everywhere else in the tree
850
879
  * @param spec the flag as the help writes it, in `globalFlag`'s syntax;
851
- * panics on a malformed spec, on `-h`, `--help`, `-v` or `--version`,
880
+ * a mistake on a malformed spec, on `-h`, `--help`, `-v` or `--version`,
852
881
  * and on a name `cmd` or a global flag already answers to
853
882
  * @param description the flag's line in `cmd`'s `Flags` help section
854
883
  * @returns the flag's handle, for `ParseResult.flag`, `value`, `values`
855
884
  * and `source`, and for `defaultValue`, `required`, `repeatable` and
856
- * `negatable`
885
+ * `negatable`, or `NO_HANDLE` for a mistake
857
886
  */
858
887
  flag(cmd: i32, spec: string, description: string): i32 {
859
888
  const f = this.tryFlag(cmd, spec, description)
860
889
  if (f.isErr()) {
861
- // A declaration mistake, which the program's own code makes.
862
- panic(f.error)
890
+ this.keep(f.error)
891
+ return NO_HANDLE
863
892
  }
864
893
  return f.value.handle
865
894
  }
866
895
 
867
896
  /**
868
- * `defaultValue`, answering a mistake rather than panicking on it.
897
+ * `defaultValue`, answering a mistake as an `Err` at the call.
869
898
  *
870
899
  * @param f the handle of a flag that takes a value and is not required
871
900
  * @param value what `value` and `values` answer when the arguments do not
872
901
  * give the flag
873
- * @returns nothing, or the error `defaultValue` would panic with, and then
902
+ * @returns nothing, or the error `defaultValue` would keep, and then
874
903
  * the flag is unchanged
875
904
  */
876
905
  tryDefaultValue(f: i32, value: string): Result<void, string> {
@@ -894,7 +923,7 @@ export class Cli {
894
923
  /**
895
924
  * The value flag `f` answers when it is not given.
896
925
  *
897
- * @param f the handle of a flag that takes a value; panics on one that
926
+ * @param f the handle of a flag that takes a value; a mistake on one that
898
927
  * takes none or is required
899
928
  * @param value what `value` and `values` answer when the arguments do not
900
929
  * give the flag, and what its help row shows as `(default: …)`; a later
@@ -903,16 +932,15 @@ export class Cli {
903
932
  defaultValue(f: i32, value: string): void {
904
933
  const r = this.tryDefaultValue(f, value)
905
934
  if (r.isErr()) {
906
- // A declaration mistake, which the program's own code makes.
907
- panic(r.error)
935
+ this.keep(r.error)
908
936
  }
909
937
  }
910
938
 
911
939
  /**
912
- * `required`, answering a mistake rather than panicking on it.
940
+ * `required`, answering a mistake as an `Err` at the call.
913
941
  *
914
942
  * @param f the handle of a command's or a global flag without a default
915
- * @returns nothing, or the error `required` would panic with, and then the
943
+ * @returns nothing, or the error `required` would keep, and then the
916
944
  * flag is unchanged
917
945
  */
918
946
  tryRequired(f: i32): Result<void, string> {
@@ -941,22 +969,21 @@ export class Cli {
941
969
  * Makes `f` an error to leave out, for the command that owns it (every
942
970
  * command, for a global); a group's flag cannot be, as no parse ends at one.
943
971
  *
944
- * @param f the handle of the flag; panics on a group's flag or one with a
972
+ * @param f the handle of the flag; a mistake on a group's flag or one with a
945
973
  * default. Calling it twice is the same as once
946
974
  */
947
975
  required(f: i32): void {
948
976
  const r = this.tryRequired(f)
949
977
  if (r.isErr()) {
950
- // A declaration mistake, which the program's own code makes.
951
- panic(r.error)
978
+ this.keep(r.error)
952
979
  }
953
980
  }
954
981
 
955
982
  /**
956
- * `repeatable`, answering a mistake rather than panicking on it.
983
+ * `repeatable`, answering a mistake as an `Err` at the call.
957
984
  *
958
985
  * @param f the handle of a flag that takes a value
959
- * @returns nothing, or the error `repeatable` would panic with, and then
986
+ * @returns nothing, or the error `repeatable` would keep, and then
960
987
  * the flag is unchanged
961
988
  */
962
989
  tryRepeatable(f: i32): Result<void, string> {
@@ -976,22 +1003,21 @@ export class Cli {
976
1003
  * Makes `f` gather every value it is given, in order, for
977
1004
  * `ParseResult.values`.
978
1005
  *
979
- * @param f the handle of a flag that takes a value; panics on one that
1006
+ * @param f the handle of a flag that takes a value; a mistake on one that
980
1007
  * takes none
981
1008
  */
982
1009
  repeatable(f: i32): void {
983
1010
  const r = this.tryRepeatable(f)
984
1011
  if (r.isErr()) {
985
- // A declaration mistake, which the program's own code makes.
986
- panic(r.error)
1012
+ this.keep(r.error)
987
1013
  }
988
1014
  }
989
1015
 
990
1016
  /**
991
- * `negatable`, answering a mistake rather than panicking on it.
1017
+ * `negatable`, answering a mistake as an `Err` at the call.
992
1018
  *
993
1019
  * @param f the handle of a flag declared `--x` with no value
994
- * @returns nothing, or the error `negatable` would panic with, and then
1020
+ * @returns nothing, or the error `negatable` would keep, and then
995
1021
  * the flag is unchanged
996
1022
  */
997
1023
  tryNegatable(f: i32): Result<void, string> {
@@ -1022,20 +1048,19 @@ export class Cli {
1022
1048
  /**
1023
1049
  * Adds `--no-x` to the boolean flag `--x`, which turns it back off.
1024
1050
  *
1025
- * @param f the handle of a flag declared `--x` with no value; panics on a
1051
+ * @param f the handle of a flag declared `--x` with no value; a mistake on a
1026
1052
  * flag that takes a value, one declared `--no-x`, or when `--no-x` is
1027
1053
  * already a name a flag in its reach answers to
1028
1054
  */
1029
1055
  negatable(f: i32): void {
1030
1056
  const r = this.tryNegatable(f)
1031
1057
  if (r.isErr()) {
1032
- // A declaration mistake, which the program's own code makes.
1033
- panic(r.error)
1058
+ this.keep(r.error)
1034
1059
  }
1035
1060
  }
1036
1061
 
1037
1062
  /**
1038
- * `argument`, answering a mistake rather than panicking on it.
1063
+ * `argument`, answering a mistake as an `Err` at the call.
1039
1064
  *
1040
1065
  * @param cmd the handle of the command that takes the argument
1041
1066
  * @param spec the argument as the help writes it, in `argument`'s forms
@@ -1043,7 +1068,7 @@ export class Cli {
1043
1068
  * section
1044
1069
  * @returns the argument, for `ParseResult.readPositional` and
1045
1070
  * `readPositionals` and, by its `handle`, for `positional` and
1046
- * `positionals`; or the error `argument` would panic with, and then the
1071
+ * `positionals`; or the error `argument` would keep, and then the
1047
1072
  * tree is unchanged
1048
1073
  */
1049
1074
  tryArgument(cmd: i32, spec: string, description: string): Result<CliArgument, string> {
@@ -1096,31 +1121,31 @@ export class Cli {
1096
1121
  * A positional argument of the command `cmd`: `<name>` required, `[name]`
1097
1122
  * optional, `<name...>` or `[name...]` taking every one left, last only.
1098
1123
  *
1099
- * @param cmd the handle of the command that takes the argument; panics on
1124
+ * @param cmd the handle of the command that takes the argument; a mistake on
1100
1125
  * a group, or on the root once it holds commands
1101
1126
  * @param spec the argument as the help writes it, one of the four forms
1102
- * above; panics on a malformed one, on any argument after a variadic one,
1127
+ * above; a mistake on a malformed one, on any argument after a variadic one,
1103
1128
  * and on a required one after an optional one
1104
1129
  * @param description the argument's line in `cmd`'s `Arguments` help
1105
1130
  * section
1106
1131
  * @returns the argument's handle, for `ParseResult.positional` and
1107
- * `positionals`
1132
+ * `positionals`, or `NO_HANDLE` for a mistake
1108
1133
  */
1109
1134
  argument(cmd: i32, spec: string, description: string): i32 {
1110
1135
  const a = this.tryArgument(cmd, spec, description)
1111
1136
  if (a.isErr()) {
1112
- // A declaration mistake, which the program's own code makes.
1113
- panic(a.error)
1137
+ this.keep(a.error)
1138
+ return NO_HANDLE
1114
1139
  }
1115
1140
  return a.value.handle
1116
1141
  }
1117
1142
 
1118
1143
  /**
1119
- * `epilog`, answering a mistake rather than panicking on it.
1144
+ * `epilog`, answering a mistake as an `Err` at the call.
1120
1145
  *
1121
1146
  * @param handle the command or group (or `ROOT`) whose help ends with it
1122
1147
  * @param text what follows a blank line after the last row
1123
- * @returns nothing, or the error `epilog` would panic with on a handle no
1148
+ * @returns nothing, or the error `epilog` would keep on a handle no
1124
1149
  * declaration answered
1125
1150
  */
1126
1151
  tryEpilog(handle: i32, text: string): Result<void, string> {
@@ -1143,8 +1168,7 @@ export class Cli {
1143
1168
  epilog(handle: i32, text: string): void {
1144
1169
  const r = this.tryEpilog(handle, text)
1145
1170
  if (r.isErr()) {
1146
- // A handle no declaration answered: the program's own mistake.
1147
- panic(r.error)
1171
+ this.keep(r.error)
1148
1172
  }
1149
1173
  }
1150
1174
 
@@ -1208,10 +1232,15 @@ export class Cli {
1208
1232
  * with commands is a missing-command error
1209
1233
  * @returns a fresh answer the caller owns, holding this tree to read its
1210
1234
  * values by handle; a usage error is its `PARSE_ERROR` kind, never a
1211
- * panic
1235
+ * panic, and so is a declaration mistake the tree kept, whatever
1236
+ * `argv` holds (`declarationError`)
1212
1237
  */
1213
1238
  parse(argv: string[], start: i32): ParseResult {
1214
1239
  const r = new ParseResult(this)
1240
+ if (this.mistake.length > 0) {
1241
+ r.declarationFailed(this.mistake)
1242
+ return r
1243
+ }
1215
1244
  let i: i32 = start < 0 ? 0 : start
1216
1245
  let dashed = false
1217
1246
  while (i >= 0 && i < toI32(argv.length)) {
@@ -1459,17 +1488,17 @@ export class Cli {
1459
1488
  /**
1460
1489
  * The help of the command or group `handle`, painted with `style`.
1461
1490
  *
1462
- * @param handle the command or group (or `ROOT`) whose help it is; panics
1463
- * on a handle no declaration answered
1491
+ * @param handle the command or group (or `ROOT`) whose help it is
1464
1492
  * @param style how the help's names and headings are painted; a style
1465
1493
  * that is off gives plain text
1466
- * @returns a fresh string, the whole page, built on each call
1494
+ * @returns a fresh string, the whole page, built on each call; empty for a
1495
+ * handle no declaration answered, whose mistake is kept for `parse`
1467
1496
  */
1468
1497
  helpText(handle: i32, style: Style): string {
1469
1498
  const c = this.commandAt("help", handle)
1470
1499
  if (c.isErr()) {
1471
- // A handle no declaration answered: the program's own mistake.
1472
- panic(c.error)
1500
+ this.keep(c.error)
1501
+ return ""
1473
1502
  }
1474
1503
  return this.helpOf(c.value, style)
1475
1504
  }
@@ -1584,6 +1613,14 @@ export class ParseResult {
1584
1613
  }
1585
1614
  }
1586
1615
 
1616
+ /** @internal Called by `Cli`. Answers the tree's declaration mistake `text` as `PARSE_ERROR`, whatever the arguments. */
1617
+ declarationFailed(text: string): void {
1618
+ this.fail(this.at, text)
1619
+ this.path = this.at.path
1620
+ this.kind = PARSE_ERROR
1621
+ this.exitCode = 2
1622
+ }
1623
+
1587
1624
  /** @internal Called by `Cli`. Records flag `f` as given `value`. */
1588
1625
  set(f: CliFlag, value: string): void {
1589
1626
  this.sets.push(new FlagSet(f.handle, value))
@@ -1785,73 +1822,79 @@ export class ParseResult {
1785
1822
  /**
1786
1823
  * Whether flag `f` is on, as `readFlag` answers.
1787
1824
  *
1788
- * @param f the handle of a flag anywhere in the tree; panics on a handle
1789
- * no declaration answered
1825
+ * @param f the handle of a flag anywhere in the tree; a handle no
1826
+ * declaration answered reads as a flag never given with no default
1790
1827
  * @returns the last of the arguments' settings for a boolean, so
1791
1828
  * `--x --no-x` is off
1792
1829
  */
1793
1830
  flag(f: i32): boolean {
1794
- return this.readFlag(this.cli.checkFlag("flag", f))
1831
+ const fl = this.cli.checkFlag(f)
1832
+ return fl === null ? false : this.readFlag(fl)
1795
1833
  }
1796
1834
 
1797
1835
  /**
1798
1836
  * The value of flag `f`, as `readValue` answers.
1799
1837
  *
1800
- * @param f the handle of a flag anywhere in the tree; panics on a handle
1801
- * no declaration answered
1838
+ * @param f the handle of a flag anywhere in the tree; a handle no
1839
+ * declaration answered reads as a flag never given with no default
1802
1840
  * @returns the value as written, or `"true"` or `"false"` for a boolean
1803
1841
  * that was given; null tells a flag never given and with no default
1804
1842
  */
1805
1843
  value(f: i32): string | null {
1806
- return this.readValue(this.cli.checkFlag("value", f))
1844
+ const fl = this.cli.checkFlag(f)
1845
+ return fl === null ? null : this.readValue(fl)
1807
1846
  }
1808
1847
 
1809
1848
  /**
1810
1849
  * Every value of flag `f`, as `readValues` answers.
1811
1850
  *
1812
- * @param f the handle of a flag anywhere in the tree; panics on a handle
1813
- * no declaration answered
1851
+ * @param f the handle of a flag anywhere in the tree; a handle no
1852
+ * declaration answered reads as a flag never given with no default
1814
1853
  * @returns a fresh array the caller owns
1815
1854
  */
1816
1855
  values(f: i32): string[] {
1817
- return this.readValues(this.cli.checkFlag("values", f))
1856
+ const fl = this.cli.checkFlag(f)
1857
+ return fl === null ? [] : this.readValues(fl)
1818
1858
  }
1819
1859
 
1820
1860
  /**
1821
1861
  * Where flag `f`'s value came from, as `readSource` answers.
1822
1862
  *
1823
- * @param f the handle of a flag anywhere in the tree; panics on a handle
1824
- * no declaration answered
1863
+ * @param f the handle of a flag anywhere in the tree; a handle no
1864
+ * declaration answered reads as a flag never given with no default
1825
1865
  * @returns `SOURCE_ARGV` when the arguments gave it, `SOURCE_DEFAULT` when
1826
1866
  * it has a default or is declared `--no-x` (on by default), else
1827
1867
  * `SOURCE_UNSET`
1828
1868
  */
1829
1869
  source(f: i32): i32 {
1830
- return this.readSource(this.cli.checkFlag("source", f))
1870
+ const fl = this.cli.checkFlag(f)
1871
+ return fl === null ? SOURCE_UNSET : this.readSource(fl)
1831
1872
  }
1832
1873
 
1833
1874
  /**
1834
1875
  * The argument `a`'s value, as `readPositional` answers.
1835
1876
  *
1836
- * @param a the handle of an argument; panics on a handle no `argument`
1837
- * call answered
1877
+ * @param a the handle of an argument; a handle no `argument`
1878
+ * call answered reads as an argument not given
1838
1879
  * @returns the word at the argument's place among the positionals, which
1839
1880
  * for a variadic one is the first it took
1840
1881
  */
1841
1882
  positional(a: i32): string | null {
1842
- return this.readPositional(this.cli.checkArgument("positional", a))
1883
+ const arg = this.cli.checkArgument(a)
1884
+ return arg === null ? null : this.readPositional(arg)
1843
1885
  }
1844
1886
 
1845
1887
  /**
1846
1888
  * Every value from argument `a` on, as `readPositionals` answers.
1847
1889
  *
1848
- * @param a the handle of an argument; panics on a handle no `argument`
1849
- * call answered
1890
+ * @param a the handle of an argument; a handle no `argument`
1891
+ * call answered reads as an argument not given
1850
1892
  * @returns a fresh array the caller owns; empty when the argument was not
1851
1893
  * given or belongs to another command
1852
1894
  */
1853
1895
  positionals(a: i32): string[] {
1854
- return this.readPositionals(this.cli.checkArgument("positionals", a))
1896
+ const arg = this.cli.checkArgument(a)
1897
+ return arg === null ? [] : this.readPositionals(arg)
1855
1898
  }
1856
1899
 
1857
1900
  /**