args-tokens 0.17.0 → 0.18.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
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ArgToken, ParserOptions, parseArgs$1 as parseArgs } from "./parser-Bx112mWZ.js";
|
|
2
|
-
import { ArgResolveError$1 as ArgResolveError, ArgResolveErrorType, ArgSchema, ArgValues, Args, ResolveArgs, resolveArgs$1 as resolveArgs } from "./resolver
|
|
2
|
+
import { ArgResolveError$1 as ArgResolveError, ArgResolveErrorType, ArgSchema, ArgValues, Args, ResolveArgs, resolveArgs$1 as resolveArgs } from "./resolver-D_oOCXlX.js";
|
|
3
3
|
|
|
4
4
|
//#region src/parse.d.ts
|
|
5
5
|
/**
|
package/lib/index.js
CHANGED
|
@@ -5,7 +5,6 @@ import { ArgToken } from "./parser-Bx112mWZ.js";
|
|
|
5
5
|
* An argument schema
|
|
6
6
|
* This schema is similar to the schema of the `node:utils`.
|
|
7
7
|
* difference is that:
|
|
8
|
-
* - `multiple` property is not supported
|
|
9
8
|
* - `required` property and `description` property are added
|
|
10
9
|
* - `type` is not only 'string' and 'boolean', but also 'number', 'enum' and 'positional' too.
|
|
11
10
|
* - `default` property type, not support multiple types
|
|
@@ -14,7 +13,6 @@ import { ArgToken } from "./parser-Bx112mWZ.js";
|
|
|
14
13
|
* An argument schema
|
|
15
14
|
* This schema is similar to the schema of the `node:utils`.
|
|
16
15
|
* difference is that:
|
|
17
|
-
* - `multiple` property is not supported
|
|
18
16
|
* - `required` property and `description` property are added
|
|
19
17
|
* - `type` is not only 'string' and 'boolean', but also 'number', 'enum' and 'positional' too.
|
|
20
18
|
* - `default` property type, not support multiple types
|
|
@@ -37,6 +35,10 @@ interface ArgSchema {
|
|
|
37
35
|
*/
|
|
38
36
|
required?: true;
|
|
39
37
|
/**
|
|
38
|
+
* Whether the argument allow multiple values or not.
|
|
39
|
+
*/
|
|
40
|
+
multiple?: true;
|
|
41
|
+
/**
|
|
40
42
|
* Whether the negatable option for `boolean` type
|
|
41
43
|
*/
|
|
42
44
|
negatable?: boolean;
|
|
@@ -60,12 +62,13 @@ interface Args {
|
|
|
60
62
|
* An object that contains the values of the arguments.
|
|
61
63
|
*/
|
|
62
64
|
type ArgValues<T> = T extends Args ? ResolveArgValues<T, { [Arg in keyof T]: ExtractOptionValue<T[Arg]> }> : {
|
|
63
|
-
[option: string]: string | boolean | number | undefined;
|
|
65
|
+
[option: string]: string | boolean | number | (string | boolean | number)[] | undefined;
|
|
64
66
|
};
|
|
65
67
|
/**
|
|
66
68
|
* @internal
|
|
67
69
|
*/
|
|
68
|
-
type ExtractOptionValue<A extends ArgSchema> = A["type"] extends "string" ? string : A["type"] extends "boolean" ? boolean : A["type"] extends "number" ? number : A["type"] extends "positional" ? string : A["type"] extends "enum" ? A["choices"] extends string[] | readonly string[] ? A["choices"][number] : never : string | boolean | number
|
|
70
|
+
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 : ResolveOptionValue<A, string | boolean | number>;
|
|
71
|
+
type ResolveOptionValue<A extends ArgSchema, T> = A["multiple"] extends true ? T[] : T;
|
|
69
72
|
/**
|
|
70
73
|
* @internal
|
|
71
74
|
*/
|
|
@@ -12,8 +12,7 @@ const SKIP_POSITIONAL_DEFAULT = -1;
|
|
|
12
12
|
function resolveArgs(args, tokens, { optionGrouping = false, skipPositional = SKIP_POSITIONAL_DEFAULT } = {}) {
|
|
13
13
|
const skipPositionalIndex = typeof skipPositional === "number" ? Math.max(skipPositional, SKIP_POSITIONAL_DEFAULT) : SKIP_POSITIONAL_DEFAULT;
|
|
14
14
|
const rest = [];
|
|
15
|
-
const
|
|
16
|
-
const shortOptionTokens = [];
|
|
15
|
+
const optionTokens = [];
|
|
17
16
|
const positionalTokens = [];
|
|
18
17
|
let currentLongOption;
|
|
19
18
|
let currentShortOption;
|
|
@@ -29,14 +28,14 @@ function resolveArgs(args, tokens, { optionGrouping = false, skipPositional = SK
|
|
|
29
28
|
function applyLongOptionValue(value = void 0) {
|
|
30
29
|
if (currentLongOption) {
|
|
31
30
|
currentLongOption.value = value;
|
|
32
|
-
|
|
31
|
+
optionTokens.push({ ...currentLongOption });
|
|
33
32
|
currentLongOption = void 0;
|
|
34
33
|
}
|
|
35
34
|
}
|
|
36
35
|
function applyShortOptionValue(value = void 0) {
|
|
37
36
|
if (currentShortOption) {
|
|
38
37
|
currentShortOption.value = value || toShortValue();
|
|
39
|
-
|
|
38
|
+
optionTokens.push({ ...currentShortOption });
|
|
40
39
|
currentShortOption = void 0;
|
|
41
40
|
}
|
|
42
41
|
}
|
|
@@ -65,18 +64,18 @@ function resolveArgs(args, tokens, { optionGrouping = false, skipPositional = SK
|
|
|
65
64
|
} else if (token.kind === "option") if (token.rawName) {
|
|
66
65
|
if (hasLongOptionPrefix(token.rawName)) {
|
|
67
66
|
applyLongOptionValue();
|
|
68
|
-
if (token.inlineValue)
|
|
67
|
+
if (token.inlineValue) optionTokens.push({ ...token });
|
|
69
68
|
else currentLongOption = { ...token };
|
|
70
69
|
applyShortOptionValue();
|
|
71
70
|
} else if (isShortOption(token.rawName)) if (currentShortOption) {
|
|
72
71
|
if (currentShortOption.index === token.index) if (optionGrouping) {
|
|
73
72
|
currentShortOption.value = token.value;
|
|
74
|
-
|
|
73
|
+
optionTokens.push({ ...currentShortOption });
|
|
75
74
|
currentShortOption = { ...token };
|
|
76
75
|
} else expandableShortOptions.push({ ...token });
|
|
77
76
|
else {
|
|
78
77
|
currentShortOption.value = toShortValue();
|
|
79
|
-
|
|
78
|
+
optionTokens.push({ ...currentShortOption });
|
|
80
79
|
currentShortOption = { ...token };
|
|
81
80
|
}
|
|
82
81
|
applyLongOptionValue();
|
|
@@ -87,7 +86,7 @@ function resolveArgs(args, tokens, { optionGrouping = false, skipPositional = SK
|
|
|
87
86
|
} else {
|
|
88
87
|
if (currentShortOption && currentShortOption.index == token.index && token.inlineValue) {
|
|
89
88
|
currentShortOption.value = token.value;
|
|
90
|
-
|
|
89
|
+
optionTokens.push({ ...currentShortOption });
|
|
91
90
|
currentShortOption = void 0;
|
|
92
91
|
}
|
|
93
92
|
applyLongOptionValue();
|
|
@@ -118,7 +117,9 @@ function resolveArgs(args, tokens, { optionGrouping = false, skipPositional = SK
|
|
|
118
117
|
let positionalsCount = 0;
|
|
119
118
|
for (const [option, schema] of Object.entries(args)) {
|
|
120
119
|
if (schema.required) {
|
|
121
|
-
const found =
|
|
120
|
+
const found = optionTokens.find((token) => {
|
|
121
|
+
return schema.short && token.name === schema.short || token.rawName && hasLongOptionPrefix(token.rawName) && token.name === option;
|
|
122
|
+
});
|
|
122
123
|
if (!found) {
|
|
123
124
|
errors.push(createRequireError(option, schema));
|
|
124
125
|
continue;
|
|
@@ -132,9 +133,9 @@ function resolveArgs(args, tokens, { optionGrouping = false, skipPositional = SK
|
|
|
132
133
|
positionalsCount++;
|
|
133
134
|
continue;
|
|
134
135
|
}
|
|
135
|
-
for (let i = 0; i <
|
|
136
|
-
const token =
|
|
137
|
-
if (checkTokenName(option, schema, token) && token.rawName != void 0 && hasLongOptionPrefix(token.rawName)) {
|
|
136
|
+
for (let i = 0; i < optionTokens.length; i++) {
|
|
137
|
+
const token = optionTokens[i];
|
|
138
|
+
if (checkTokenName(option, schema, token) && token.rawName != void 0 && hasLongOptionPrefix(token.rawName) || schema.short === token.name && token.rawName != void 0 && isShortOption(token.rawName)) {
|
|
138
139
|
const invalid = validateRequire(token, option, schema);
|
|
139
140
|
if (invalid) {
|
|
140
141
|
errors.push(invalid);
|
|
@@ -148,27 +149,10 @@ function resolveArgs(args, tokens, { optionGrouping = false, skipPositional = SK
|
|
|
148
149
|
continue;
|
|
149
150
|
}
|
|
150
151
|
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
for (let i = 0; i < shortOptionTokens.length; i++) {
|
|
156
|
-
const token = shortOptionTokens[i];
|
|
157
|
-
if (schema.short === token.name && token.rawName != null && isShortOption(token.rawName)) {
|
|
158
|
-
const invalid = validateRequire(token, option, schema);
|
|
159
|
-
if (invalid) {
|
|
160
|
-
errors.push(invalid);
|
|
161
|
-
continue;
|
|
162
|
-
}
|
|
163
|
-
if (schema.type === "boolean") token.value = void 0;
|
|
164
|
-
else {
|
|
165
|
-
const invalid$1 = validateValue(token, option, schema);
|
|
166
|
-
if (invalid$1) {
|
|
167
|
-
errors.push(invalid$1);
|
|
168
|
-
continue;
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
values[option] = resolveArgumentValue(token, schema);
|
|
152
|
+
if (schema.multiple) {
|
|
153
|
+
values[option] ||= [];
|
|
154
|
+
values[option].push(resolveArgumentValue(token, schema));
|
|
155
|
+
} else values[option] = resolveArgumentValue(token, schema);
|
|
172
156
|
continue;
|
|
173
157
|
}
|
|
174
158
|
}
|
|
@@ -182,7 +166,7 @@ function resolveArgs(args, tokens, { optionGrouping = false, skipPositional = SK
|
|
|
182
166
|
};
|
|
183
167
|
}
|
|
184
168
|
function createRequireError(option, schema) {
|
|
185
|
-
const message = schema.type === "positional" ? `Positional argument '${option}' is required` : `
|
|
169
|
+
const message = schema.type === "positional" ? `Positional argument '${option}' is required` : `Optional argument '--${option}' ${schema.short ? `or '-${schema.short}' ` : ""}is required`;
|
|
186
170
|
return new ArgResolveError(message, option, "required", schema);
|
|
187
171
|
}
|
|
188
172
|
/**
|
|
@@ -214,7 +198,7 @@ function validateValue(token, option, schema) {
|
|
|
214
198
|
break;
|
|
215
199
|
}
|
|
216
200
|
case "enum": {
|
|
217
|
-
if (schema.choices && !schema.choices.includes(token.value)) return new ArgResolveError(`
|
|
201
|
+
if (schema.choices && !schema.choices.includes(token.value)) return 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);
|
|
218
202
|
break;
|
|
219
203
|
}
|
|
220
204
|
}
|
|
@@ -223,7 +207,7 @@ function isNumeric(str) {
|
|
|
223
207
|
return str.trim() !== "" && !isNaN(str);
|
|
224
208
|
}
|
|
225
209
|
function createTypeError(option, schema) {
|
|
226
|
-
return new ArgResolveError(`
|
|
210
|
+
return new ArgResolveError(`Optional argument '--${option}' ${schema.short ? `or '-${schema.short}' ` : ""}should be '${schema.type}'`, option, "type", schema);
|
|
227
211
|
}
|
|
228
212
|
function resolveArgumentValue(token, schema) {
|
|
229
213
|
if (token.value) return schema.type === "number" ? +token.value : token.value;
|
package/lib/resolver.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import "./parser-Bx112mWZ.js";
|
|
2
|
-
import { ArgResolveError$1 as ArgResolveError, ArgResolveErrorType, ArgSchema, ArgValues, Args, ExtractOptionValue, FilterArgs, ResolveArgValues, ResolveArgs, resolveArgs$1 as resolveArgs } from "./resolver
|
|
2
|
+
import { ArgResolveError$1 as ArgResolveError, ArgResolveErrorType, ArgSchema, ArgValues, Args, ExtractOptionValue, FilterArgs, ResolveArgValues, ResolveArgs, resolveArgs$1 as resolveArgs } from "./resolver-D_oOCXlX.js";
|
|
3
3
|
export { ArgResolveError, ArgResolveErrorType, ArgSchema, ArgValues, Args, ExtractOptionValue, FilterArgs, ResolveArgValues, ResolveArgs, resolveArgs };
|
package/lib/resolver.js
CHANGED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "args-tokens",
|
|
3
3
|
"description": "parseArgs tokens compatibility and more high-performance parser",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.18.0",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "kazuya kawaguchi",
|
|
7
7
|
"email": "kawakazu80@gmail.com"
|
|
@@ -68,29 +68,30 @@
|
|
|
68
68
|
"@eslint/markdown": "^6.4.0",
|
|
69
69
|
"@kazupon/eslint-config": "^0.29.0",
|
|
70
70
|
"@kazupon/prettier-config": "^0.1.1",
|
|
71
|
-
"@types/node": "^22.15.
|
|
72
|
-
"@
|
|
73
|
-
"
|
|
74
|
-
"
|
|
75
|
-
"
|
|
76
|
-
"eslint
|
|
77
|
-
"eslint-
|
|
71
|
+
"@types/node": "^22.15.21",
|
|
72
|
+
"@typescript/native-preview": "7.0.0-dev.20250522.2",
|
|
73
|
+
"@vitest/eslint-plugin": "^1.2.0",
|
|
74
|
+
"bumpp": "^10.1.1",
|
|
75
|
+
"deno": "^2.3.3",
|
|
76
|
+
"eslint": "^9.27.0",
|
|
77
|
+
"eslint-config-prettier": "^10.1.5",
|
|
78
|
+
"eslint-plugin-jsonc": "^2.20.1",
|
|
78
79
|
"eslint-plugin-promise": "^7.2.1",
|
|
79
80
|
"eslint-plugin-regexp": "^2.7.0",
|
|
80
|
-
"eslint-plugin-unicorn": "^59.0.
|
|
81
|
+
"eslint-plugin-unicorn": "^59.0.1",
|
|
81
82
|
"eslint-plugin-yml": "^1.18.0",
|
|
82
83
|
"gh-changelogen": "^0.2.8",
|
|
83
84
|
"jsr": "^0.13.4",
|
|
84
|
-
"jsr-exports-lint": "^0.
|
|
85
|
-
"knip": "^5.
|
|
86
|
-
"lint-staged": "^15.5.
|
|
85
|
+
"jsr-exports-lint": "^0.4.0",
|
|
86
|
+
"knip": "^5.57.1",
|
|
87
|
+
"lint-staged": "^15.5.2",
|
|
87
88
|
"mitata": "^1.0.34",
|
|
88
|
-
"pkg-pr-new": "^0.0.
|
|
89
|
+
"pkg-pr-new": "^0.0.50",
|
|
89
90
|
"prettier": "^3.5.3",
|
|
90
|
-
"tsdown": "^0.
|
|
91
|
+
"tsdown": "^0.12.1",
|
|
91
92
|
"typescript": "^5.8.3",
|
|
92
|
-
"typescript-eslint": "^8.
|
|
93
|
-
"vitest": "^3.1.
|
|
93
|
+
"typescript-eslint": "^8.32.1",
|
|
94
|
+
"vitest": "^3.1.4"
|
|
94
95
|
},
|
|
95
96
|
"prettier": "@kazupon/prettier-config",
|
|
96
97
|
"lint-staged": {
|
|
@@ -126,6 +127,6 @@
|
|
|
126
127
|
"test": "vitest run",
|
|
127
128
|
"typecheck": "pnpm run --parallel --color \"/^typecheck:/\"",
|
|
128
129
|
"typecheck:deno": "deno check src",
|
|
129
|
-
"typecheck:tsc": "
|
|
130
|
+
"typecheck:tsc": "tsgo --noEmit"
|
|
130
131
|
}
|
|
131
132
|
}
|