@gunshi/combinators 0.35.1 → 0.35.3

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/lib/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- //#region ../../node_modules/.pnpm/args-tokens@0.27.0/node_modules/args-tokens/lib/resolver.d.ts
1
+ //#region ../../node_modules/.pnpm/args-tokens@0.28.0/node_modules/args-tokens/lib/resolver.d.ts
2
2
 
3
3
  //#region src/resolver.d.ts
4
4
  /**
@@ -420,7 +420,7 @@ interface ArgSchema {
420
420
  *
421
421
  * @param value - Raw string value from command line
422
422
  * @returns Parsed value of any type
423
- * @throws Error or subclass when value is invalid
423
+ * @throws {Error} Error or subclass when value is invalid
424
424
  *
425
425
  * @example
426
426
  * Custom parsing functions:
@@ -452,6 +452,13 @@ interface ArgSchema {
452
452
  */
453
453
  parse?: (value: string) => any;
454
454
  }
455
+ /**
456
+ * Machine-readable error codes for {@link ArgsValidationError}.
457
+ *
458
+ * Each code identifies a validation failure category and is also suitable as an
459
+ * i18n resource key for localized rendering.
460
+ */
461
+
455
462
  /**
456
463
  * An object that contains {@link ArgSchema | argument schema}.
457
464
  *
@@ -466,12 +473,8 @@ interface Args {
466
473
  * @typeParam T - {@link Args | Arguments} which is an object that defines the command line arguments.
467
474
  */
468
475
  //#endregion
469
- //#region ../../node_modules/.pnpm/args-tokens@0.27.0/node_modules/args-tokens/lib/combinators.d.ts
476
+ //#region ../../node_modules/.pnpm/args-tokens@0.28.0/node_modules/args-tokens/lib/combinators.d.ts
470
477
  //#region src/combinators.d.ts
471
- /**
472
- * @author kazuya kawaguchi (a.k.a. kazupon)
473
- * @license MIT
474
- */
475
478
  /**
476
479
  * A combinator produced by combinator factory functions.
477
480
  *
package/lib/index.js CHANGED
@@ -1,9 +1,161 @@
1
- //#region ../../node_modules/.pnpm/args-tokens@0.27.0/node_modules/args-tokens/lib/combinators.js
1
+ //#region ../../node_modules/.pnpm/args-tokens@0.28.0/node_modules/args-tokens/lib/utils.js
2
2
  /**
3
3
  * @author kazuya kawaguchi (a.k.a. kazupon)
4
4
  * @license MIT
5
5
  */
6
6
  /**
7
+ * Format allowed argument choices for validation error values and fallback messages.
8
+ *
9
+ * @param choices - Choice values to format
10
+ * @returns JSON-quoted choices joined with `, `.
11
+ */
12
+ function formatChoices(choices) {
13
+ return choices.map((value) => JSON.stringify(value)).join(", ");
14
+ }
15
+ /**
16
+ * forked from `nodejs/node` (`pkgjs/parseargs`)
17
+ * repository url: https://github.com/nodejs/node (https://github.com/pkgjs/parseargs)
18
+ * code url: https://github.com/nodejs/node/blob/main/lib/internal/util/parse_args/parse_args.js
19
+ *
20
+ * @author kazuya kawaguchi (a.k.a. kazupon)
21
+ * @license MIT
22
+ */
23
+ "-".codePointAt(0);
24
+ "=".codePointAt(0);
25
+ //#endregion
26
+ //#region ../../node_modules/.pnpm/args-tokens@0.28.0/node_modules/args-tokens/lib/resolver.js
27
+ /**
28
+ * Entry point of argument options resolver.
29
+ *
30
+ * @module
31
+ */
32
+ /**
33
+ * @author kazuya kawaguchi (a.k.a. kazupon)
34
+ * @license MIT
35
+ */
36
+ /**
37
+ * Machine-readable error codes for {@link ArgsValidationError}.
38
+ *
39
+ * Each code identifies a validation failure category and is also suitable as an
40
+ * i18n resource key for localized rendering.
41
+ */
42
+ const ArgsValidationErrorKeys = {
43
+ requiredOption: "err:arg:required-option",
44
+ requiredPositional: "err:arg:required-positional",
45
+ invalidType: "err:arg:invalid-type",
46
+ invalidChoice: "err:arg:invalid-choice",
47
+ customParse: "err:arg:custom-parse",
48
+ unknownOption: "err:arg:unknown-option"
49
+ };
50
+ /**
51
+ * An error that contains structured metadata for argument validation failures.
52
+ *
53
+ * The `message` remains the English fallback message. Renderers can use `code`
54
+ * and `values` to localize the error, falling back to `message` when localization
55
+ * is unavailable.
56
+ */
57
+ var ArgsValidationError = class extends Error {
58
+ /**
59
+ * Machine-readable error code for this validation failure.
60
+ *
61
+ * This code can also be used as an i18n resource key.
62
+ */
63
+ code;
64
+ /**
65
+ * Interpolation values for `code`.
66
+ */
67
+ values;
68
+ /**
69
+ * Create an `ArgsValidationError` instance.
70
+ *
71
+ * @param message - fallback error message
72
+ * @param options - structured validation metadata
73
+ */
74
+ constructor(message, options = {}) {
75
+ super(message, { cause: options.cause });
76
+ this.name = "ArgsValidationError";
77
+ this.code = options.code;
78
+ this.values = options.values ?? {};
79
+ }
80
+ };
81
+ //#endregion
82
+ //#region ../../node_modules/.pnpm/args-tokens@0.28.0/node_modules/args-tokens/lib/combinators.js
83
+ /**
84
+ * Parser combinator factory functions for composable argument schema construction.
85
+ *
86
+ * @example
87
+ * ```ts
88
+ * import { parseArgs, resolveArgs } from 'args-tokens'
89
+ * import {
90
+ * args,
91
+ * string,
92
+ * integer,
93
+ * boolean,
94
+ * positional,
95
+ * choice,
96
+ * withDefault,
97
+ * multiple,
98
+ * required,
99
+ * short,
100
+ * map,
101
+ * merge,
102
+ * extend
103
+ * } from 'args-tokens/combinators'
104
+ *
105
+ * // Define reusable schema groups with args()
106
+ * const common = args({
107
+ * help: short(boolean(), 'h'),
108
+ * verbose: boolean()
109
+ * })
110
+ *
111
+ * const network = args({
112
+ * port: short(withDefault(integer({ min: 1, max: 65535 }), 8080), 'p'),
113
+ * host: required(short(string({ minLength: 1 }), 'o'))
114
+ * })
115
+ *
116
+ * // Compose schemas with merge()
117
+ * const schema = merge(
118
+ * common,
119
+ * network,
120
+ * args({
121
+ * command: positional()
122
+ * })
123
+ * )
124
+ *
125
+ * const argv = ['dev', '--port', '9131', '--host', 'example.com', '--verbose']
126
+ * const tokens = parseArgs(argv)
127
+ * const { values } = resolveArgs(schema, tokens)
128
+ * ```
129
+ *
130
+ * @experimental This module is experimental and may change in future versions.
131
+ *
132
+ * @module
133
+ */
134
+ /**
135
+ * @author kazuya kawaguchi (a.k.a. kazupon)
136
+ * @license MIT
137
+ */
138
+ function createInvalidTypeError(message, expected, actual) {
139
+ return new ArgsValidationError(message, {
140
+ code: ArgsValidationErrorKeys.invalidType,
141
+ values: {
142
+ expected,
143
+ actual
144
+ }
145
+ });
146
+ }
147
+ function createInvalidChoiceError(message, choices, actual) {
148
+ return new ArgsValidationError(message, {
149
+ code: ArgsValidationErrorKeys.invalidChoice,
150
+ values: {
151
+ expected: "enum",
152
+ choices: formatChoices(choices),
153
+ choiceValues: [...choices],
154
+ actual
155
+ }
156
+ });
157
+ }
158
+ /**
7
159
  * Create a string argument schema with optional validation.
8
160
  *
9
161
  * @param opts - Validation options.
@@ -63,7 +215,7 @@ function number(opts) {
63
215
  ...opts?.required != null ? { required: opts.required } : {},
64
216
  parse(value) {
65
217
  const n = Number(value);
66
- if (value.trim() === "" || isNaN(n)) throw new TypeError(`Expected a number, got '${value}'`);
218
+ if (value.trim() === "" || isNaN(n)) throw createInvalidTypeError(`Expected a number, got '${value}'`, "number", value);
67
219
  if (opts?.min != null && n < opts.min) throw new RangeError(`Number must be >= ${opts.min}, got ${n}`);
68
220
  if (opts?.max != null && n > opts.max) throw new RangeError(`Number must be <= ${opts.max}, got ${n}`);
69
221
  return n;
@@ -97,9 +249,9 @@ function integer(opts) {
97
249
  ...opts?.short != null ? { short: opts.short } : {},
98
250
  ...opts?.required != null ? { required: opts.required } : {},
99
251
  parse(value) {
100
- if (!/^-?\d+$/.test(value)) throw new TypeError(`Expected an integer, got '${value}'`);
252
+ if (!/^-?\d+$/.test(value)) throw createInvalidTypeError(`Expected an integer, got '${value}'`, "integer", value);
101
253
  const n = Number.parseInt(value, 10);
102
- if (isNaN(n)) throw new TypeError(`Expected an integer, got '${value}'`);
254
+ if (isNaN(n)) throw createInvalidTypeError(`Expected an integer, got '${value}'`, "integer", value);
103
255
  if (opts?.min != null && n < opts.min) throw new RangeError(`Integer must be >= ${opts.min}, got ${n}`);
104
256
  if (opts?.max != null && n > opts.max) throw new RangeError(`Integer must be <= ${opts.max}, got ${n}`);
105
257
  return n;
@@ -134,9 +286,9 @@ function float(opts) {
134
286
  ...opts?.required != null ? { required: opts.required } : {},
135
287
  parse(value) {
136
288
  const trimmed = value.trim();
137
- if (!/^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?$/i.test(trimmed)) throw new TypeError(`Expected a finite float, got '${value}'`);
289
+ if (!/^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?$/i.test(trimmed)) throw createInvalidTypeError(`Expected a finite float, got '${value}'`, "float", value);
138
290
  const n = Number.parseFloat(trimmed);
139
- if (isNaN(n) || !isFinite(n)) throw new TypeError(`Expected a finite float, got '${value}'`);
291
+ if (isNaN(n) || !isFinite(n)) throw createInvalidTypeError(`Expected a finite float, got '${value}'`, "float", value);
140
292
  if (opts?.min != null && n < opts.min) throw new RangeError(`Float must be >= ${opts.min}, got ${n}`);
141
293
  if (opts?.max != null && n > opts.max) throw new RangeError(`Float must be <= ${opts.max}, got ${n}`);
142
294
  return n;
@@ -229,7 +381,7 @@ function choice(values, opts) {
229
381
  ...opts?.short != null ? { short: opts.short } : {},
230
382
  ...opts?.required != null ? { required: opts.required } : {},
231
383
  parse(value) {
232
- if (!values.includes(value)) throw new Error(`Value must be one of: ${values.join(", ")}`);
384
+ if (!values.includes(value)) throw createInvalidChoiceError(`Value must be one of: ${values.join(", ")}`, values, value);
233
385
  return value;
234
386
  }
235
387
  };
@@ -560,6 +712,12 @@ function extend(base, overrides) {
560
712
  return result;
561
713
  }
562
714
  //#endregion
715
+ //#region ../gunshi/src/combinators.ts
716
+ /**
717
+ * @author kazuya kawaguchi (a.k.a. kazupon)
718
+ * @license MIT
719
+ */
720
+ //#endregion
563
721
  //#region src/index.ts
564
722
  /**
565
723
  * @author kazuya kawaguchi (a.k.a. kazupon)
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@gunshi/combinators",
3
3
  "description": "parser combinators for gunshi argument schema",
4
- "version": "0.35.1",
4
+ "version": "0.35.3",
5
5
  "author": {
6
6
  "name": "kazuya kawaguchi",
7
7
  "email": "kawakazu80@gmail.com"
@@ -57,7 +57,7 @@
57
57
  "jsr-exports-lint": "^0.4.2",
58
58
  "publint": "^0.3.20",
59
59
  "tsdown": "0.21.0",
60
- "gunshi": "0.35.1"
60
+ "gunshi": "0.35.3"
61
61
  },
62
62
  "scripts": {
63
63
  "build": "tsdown",