args-tokens 0.16.2 → 0.17.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 +8 -8
- package/lib/index.js +4 -4
- package/lib/resolver--lXziDSy.d.ts +125 -0
- package/lib/{resolver-B89Gv70Z.js → resolver-pmWK3ioa.js} +37 -21
- package/lib/resolver.d.ts +2 -2
- package/lib/resolver.js +2 -2
- package/package.json +1 -1
- package/lib/resolver-ByBRRKOK.d.ts +0 -119
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 {
|
|
2
|
+
import { ArgResolveError$1 as ArgResolveError, ArgResolveErrorType, ArgSchema, ArgValues, Args, ResolveArgs, resolveArgs$1 as resolveArgs } from "./resolver--lXziDSy.js";
|
|
3
3
|
|
|
4
4
|
//#region src/parse.d.ts
|
|
5
5
|
/**
|
|
@@ -8,20 +8,20 @@ import { ArgOptionSchema, ArgOptions, ArgValues, OptionResolveError$1 as OptionR
|
|
|
8
8
|
/**
|
|
9
9
|
* Parse options for {@link parse} function.
|
|
10
10
|
*/
|
|
11
|
-
interface ParseOptions<
|
|
11
|
+
interface ParseOptions<A extends Args> extends ParserOptions, ResolveArgs {
|
|
12
12
|
/**
|
|
13
|
-
* Command line
|
|
13
|
+
* Command line arguments, about details see {@link Args}.
|
|
14
14
|
*/
|
|
15
|
-
|
|
15
|
+
args?: A;
|
|
16
16
|
}
|
|
17
17
|
/**
|
|
18
18
|
* Parsed command line arguments.
|
|
19
19
|
*/
|
|
20
|
-
type ParsedArgs<
|
|
20
|
+
type ParsedArgs<A extends Args> = {
|
|
21
21
|
/**
|
|
22
22
|
* Parsed values, same as `values` in {@link resolveArgs}.
|
|
23
23
|
*/
|
|
24
|
-
values: ArgValues<
|
|
24
|
+
values: ArgValues<A>;
|
|
25
25
|
/**
|
|
26
26
|
* Positional arguments, same as `positionals` in {@link resolveArgs}.
|
|
27
27
|
*/
|
|
@@ -54,5 +54,5 @@ type ParsedArgs<T extends ArgOptions> = {
|
|
|
54
54
|
* @param options - parse options, about details see {@link ParseOptions}
|
|
55
55
|
* @returns An object that contains the values of the arguments, positional arguments, {@link AggregateError | validation errors}, and {@link ArgToken | argument tokens}.
|
|
56
56
|
*/
|
|
57
|
-
declare function parse<
|
|
58
|
-
export {
|
|
57
|
+
declare function parse<A extends Args>(args: string[], options?: ParseOptions<A>): ParsedArgs<A>; //#endregion
|
|
58
|
+
export { ArgResolveError, ArgResolveErrorType, ArgSchema, ArgToken, ArgValues, Args, ParseOptions, ParsedArgs, ParserOptions, ResolveArgs, parse, parseArgs, resolveArgs };
|
package/lib/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { parseArgs } from "./parser-Dr4iAGaX.js";
|
|
2
|
-
import {
|
|
2
|
+
import { ArgResolveError, resolveArgs } from "./resolver-pmWK3ioa.js";
|
|
3
3
|
|
|
4
4
|
//#region src/parse.ts
|
|
5
5
|
const DEFAULT_OPTIONS = {
|
|
@@ -28,10 +28,10 @@ const DEFAULT_OPTIONS = {
|
|
|
28
28
|
* @returns An object that contains the values of the arguments, positional arguments, {@link AggregateError | validation errors}, and {@link ArgToken | argument tokens}.
|
|
29
29
|
*/
|
|
30
30
|
function parse(args, options = {}) {
|
|
31
|
-
const {
|
|
31
|
+
const { args: _args, allowCompatible = false } = options;
|
|
32
32
|
const tokens = parseArgs(args, { allowCompatible });
|
|
33
|
-
return Object.assign(Object.create(null), resolveArgs(
|
|
33
|
+
return Object.assign(Object.create(null), resolveArgs(_args || DEFAULT_OPTIONS, tokens), { tokens });
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
//#endregion
|
|
37
|
-
export {
|
|
37
|
+
export { ArgResolveError, parse, parseArgs, resolveArgs };
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { ArgToken } from "./parser-Bx112mWZ.js";
|
|
2
|
+
|
|
3
|
+
//#region src/resolver.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* An argument schema
|
|
6
|
+
* This schema is similar to the schema of the `node:utils`.
|
|
7
|
+
* difference is that:
|
|
8
|
+
* - `multiple` property is not supported
|
|
9
|
+
* - `required` property and `description` property are added
|
|
10
|
+
* - `type` is not only 'string' and 'boolean', but also 'number', 'enum' and 'positional' too.
|
|
11
|
+
* - `default` property type, not support multiple types
|
|
12
|
+
*/
|
|
13
|
+
/**
|
|
14
|
+
* An argument schema
|
|
15
|
+
* This schema is similar to the schema of the `node:utils`.
|
|
16
|
+
* difference is that:
|
|
17
|
+
* - `multiple` property is not supported
|
|
18
|
+
* - `required` property and `description` property are added
|
|
19
|
+
* - `type` is not only 'string' and 'boolean', but also 'number', 'enum' and 'positional' too.
|
|
20
|
+
* - `default` property type, not support multiple types
|
|
21
|
+
*/
|
|
22
|
+
interface ArgSchema {
|
|
23
|
+
/**
|
|
24
|
+
* Type of argument.
|
|
25
|
+
*/
|
|
26
|
+
type: "string" | "boolean" | "number" | "enum" | "positional";
|
|
27
|
+
/**
|
|
28
|
+
* A single character alias for the option.
|
|
29
|
+
*/
|
|
30
|
+
short?: string;
|
|
31
|
+
/**
|
|
32
|
+
* A description of the argument.
|
|
33
|
+
*/
|
|
34
|
+
description?: string;
|
|
35
|
+
/**
|
|
36
|
+
* Whether the argument is required or not.
|
|
37
|
+
*/
|
|
38
|
+
required?: true;
|
|
39
|
+
/**
|
|
40
|
+
* Whether the negatable option for `boolean` type
|
|
41
|
+
*/
|
|
42
|
+
negatable?: boolean;
|
|
43
|
+
/**
|
|
44
|
+
* The allowed values of the argument, and string only. This property is only used when the type is 'enum'.
|
|
45
|
+
*/
|
|
46
|
+
choices?: string[] | readonly string[];
|
|
47
|
+
/**
|
|
48
|
+
* The default value of the argument.
|
|
49
|
+
* if the type is 'enum', the default value must be one of the allowed values.
|
|
50
|
+
*/
|
|
51
|
+
default?: string | boolean | number;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* An object that contains {@link ArgSchema | argument schema}.
|
|
55
|
+
*/
|
|
56
|
+
interface Args {
|
|
57
|
+
[option: string]: ArgSchema;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* An object that contains the values of the arguments.
|
|
61
|
+
*/
|
|
62
|
+
type ArgValues<T> = T extends Args ? ResolveArgValues<T, { [Arg in keyof T]: ExtractOptionValue<T[Arg]> }> : {
|
|
63
|
+
[option: string]: string | boolean | number | undefined;
|
|
64
|
+
};
|
|
65
|
+
/**
|
|
66
|
+
* @internal
|
|
67
|
+
*/
|
|
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;
|
|
69
|
+
/**
|
|
70
|
+
* @internal
|
|
71
|
+
*/
|
|
72
|
+
type ResolveArgValues<A extends Args, V extends Record<keyof A, unknown>> = { -readonly [Arg in keyof A]?: V[Arg] } & FilterArgs<A, V, "default"> & FilterArgs<A, V, "required"> extends infer P ? { [K in keyof P]: P[K] } : never;
|
|
73
|
+
/**
|
|
74
|
+
* @internal
|
|
75
|
+
*/
|
|
76
|
+
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] };
|
|
77
|
+
/**
|
|
78
|
+
* An arguments for {@link resolveArgs | resolve arguments}.
|
|
79
|
+
*/
|
|
80
|
+
interface ResolveArgs {
|
|
81
|
+
/**
|
|
82
|
+
* Whether to group short options.
|
|
83
|
+
* @default false
|
|
84
|
+
* @see guideline 5 in https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap12.html
|
|
85
|
+
*/
|
|
86
|
+
optionGrouping?: boolean;
|
|
87
|
+
/**
|
|
88
|
+
* Skip positional arguments index.
|
|
89
|
+
* @default -1
|
|
90
|
+
*/
|
|
91
|
+
skipPositional?: number;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Resolve command line arguments.
|
|
95
|
+
* @param args - An arguments that contains {@link ArgSchema | arguments schema}.
|
|
96
|
+
* @param tokens - An array of {@link ArgToken | tokens}.
|
|
97
|
+
* @param resolveArgs - An arguments that contains {@link ResolveArgs | resolve arguments}.
|
|
98
|
+
* @returns An object that contains the values of the arguments, positional arguments, rest arguments, and {@link AggregateError | validation errors}.
|
|
99
|
+
*/
|
|
100
|
+
declare function resolveArgs<A extends Args>(args: A, tokens: ArgToken[], {
|
|
101
|
+
optionGrouping,
|
|
102
|
+
skipPositional
|
|
103
|
+
}?: ResolveArgs): {
|
|
104
|
+
values: ArgValues<A>;
|
|
105
|
+
positionals: string[];
|
|
106
|
+
rest: string[];
|
|
107
|
+
error: AggregateError | undefined;
|
|
108
|
+
};
|
|
109
|
+
/**
|
|
110
|
+
* An error type for {@link ArgResolveError}.
|
|
111
|
+
*/
|
|
112
|
+
type ArgResolveErrorType = "type" | "required";
|
|
113
|
+
/**
|
|
114
|
+
* An error that occurs when resolving arguments.
|
|
115
|
+
* This error is thrown when the argument is not valid.
|
|
116
|
+
*/
|
|
117
|
+
declare class ArgResolveError extends Error {
|
|
118
|
+
name: string;
|
|
119
|
+
schema: ArgSchema;
|
|
120
|
+
type: ArgResolveErrorType;
|
|
121
|
+
constructor(message: string, name: string, type: ArgResolveErrorType, schema: ArgSchema);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
//#endregion
|
|
125
|
+
export { ArgResolveError as ArgResolveError$1, ArgResolveErrorType, ArgSchema, ArgValues, Args, ExtractOptionValue, FilterArgs, ResolveArgValues, ResolveArgs, resolveArgs as resolveArgs$1 };
|
|
@@ -1,18 +1,20 @@
|
|
|
1
1
|
import { hasLongOptionPrefix, isShortOption } from "./parser-Dr4iAGaX.js";
|
|
2
2
|
|
|
3
3
|
//#region src/resolver.ts
|
|
4
|
+
const SKIP_POSITIONAL_DEFAULT = -1;
|
|
4
5
|
/**
|
|
5
6
|
* Resolve command line arguments.
|
|
6
|
-
* @param
|
|
7
|
+
* @param args - An arguments that contains {@link ArgSchema | arguments schema}.
|
|
7
8
|
* @param tokens - An array of {@link ArgToken | tokens}.
|
|
8
|
-
* @param
|
|
9
|
+
* @param resolveArgs - An arguments that contains {@link ResolveArgs | resolve arguments}.
|
|
9
10
|
* @returns An object that contains the values of the arguments, positional arguments, rest arguments, and {@link AggregateError | validation errors}.
|
|
10
11
|
*/
|
|
11
|
-
function resolveArgs(
|
|
12
|
-
const
|
|
12
|
+
function resolveArgs(args, tokens, { optionGrouping = false, skipPositional = SKIP_POSITIONAL_DEFAULT } = {}) {
|
|
13
|
+
const skipPositionalIndex = typeof skipPositional === "number" ? Math.max(skipPositional, SKIP_POSITIONAL_DEFAULT) : SKIP_POSITIONAL_DEFAULT;
|
|
13
14
|
const rest = [];
|
|
14
15
|
const longOptionTokens = [];
|
|
15
16
|
const shortOptionTokens = [];
|
|
17
|
+
const positionalTokens = [];
|
|
16
18
|
let currentLongOption;
|
|
17
19
|
let currentShortOption;
|
|
18
20
|
const expandableShortOptions = [];
|
|
@@ -42,7 +44,7 @@ function resolveArgs(options, tokens, { optionGrouping = false } = {}) {
|
|
|
42
44
|
* analyze phase to resolve value
|
|
43
45
|
* separate tokens into positionals, long and short options, after that resolve values
|
|
44
46
|
*/
|
|
45
|
-
const schemas = Object.values(
|
|
47
|
+
const schemas = Object.values(args);
|
|
46
48
|
let terminated = false;
|
|
47
49
|
for (let i = 0; i < tokens.length; i++) {
|
|
48
50
|
const token = tokens[i];
|
|
@@ -53,11 +55,11 @@ function resolveArgs(options, tokens, { optionGrouping = false } = {}) {
|
|
|
53
55
|
}
|
|
54
56
|
if (currentShortOption) {
|
|
55
57
|
const found = schemas.find((schema) => schema.short === currentShortOption.name && schema.type === "boolean");
|
|
56
|
-
if (found)
|
|
58
|
+
if (found) positionalTokens.push({ ...token });
|
|
57
59
|
} else if (currentLongOption) {
|
|
58
|
-
const found =
|
|
59
|
-
if (found)
|
|
60
|
-
} else
|
|
60
|
+
const found = args[currentLongOption.name]?.type === "boolean";
|
|
61
|
+
if (found) positionalTokens.push({ ...token });
|
|
62
|
+
} else positionalTokens.push({ ...token });
|
|
61
63
|
applyLongOptionValue(token.value);
|
|
62
64
|
applyShortOptionValue(token.value);
|
|
63
65
|
} else if (token.kind === "option") if (token.rawName) {
|
|
@@ -109,7 +111,12 @@ function resolveArgs(options, tokens, { optionGrouping = false } = {}) {
|
|
|
109
111
|
function checkTokenName(option, schema, token) {
|
|
110
112
|
return token.name === (schema.type === "boolean" ? schema.negatable && token.name?.startsWith("no-") ? `no-${option}` : option : option);
|
|
111
113
|
}
|
|
112
|
-
|
|
114
|
+
const positionalItemCount = tokens.filter((token) => token.kind === "positional").length;
|
|
115
|
+
function getPositionalSkipIndex() {
|
|
116
|
+
return Math.min(skipPositionalIndex, positionalItemCount);
|
|
117
|
+
}
|
|
118
|
+
let positionalsCount = 0;
|
|
119
|
+
for (const [option, schema] of Object.entries(args)) {
|
|
113
120
|
if (schema.required) {
|
|
114
121
|
const found = longOptionTokens.find((token) => token.name === option) || schema.short && shortOptionTokens.find((token) => token.name === schema.short);
|
|
115
122
|
if (!found) {
|
|
@@ -117,6 +124,14 @@ function resolveArgs(options, tokens, { optionGrouping = false } = {}) {
|
|
|
117
124
|
continue;
|
|
118
125
|
}
|
|
119
126
|
}
|
|
127
|
+
if (schema.type === "positional") {
|
|
128
|
+
if (skipPositionalIndex > SKIP_POSITIONAL_DEFAULT) while (positionalsCount <= getPositionalSkipIndex()) positionalsCount++;
|
|
129
|
+
const positional = positionalTokens[positionalsCount];
|
|
130
|
+
if (positional != null) values[option] = positional.value;
|
|
131
|
+
else errors.push(createRequireError(option, schema));
|
|
132
|
+
positionalsCount++;
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
120
135
|
for (let i = 0; i < longOptionTokens.length; i++) {
|
|
121
136
|
const token = longOptionTokens[i];
|
|
122
137
|
if (checkTokenName(option, schema, token) && token.rawName != void 0 && hasLongOptionPrefix(token.rawName)) {
|
|
@@ -133,7 +148,7 @@ function resolveArgs(options, tokens, { optionGrouping = false } = {}) {
|
|
|
133
148
|
continue;
|
|
134
149
|
}
|
|
135
150
|
}
|
|
136
|
-
values[option] =
|
|
151
|
+
values[option] = resolveArgumentValue(token, schema);
|
|
137
152
|
continue;
|
|
138
153
|
}
|
|
139
154
|
}
|
|
@@ -153,7 +168,7 @@ function resolveArgs(options, tokens, { optionGrouping = false } = {}) {
|
|
|
153
168
|
continue;
|
|
154
169
|
}
|
|
155
170
|
}
|
|
156
|
-
values[option] =
|
|
171
|
+
values[option] = resolveArgumentValue(token, schema);
|
|
157
172
|
continue;
|
|
158
173
|
}
|
|
159
174
|
}
|
|
@@ -161,19 +176,20 @@ function resolveArgs(options, tokens, { optionGrouping = false } = {}) {
|
|
|
161
176
|
}
|
|
162
177
|
return {
|
|
163
178
|
values,
|
|
164
|
-
positionals,
|
|
179
|
+
positionals: positionalTokens.map((token) => token.value),
|
|
165
180
|
rest,
|
|
166
181
|
error: errors.length > 0 ? new AggregateError(errors) : void 0
|
|
167
182
|
};
|
|
168
183
|
}
|
|
169
184
|
function createRequireError(option, schema) {
|
|
170
|
-
|
|
185
|
+
const message = schema.type === "positional" ? `Positional argument '${option}' is required` : `Option '--${option}' ${schema.short ? `or '-${schema.short}' ` : ""}is required`;
|
|
186
|
+
return new ArgResolveError(message, option, "required", schema);
|
|
171
187
|
}
|
|
172
188
|
/**
|
|
173
|
-
* An error that occurs when resolving
|
|
174
|
-
* This error is thrown when the
|
|
189
|
+
* An error that occurs when resolving arguments.
|
|
190
|
+
* This error is thrown when the argument is not valid.
|
|
175
191
|
*/
|
|
176
|
-
var
|
|
192
|
+
var ArgResolveError = class extends Error {
|
|
177
193
|
name;
|
|
178
194
|
schema;
|
|
179
195
|
type;
|
|
@@ -198,7 +214,7 @@ function validateValue(token, option, schema) {
|
|
|
198
214
|
break;
|
|
199
215
|
}
|
|
200
216
|
case "enum": {
|
|
201
|
-
if (schema.choices && !schema.choices.includes(token.value)) return new
|
|
217
|
+
if (schema.choices && !schema.choices.includes(token.value)) return new ArgResolveError(`Option '--${option}' ${schema.short ? `or '-${schema.short}' ` : ""}should be chosen from '${schema.type}' [${schema.choices.map((c) => JSON.stringify(c)).join(", ")}] values`, option, "type", schema);
|
|
202
218
|
break;
|
|
203
219
|
}
|
|
204
220
|
}
|
|
@@ -207,13 +223,13 @@ function isNumeric(str) {
|
|
|
207
223
|
return str.trim() !== "" && !isNaN(str);
|
|
208
224
|
}
|
|
209
225
|
function createTypeError(option, schema) {
|
|
210
|
-
return new
|
|
226
|
+
return new ArgResolveError(`Option '--${option}' ${schema.short ? `or '-${schema.short}' ` : ""}should be '${schema.type}'`, option, "type", schema);
|
|
211
227
|
}
|
|
212
|
-
function
|
|
228
|
+
function resolveArgumentValue(token, schema) {
|
|
213
229
|
if (token.value) return schema.type === "number" ? +token.value : token.value;
|
|
214
230
|
if (schema.type === "boolean") return schema.negatable && token.name.startsWith("no-") ? false : true;
|
|
215
231
|
return schema.type === "number" ? +(schema.default || "") : schema.default;
|
|
216
232
|
}
|
|
217
233
|
|
|
218
234
|
//#endregion
|
|
219
|
-
export {
|
|
235
|
+
export { ArgResolveError, resolveArgs };
|
package/lib/resolver.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import "./parser-Bx112mWZ.js";
|
|
2
|
-
import {
|
|
3
|
-
export {
|
|
2
|
+
import { ArgResolveError$1 as ArgResolveError, ArgResolveErrorType, ArgSchema, ArgValues, Args, ExtractOptionValue, FilterArgs, ResolveArgValues, ResolveArgs, resolveArgs$1 as resolveArgs } from "./resolver--lXziDSy.js";
|
|
3
|
+
export { ArgResolveError, ArgResolveErrorType, ArgSchema, ArgValues, Args, ExtractOptionValue, FilterArgs, ResolveArgValues, ResolveArgs, resolveArgs };
|
package/lib/resolver.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import "./parser-Dr4iAGaX.js";
|
|
2
|
-
import {
|
|
2
|
+
import { ArgResolveError, resolveArgs } from "./resolver-pmWK3ioa.js";
|
|
3
3
|
|
|
4
|
-
export {
|
|
4
|
+
export { ArgResolveError, resolveArgs };
|
package/package.json
CHANGED
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
import { ArgToken } from "./parser-Bx112mWZ.js";
|
|
2
|
-
|
|
3
|
-
//#region src/resolver.d.ts
|
|
4
|
-
/**
|
|
5
|
-
* An option schema for an argument.
|
|
6
|
-
* This schema is similar to the schema of the `node:utils`.
|
|
7
|
-
* difference is that:
|
|
8
|
-
* - `multiple` property is not supported
|
|
9
|
-
* - `required` property and `description` property are added
|
|
10
|
-
* - `type` is not only 'string' and 'boolean', but also 'number' and 'enum' too.
|
|
11
|
-
* - `default` property type, not support multiple types
|
|
12
|
-
*/
|
|
13
|
-
/**
|
|
14
|
-
* An option schema for an argument.
|
|
15
|
-
* This schema is similar to the schema of the `node:utils`.
|
|
16
|
-
* difference is that:
|
|
17
|
-
* - `multiple` property is not supported
|
|
18
|
-
* - `required` property and `description` property are added
|
|
19
|
-
* - `type` is not only 'string' and 'boolean', but also 'number' and 'enum' too.
|
|
20
|
-
* - `default` property type, not support multiple types
|
|
21
|
-
*/
|
|
22
|
-
interface ArgOptionSchema {
|
|
23
|
-
/**
|
|
24
|
-
* Type of argument.
|
|
25
|
-
*/
|
|
26
|
-
type: "string" | "boolean" | "number" | "enum";
|
|
27
|
-
/**
|
|
28
|
-
* A single character alias for the option.
|
|
29
|
-
*/
|
|
30
|
-
short?: string;
|
|
31
|
-
/**
|
|
32
|
-
* A description of the argument.
|
|
33
|
-
*/
|
|
34
|
-
description?: string;
|
|
35
|
-
/**
|
|
36
|
-
* Whether the argument is required or not.
|
|
37
|
-
*/
|
|
38
|
-
required?: true;
|
|
39
|
-
/**
|
|
40
|
-
* Whether the negatable option for `boolean` type
|
|
41
|
-
*/
|
|
42
|
-
negatable?: boolean;
|
|
43
|
-
/**
|
|
44
|
-
* The allowed values of the argument, and string only. This property is only used when the type is 'enum'.
|
|
45
|
-
*/
|
|
46
|
-
choices?: string[] | readonly string[];
|
|
47
|
-
/**
|
|
48
|
-
* The default value of the argument.
|
|
49
|
-
* if the type is 'enum', the default value must be one of the allowed values.
|
|
50
|
-
*/
|
|
51
|
-
default?: string | boolean | number;
|
|
52
|
-
}
|
|
53
|
-
/**
|
|
54
|
-
* An object that contains {@link ArgOptionSchema | options schema}.
|
|
55
|
-
*/
|
|
56
|
-
interface ArgOptions {
|
|
57
|
-
[option: string]: ArgOptionSchema;
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* An object that contains the values of the arguments.
|
|
61
|
-
*/
|
|
62
|
-
type ArgValues<T> = T extends ArgOptions ? ResolveArgValues<T, { [Option in keyof T]: ExtractOptionValue<T[Option]> }> : {
|
|
63
|
-
[option: string]: string | boolean | number | undefined;
|
|
64
|
-
};
|
|
65
|
-
/**
|
|
66
|
-
* @internal
|
|
67
|
-
*/
|
|
68
|
-
type ExtractOptionValue<O extends ArgOptionSchema> = O["type"] extends "string" ? string : O["type"] extends "boolean" ? boolean : O["type"] extends "number" ? number : O["type"] extends "enum" ? O["choices"] extends string[] | readonly string[] ? O["choices"][number] : never : string | boolean | number;
|
|
69
|
-
/**
|
|
70
|
-
* @internal
|
|
71
|
-
*/
|
|
72
|
-
type ResolveArgValues<O extends ArgOptions, V extends Record<keyof O, unknown>> = { -readonly [Option in keyof O]?: V[Option] } & FilterArgs<O, V, "default"> & FilterArgs<O, V, "required"> extends infer P ? { [K in keyof P]: P[K] } : never;
|
|
73
|
-
/**
|
|
74
|
-
* @internal
|
|
75
|
-
*/
|
|
76
|
-
type FilterArgs<O extends ArgOptions, V extends Record<keyof O, unknown>, K extends keyof ArgOptionSchema> = { [Option in keyof O as O[Option][K] extends {} ? Option : never]: V[Option] };
|
|
77
|
-
/**
|
|
78
|
-
* An options for {@link resolveArgs | resolve arguments}.
|
|
79
|
-
*/
|
|
80
|
-
interface ResolveArgsOptions {
|
|
81
|
-
/**
|
|
82
|
-
* Whether to group short options.
|
|
83
|
-
* @default false
|
|
84
|
-
* @see guideline 5 in https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap12.html
|
|
85
|
-
*/
|
|
86
|
-
optionGrouping?: boolean;
|
|
87
|
-
}
|
|
88
|
-
/**
|
|
89
|
-
* Resolve command line arguments.
|
|
90
|
-
* @param options - An options that contains {@link ArgOptionSchema | options schema}.
|
|
91
|
-
* @param tokens - An array of {@link ArgToken | tokens}.
|
|
92
|
-
* @param resolveArgsOptions - An options that contains {@link resolveArgsOptions | resolve arguments options}.
|
|
93
|
-
* @returns An object that contains the values of the arguments, positional arguments, rest arguments, and {@link AggregateError | validation errors}.
|
|
94
|
-
*/
|
|
95
|
-
declare function resolveArgs<T extends ArgOptions>(options: T, tokens: ArgToken[], {
|
|
96
|
-
optionGrouping
|
|
97
|
-
}?: ResolveArgsOptions): {
|
|
98
|
-
values: ArgValues<T>;
|
|
99
|
-
positionals: string[];
|
|
100
|
-
rest: string[];
|
|
101
|
-
error: AggregateError | undefined;
|
|
102
|
-
};
|
|
103
|
-
/**
|
|
104
|
-
* An error type for {@link OptionResolveError}.
|
|
105
|
-
*/
|
|
106
|
-
type OptionResolveErrorType = "type" | "required";
|
|
107
|
-
/**
|
|
108
|
-
* An error that occurs when resolving options.
|
|
109
|
-
* This error is thrown when the option is not valid.
|
|
110
|
-
*/
|
|
111
|
-
declare class OptionResolveError extends Error {
|
|
112
|
-
name: string;
|
|
113
|
-
schema: ArgOptionSchema;
|
|
114
|
-
type: OptionResolveErrorType;
|
|
115
|
-
constructor(message: string, name: string, type: OptionResolveErrorType, schema: ArgOptionSchema);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
//#endregion
|
|
119
|
-
export { ArgOptionSchema, ArgOptions, ArgValues, ExtractOptionValue, FilterArgs, OptionResolveError as OptionResolveError$1, OptionResolveErrorType, ResolveArgValues, ResolveArgsOptions, resolveArgs as resolveArgs$1 };
|