@gunshi/bone 0.28.2 → 0.29.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/lib/index.d.ts +27 -6
- package/lib/index.js +38 -21
- package/package.json +5 -5
package/lib/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//#region ../../node_modules/.pnpm/args-tokens@0.
|
|
1
|
+
//#region ../../node_modules/.pnpm/args-tokens@0.24.1/node_modules/args-tokens/lib/parser-DH5KtXsI.d.ts
|
|
2
2
|
//#region src/parser.d.ts
|
|
3
3
|
/**
|
|
4
4
|
* Entry point of argument parser.
|
|
@@ -55,7 +55,7 @@ interface ArgToken {
|
|
|
55
55
|
* Parser Options.
|
|
56
56
|
*/
|
|
57
57
|
//#endregion
|
|
58
|
-
//#region ../../node_modules/.pnpm/args-tokens@0.
|
|
58
|
+
//#region ../../node_modules/.pnpm/args-tokens@0.24.1/node_modules/args-tokens/lib/resolver.d.ts
|
|
59
59
|
//#region src/resolver.d.ts
|
|
60
60
|
/**
|
|
61
61
|
* An argument schema definition for command-line argument parsing.
|
|
@@ -190,7 +190,7 @@ interface ArgSchema {
|
|
|
190
190
|
* }
|
|
191
191
|
* ```
|
|
192
192
|
*/
|
|
193
|
-
required?:
|
|
193
|
+
required?: boolean;
|
|
194
194
|
/**
|
|
195
195
|
* Allows the argument to accept multiple values.
|
|
196
196
|
*
|
|
@@ -417,6 +417,28 @@ interface ArgSchema {
|
|
|
417
417
|
* ```
|
|
418
418
|
*/
|
|
419
419
|
conflicts?: string | string[];
|
|
420
|
+
/**
|
|
421
|
+
* Display name hint for help text generation.
|
|
422
|
+
*
|
|
423
|
+
* Provides a meaningful type hint for the argument value in help output.
|
|
424
|
+
* Particularly useful for `type: 'custom'` arguments where the type
|
|
425
|
+
* name would otherwise be unhelpful.
|
|
426
|
+
*
|
|
427
|
+
* @example
|
|
428
|
+
* Metavar usage:
|
|
429
|
+
* ```ts
|
|
430
|
+
* {
|
|
431
|
+
* port: {
|
|
432
|
+
* type: 'custom',
|
|
433
|
+
* parse: (v: string) => parseInt(v, 10),
|
|
434
|
+
* metavar: 'integer',
|
|
435
|
+
* description: 'Port number (1-65535)'
|
|
436
|
+
* }
|
|
437
|
+
* }
|
|
438
|
+
* // Help output: --port <integer> Port number (1-65535)
|
|
439
|
+
* ```
|
|
440
|
+
*/
|
|
441
|
+
metavar?: string;
|
|
420
442
|
/**
|
|
421
443
|
* Custom parsing function for `type: 'custom'` arguments.
|
|
422
444
|
*
|
|
@@ -475,7 +497,6 @@ interface Args {
|
|
|
475
497
|
type ArgValues<T> = T extends Args ? ResolveArgValues<T, { [Arg in keyof T]: ExtractOptionValue<T[Arg]> }> : {
|
|
476
498
|
[option: string]: string | boolean | number | (string | boolean | number)[] | undefined;
|
|
477
499
|
};
|
|
478
|
-
type IsFunction<T> = T extends ((...args: any[]) => any) ? true : false;
|
|
479
500
|
/**
|
|
480
501
|
* Extracts the value type from the argument schema.
|
|
481
502
|
*
|
|
@@ -483,7 +504,7 @@ type IsFunction<T> = T extends ((...args: any[]) => any) ? true : false;
|
|
|
483
504
|
*
|
|
484
505
|
* @internal
|
|
485
506
|
*/
|
|
486
|
-
type ExtractOptionValue<A extends ArgSchema> = A['type'] extends 'string' ? ResolveOptionValue<A, string> : A['type'] extends 'boolean' ? ResolveOptionValue<A, boolean> : A['type'] extends 'number' ? ResolveOptionValue<A, number> : A['type'] extends 'positional' ? ResolveOptionValue<A, string> : A['type'] extends 'enum' ? A['choices'] extends string[] | readonly string[] ? ResolveOptionValue<A, A['choices'][number]> : never : A['type'] extends 'custom' ?
|
|
507
|
+
type ExtractOptionValue<A extends ArgSchema> = undefined extends A['parse'] ? A['type'] extends 'string' ? ResolveOptionValue<A, string> : A['type'] extends 'boolean' ? ResolveOptionValue<A, boolean> : A['type'] extends 'number' ? ResolveOptionValue<A, number> : A['type'] extends 'positional' ? ResolveOptionValue<A, string> : A['type'] extends 'enum' ? A['choices'] extends string[] | readonly string[] ? ResolveOptionValue<A, A['choices'][number]> : never : A['type'] extends 'custom' ? never : ResolveOptionValue<A, string | boolean | number> : A['parse'] extends ((value: string) => infer R) ? ResolveOptionValue<A, R> : never;
|
|
487
508
|
type ResolveOptionValue<A extends ArgSchema, T> = A['multiple'] extends true ? T[] : T;
|
|
488
509
|
/**
|
|
489
510
|
* Resolved argument values.
|
|
@@ -503,7 +524,7 @@ type ResolveArgValues<A extends Args, V extends Record<keyof A, unknown>> = { -r
|
|
|
503
524
|
*
|
|
504
525
|
* @internal
|
|
505
526
|
*/
|
|
506
|
-
type FilterArgs<A extends Args, V extends Record<keyof A, unknown>, K extends keyof ArgSchema> = { [Arg in keyof A as A[Arg][K] extends {} ? Arg : never]: V[Arg] };
|
|
527
|
+
type FilterArgs<A extends Args, V extends Record<keyof A, unknown>, K extends keyof ArgSchema> = { [Arg in keyof A as K extends 'required' ? A[Arg][K] extends true ? Arg : never : A[Arg][K] extends {} ? Arg : never]: V[Arg] };
|
|
507
528
|
/**
|
|
508
529
|
* Filters positional arguments from the argument schema.
|
|
509
530
|
*
|
package/lib/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//#region ../../node_modules/.pnpm/args-tokens@0.
|
|
1
|
+
//#region ../../node_modules/.pnpm/args-tokens@0.24.1/node_modules/args-tokens/lib/parser.js
|
|
2
2
|
const HYPHEN_CHAR = "-";
|
|
3
3
|
const HYPHEN_CODE = HYPHEN_CHAR.codePointAt(0);
|
|
4
4
|
const EQUAL_CHAR = "=";
|
|
@@ -196,7 +196,7 @@ function hasOptionValue(value) {
|
|
|
196
196
|
}
|
|
197
197
|
|
|
198
198
|
//#endregion
|
|
199
|
-
//#region ../../node_modules/.pnpm/args-tokens@0.
|
|
199
|
+
//#region ../../node_modules/.pnpm/args-tokens@0.24.1/node_modules/args-tokens/lib/utils-CxvkckUD.js
|
|
200
200
|
/**
|
|
201
201
|
* Entry point of utils.
|
|
202
202
|
*
|
|
@@ -219,7 +219,7 @@ function kebabnize(str) {
|
|
|
219
219
|
}
|
|
220
220
|
|
|
221
221
|
//#endregion
|
|
222
|
-
//#region ../../node_modules/.pnpm/args-tokens@0.
|
|
222
|
+
//#region ../../node_modules/.pnpm/args-tokens@0.24.1/node_modules/args-tokens/lib/resolver.js
|
|
223
223
|
/**
|
|
224
224
|
* Entry point of argument options resolver.
|
|
225
225
|
*
|
|
@@ -305,13 +305,15 @@ function resolveArgs(args, tokens, { shortGrouping = false, skipPositional = SKI
|
|
|
305
305
|
rest.push(token.value);
|
|
306
306
|
continue;
|
|
307
307
|
}
|
|
308
|
-
if (currentShortOption) {
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
308
|
+
if (currentShortOption) if (schemas.find((schema) => schema.short === currentShortOption.name && schema.type === "boolean")) {
|
|
309
|
+
positionalTokens.push({ ...token });
|
|
310
|
+
applyShortOptionValue();
|
|
311
|
+
} else applyShortOptionValue(token.value);
|
|
312
|
+
else if (currentLongOption) if (args[currentLongOption.name]?.type === "boolean") {
|
|
313
|
+
positionalTokens.push({ ...token });
|
|
314
|
+
applyLongOptionValue();
|
|
315
|
+
} else applyLongOptionValue(token.value);
|
|
316
|
+
else positionalTokens.push({ ...token });
|
|
315
317
|
} else if (token.kind === "option") if (token.rawName) {
|
|
316
318
|
if (hasLongOptionPrefix(token.rawName)) {
|
|
317
319
|
applyLongOptionValue();
|
|
@@ -376,14 +378,27 @@ function resolveArgs(args, tokens, { shortGrouping = false, skipPositional = SKI
|
|
|
376
378
|
if (schema.multiple) {
|
|
377
379
|
const remainingPositionals = positionalTokens.slice(positionalsCount);
|
|
378
380
|
if (remainingPositionals.length > 0) {
|
|
379
|
-
|
|
381
|
+
if (typeof schema.parse === "function") {
|
|
382
|
+
const parsed = [];
|
|
383
|
+
for (const p of remainingPositionals) try {
|
|
384
|
+
parsed.push(schema.parse(p.value));
|
|
385
|
+
} catch (error) {
|
|
386
|
+
errors.push(error);
|
|
387
|
+
}
|
|
388
|
+
values[rawArg] = parsed;
|
|
389
|
+
} else values[rawArg] = remainingPositionals.map((p) => p.value);
|
|
380
390
|
positionalsCount += remainingPositionals.length;
|
|
381
391
|
explicit[rawArg] = true;
|
|
382
392
|
} else if (schema.required) errors.push(createRequireError(arg, schema));
|
|
383
393
|
} else {
|
|
384
394
|
const positional = positionalTokens[positionalsCount];
|
|
385
395
|
if (positional != null) {
|
|
386
|
-
|
|
396
|
+
if (typeof schema.parse === "function") try {
|
|
397
|
+
values[rawArg] = schema.parse(positional.value);
|
|
398
|
+
} catch (error) {
|
|
399
|
+
errors.push(error);
|
|
400
|
+
}
|
|
401
|
+
else values[rawArg] = positional.value;
|
|
387
402
|
explicit[rawArg] = true;
|
|
388
403
|
} else errors.push(createRequireError(arg, schema));
|
|
389
404
|
positionalsCount++;
|
|
@@ -409,7 +424,6 @@ function resolveArgs(args, tokens, { shortGrouping = false, skipPositional = SKI
|
|
|
409
424
|
explicit[rawArg] = true;
|
|
410
425
|
const actualInputName = isShortOption(token.rawName) ? `-${token.name}` : `--${arg}`;
|
|
411
426
|
actualInputNames.set(rawArg, actualInputName);
|
|
412
|
-
if (schema.type === "boolean") token.value = void 0;
|
|
413
427
|
const [parsedValue, error] = parse(token, arg, schema);
|
|
414
428
|
if (error) errors.push(error);
|
|
415
429
|
else if (schema.multiple) {
|
|
@@ -431,22 +445,25 @@ function resolveArgs(args, tokens, { shortGrouping = false, skipPositional = SKI
|
|
|
431
445
|
};
|
|
432
446
|
}
|
|
433
447
|
function parse(token, option, schema) {
|
|
448
|
+
if (typeof schema.parse === "function") try {
|
|
449
|
+
if (schema.type === "boolean") {
|
|
450
|
+
const boolValue = !(schema.negatable && token.name.startsWith("no-"));
|
|
451
|
+
return [schema.parse(String(boolValue)), void 0];
|
|
452
|
+
}
|
|
453
|
+
return [schema.parse(token.value ?? String(schema.default ?? "")), void 0];
|
|
454
|
+
} catch (error) {
|
|
455
|
+
return [void 0, error];
|
|
456
|
+
}
|
|
434
457
|
switch (schema.type) {
|
|
435
458
|
case "string": return typeof token.value === "string" ? [token.value || schema.default, void 0] : [void 0, createTypeError(option, schema)];
|
|
436
|
-
case "boolean": return
|
|
459
|
+
case "boolean": return [!(schema.negatable && token.name.startsWith("no-")), void 0];
|
|
437
460
|
case "number":
|
|
438
461
|
if (!isNumeric(token.value)) return [void 0, createTypeError(option, schema)];
|
|
439
462
|
return token.value ? [+token.value, void 0] : [+(schema.default || ""), void 0];
|
|
440
463
|
case "enum":
|
|
441
464
|
if (schema.choices && !schema.choices.includes(token.value)) return [void 0, new ArgResolveError(`Optional argument '--${option}' ${schema.short ? `or '-${schema.short}' ` : ""}should be chosen from '${schema.type}' [${schema.choices.map((c) => JSON.stringify(c)).join(", ")}] values`, option, "type", schema)];
|
|
442
465
|
return [token.value || schema.default, void 0];
|
|
443
|
-
case "custom":
|
|
444
|
-
if (typeof schema.parse !== "function") throw new TypeError(`argument '${option}' should have a 'parse' function`);
|
|
445
|
-
try {
|
|
446
|
-
return [schema.parse(token.value || String(schema.default || "")), void 0];
|
|
447
|
-
} catch (error) {
|
|
448
|
-
return [void 0, error];
|
|
449
|
-
}
|
|
466
|
+
case "custom": throw new TypeError(`argument '${option}' should have a 'parse' function`);
|
|
450
467
|
default: throw new Error(`Unsupported argument type '${schema.type}' for option '${option}'`);
|
|
451
468
|
}
|
|
452
469
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gunshi/bone",
|
|
3
3
|
"description": "gunshi minimum",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.29.0",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "kazuya kawaguchi",
|
|
7
7
|
"email": "kawakazu80@gmail.com"
|
|
@@ -56,10 +56,10 @@
|
|
|
56
56
|
"jsr-exports-lint": "^0.4.1",
|
|
57
57
|
"publint": "^0.3.16",
|
|
58
58
|
"tsdown": "0.15.12",
|
|
59
|
-
"@gunshi/
|
|
60
|
-
"
|
|
61
|
-
"gunshi": "0.
|
|
62
|
-
"@gunshi/plugin-renderer": "0.
|
|
59
|
+
"@gunshi/definition": "0.29.0",
|
|
60
|
+
"gunshi": "0.29.0",
|
|
61
|
+
"@gunshi/plugin-global": "0.29.0",
|
|
62
|
+
"@gunshi/plugin-renderer": "0.29.0"
|
|
63
63
|
},
|
|
64
64
|
"scripts": {
|
|
65
65
|
"build": "tsdown",
|