@fraczak/k 6.0.3 → 6.1.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.
package/codecs/int.mjs CHANGED
@@ -17,6 +17,13 @@ import { stdin, stdout, argv, exit } from "node:process";
17
17
  import { Product, Variant } from "../Value.mjs";
18
18
  import { decodeWire, encodeToWire } from "./runtime/prefix-codec.mjs";
19
19
 
20
+ function usage(stream = console.error) {
21
+ stream(`Usage: ${argv[1]} --parse | --print`);
22
+ stream(" --parse Read a decimal integer from stdin, write binary pattern+value stream.");
23
+ stream(" --print Read binary pattern+value stream from stdin, write decimal integer.");
24
+ stream(" -h, --help Show this help.");
25
+ }
26
+
20
27
  // Closed pattern for $ int = < bits '+', bits '-' >
21
28
  // with $ bits = < {} _, bits 0, bits 1 >
22
29
  const INT_PATTERN = [
@@ -79,10 +86,13 @@ function printIntValue(value) {
79
86
 
80
87
  async function main() {
81
88
  const args = argv.slice(2);
89
+ if (args.includes("-h") || args.includes("--help")) {
90
+ usage(console.log);
91
+ exit(0);
92
+ }
93
+
82
94
  if (args.length !== 1 || (args[0] !== "--parse" && args[0] !== "--print")) {
83
- console.error("Usage: int.mjs --parse | --print");
84
- console.error(" --parse read a decimal integer from stdin, write binary pattern+value stream");
85
- console.error(" --print read binary pattern+value stream from stdin, write decimal integer");
95
+ usage();
86
96
  exit(1);
87
97
  }
88
98
 
package/codecs/json.mjs CHANGED
@@ -4,6 +4,13 @@ import { stdin, stdout, argv, exit } from "node:process";
4
4
  import { decodeWire, encodeToWire } from "./runtime/prefix-codec.mjs";
5
5
  import { fromJsonValue, toJsonValue, patternFromJsonValue } from "./json-codec.mjs";
6
6
 
7
+ function usage(stream = console.error) {
8
+ stream(`Usage: ${argv[1]} --parse | --print`);
9
+ stream(" --parse Read JSON from stdin, write binary pattern+value stream.");
10
+ stream(" --print Read binary pattern+value stream from stdin, write JSON.");
11
+ stream(" -h, --help Show this help.");
12
+ }
13
+
7
14
  function readAll(stream) {
8
15
  return new Promise((resolve, reject) => {
9
16
  const chunks = [];
@@ -15,10 +22,13 @@ function readAll(stream) {
15
22
 
16
23
  async function main() {
17
24
  const args = argv.slice(2);
25
+ if (args.includes("-h") || args.includes("--help")) {
26
+ usage(console.log);
27
+ exit(0);
28
+ }
29
+
18
30
  if (args.length !== 1 || (args[0] !== "--parse" && args[0] !== "--print")) {
19
- console.error("Usage: json.mjs --parse | --print");
20
- console.error(" --parse read JSON from stdin, write binary pattern+value stream");
21
- console.error(" --print read binary pattern+value stream from stdin, write JSON");
31
+ usage();
22
32
  exit(1);
23
33
  }
24
34
 
@@ -49,6 +49,11 @@ async function main() {
49
49
  const prog = argv[1];
50
50
  const args = argv.slice(2);
51
51
 
52
+ if (args.includes("-h") || args.includes("--help")) {
53
+ usage(prog);
54
+ return exit(0);
55
+ }
56
+
52
57
  let inputTypeArg = null;
53
58
  let inputPatternArg = null;
54
59
  let valueFile = null;
@@ -4,6 +4,15 @@ import fs from "node:fs";
4
4
  import { argv, stdin, stdout } from "node:process";
5
5
  import { decodeWire } from "./runtime/prefix-codec.mjs";
6
6
 
7
+ function usage(stream = console.error) {
8
+ stream(`Usage: ${argv[1]} [--debug] [file]`);
9
+ stream(" Read a binary pattern+value stream and print the decoded textual value.");
10
+ stream("");
11
+ stream("Options:");
12
+ stream(" --debug Print decoded pattern and value as JSON.");
13
+ stream(" -h, --help Show this help.");
14
+ }
15
+
7
16
  function readAll(stream) {
8
17
  return new Promise((resolve, reject) => {
9
18
  const chunks = [];
@@ -15,6 +24,11 @@ function readAll(stream) {
15
24
 
16
25
  async function main() {
17
26
  const args = argv.slice(2);
27
+ if (args.includes("-h") || args.includes("--help")) {
28
+ usage(console.log);
29
+ return;
30
+ }
31
+
18
32
  let debug = false;
19
33
  let fileArg = null;
20
34
 
@@ -24,7 +38,8 @@ async function main() {
24
38
  } else if (fileArg == null) {
25
39
  fileArg = arg;
26
40
  } else {
27
- throw new Error("Usage: k-print [--debug] [file]");
41
+ usage();
42
+ process.exit(1);
28
43
  }
29
44
  }
30
45
 
package/codecs/show.mjs CHANGED
@@ -5,6 +5,15 @@ import { argv, stdin, stdout, stderr } from "node:process";
5
5
  import { decodeWire } from "./runtime/prefix-codec.mjs";
6
6
  import { propertyListToFilter, valueToK } from "./runtime/show-value.mjs";
7
7
 
8
+ function usage(stream = console.error) {
9
+ stream(`Usage: ${argv[1]} [wire-file]`);
10
+ stream(" Pass the wire stream through unchanged on stdout.");
11
+ stream(" Print the decoded value and filter to stderr.");
12
+ stream("");
13
+ stream("Options:");
14
+ stream(" -h, --help Show this help.");
15
+ }
16
+
8
17
  function readAll(stream) {
9
18
  return new Promise((resolve, reject) => {
10
19
  const chunks = [];
@@ -16,10 +25,15 @@ function readAll(stream) {
16
25
 
17
26
  async function main() {
18
27
  const args = argv.slice(2);
28
+ if (args.includes("-h") || args.includes("--help")) {
29
+ usage(console.log);
30
+ return;
31
+ }
32
+
19
33
  let fileArg = null;
20
34
  for (const arg of args) {
21
35
  if (fileArg == null) fileArg = arg;
22
- else { console.error("Usage: show.mjs [wire-file]"); process.exit(1); }
36
+ else { usage(); process.exit(1); }
23
37
  }
24
38
 
25
39
  const buffer = fileArg ? fs.readFileSync(fileArg) : await readAll(stdin);
package/codecs/unit.mjs CHANGED
@@ -4,6 +4,13 @@ import { stdin, stdout, argv, exit } from "node:process";
4
4
  import { Product } from "../Value.mjs";
5
5
  import { decodeWire, encodeToWire } from "./runtime/prefix-codec.mjs";
6
6
 
7
+ function usage(stream = console.error) {
8
+ stream(`Usage: ${argv[1]} --parse | --print`);
9
+ stream(" --parse Ignore stdin and write the unit binary pattern+value stream.");
10
+ stream(" --print Validate the unit binary pattern+value stream and write {}.");
11
+ stream(" -h, --help Show this help.");
12
+ }
13
+
7
14
  const UNIT_PATTERN = [
8
15
  ["closed-product", []]
9
16
  ];
@@ -23,10 +30,13 @@ function unitEncoding() {
23
30
 
24
31
  async function main() {
25
32
  const args = argv.slice(2);
33
+ if (args.includes("-h") || args.includes("--help")) {
34
+ usage(console.log);
35
+ exit(0);
36
+ }
37
+
26
38
  if (args.length !== 1 || (args[0] !== "--parse" && args[0] !== "--print")) {
27
- console.error("Usage: unit.mjs --parse | --print");
28
- console.error(" --parse ignore stdin and write the unit binary pattern+value stream");
29
- console.error(" --print validate the unit binary pattern+value stream and write {}");
39
+ usage();
30
40
  exit(1);
31
41
  }
32
42
 
package/codecs/utf16.mjs CHANGED
@@ -3,6 +3,13 @@
3
3
  import { stdin, stdout, argv, exit } from "node:process";
4
4
  import { encodeText, decodeText } from "./string-codec.mjs";
5
5
 
6
+ function usage(stream = console.error) {
7
+ stream(`Usage: ${argv[1]} --parse | --print`);
8
+ stream(" --parse Read UTF-16 text from stdin (BOM-aware), write binary pattern+value stream of k string.");
9
+ stream(" --print Read binary pattern+value stream of k string, write UTF-16LE text with BOM.");
10
+ stream(" -h, --help Show this help.");
11
+ }
12
+
6
13
  function readAll(stream) {
7
14
  return new Promise((resolve, reject) => {
8
15
  const chunks = [];
@@ -43,10 +50,13 @@ function encodeUtf16Output(text) {
43
50
 
44
51
  async function main() {
45
52
  const args = argv.slice(2);
53
+ if (args.includes("-h") || args.includes("--help")) {
54
+ usage(console.log);
55
+ exit(0);
56
+ }
57
+
46
58
  if (args.length !== 1 || (args[0] !== "--parse" && args[0] !== "--print")) {
47
- console.error("Usage: utf16.mjs --parse | --print");
48
- console.error(" --parse read UTF-16 text from stdin (BOM-aware), write binary pattern+value stream of k string");
49
- console.error(" --print read binary pattern+value stream of k string, write UTF-16LE text with BOM");
59
+ usage();
50
60
  exit(1);
51
61
  }
52
62
 
package/codecs/utf8.mjs CHANGED
@@ -3,6 +3,13 @@
3
3
  import { stdin, stdout, argv, exit } from "node:process";
4
4
  import { encodeText, decodeText } from "./string-codec.mjs";
5
5
 
6
+ function usage(stream = console.error) {
7
+ stream(`Usage: ${argv[1]} --parse | --print`);
8
+ stream(" --parse Read UTF-8 text from stdin, write binary pattern+value stream of k string.");
9
+ stream(" --print Read binary pattern+value stream of k string, write UTF-8 text.");
10
+ stream(" -h, --help Show this help.");
11
+ }
12
+
6
13
  function readAll(stream) {
7
14
  return new Promise((resolve, reject) => {
8
15
  const chunks = [];
@@ -14,10 +21,13 @@ function readAll(stream) {
14
21
 
15
22
  async function main() {
16
23
  const args = argv.slice(2);
24
+ if (args.includes("-h") || args.includes("--help")) {
25
+ usage(console.log);
26
+ exit(0);
27
+ }
28
+
17
29
  if (args.length !== 1 || (args[0] !== "--parse" && args[0] !== "--print")) {
18
- console.error("Usage: utf8.mjs --parse | --print");
19
- console.error(" --parse read UTF-8 text from stdin, write binary pattern+value stream of k string");
20
- console.error(" --print read binary pattern+value stream of k string, write UTF-8 text");
30
+ usage();
21
31
  exit(1);
22
32
  }
23
33