args-tokens 0.15.1 → 0.16.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 +43 -44
- package/lib/index.js +19 -4
- package/lib/{parser-DxH6Mf-o.js → parser-Dr4iAGaX.js} +26 -1
- package/lib/parser.d-Cheh2e-O.d.ts +90 -0
- package/lib/parser.d.ts +1 -2
- package/lib/parser.js +1 -1
- package/lib/{resolver-D4hvuBLP.js → resolver-B89Gv70Z.js} +17 -6
- package/lib/resolver.d-D6XfwWOY.d.ts +115 -0
- package/lib/resolver.d.ts +2 -3
- package/lib/resolver.js +2 -2
- package/package.json +9 -9
- package/lib/parser.d-nUUBO6hL.d.ts +0 -99
- package/lib/resolver.d-D_30OItP.d.ts +0 -116
package/lib/index.d.ts
CHANGED
|
@@ -1,57 +1,56 @@
|
|
|
1
|
-
import { ArgToken, ParserOptions, parseArgs$1 as parseArgs } from "./parser.d-
|
|
2
|
-
import { ArgOptionSchema, ArgOptions, ArgValues, OptionResolveError$1 as OptionResolveError, OptionResolveErrorType, ResolveArgsOptions, resolveArgs$1 as resolveArgs } from "./resolver.d-
|
|
1
|
+
import { ArgToken, ParserOptions, parseArgs$1 as parseArgs } from "./parser.d-Cheh2e-O.js";
|
|
2
|
+
import { ArgOptionSchema, ArgOptions, ArgValues, OptionResolveError$1 as OptionResolveError, OptionResolveErrorType, ResolveArgsOptions, resolveArgs$1 as resolveArgs } from "./resolver.d-D6XfwWOY.js";
|
|
3
3
|
|
|
4
4
|
//#region src/parse.d.ts
|
|
5
5
|
/**
|
|
6
6
|
* Parse options for {@link parse} function.
|
|
7
7
|
*/
|
|
8
8
|
interface ParseOptions<O extends ArgOptions> extends ParserOptions, ResolveArgsOptions {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
/**
|
|
10
|
+
* Command line options, about details see {@link ArgOptions}.
|
|
11
|
+
*/
|
|
12
|
+
options?: O;
|
|
13
13
|
}
|
|
14
|
+
|
|
14
15
|
/**
|
|
15
16
|
* Parsed command line arguments.
|
|
16
17
|
*/
|
|
17
18
|
type ParsedArgs<T extends ArgOptions> = {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
* console.log('
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
* @param
|
|
51
|
-
* @
|
|
52
|
-
|
|
53
|
-
*/
|
|
54
|
-
declare function parse<O extends ArgOptions>(args: string[], options?: ParseOptions<O>): ParsedArgs<O>;
|
|
19
|
+
/**
|
|
20
|
+
* Parsed values, same as `values` in {@link resolveArgs}.
|
|
21
|
+
*/
|
|
22
|
+
values: ArgValues<T>;
|
|
23
|
+
/**
|
|
24
|
+
* Positional arguments, same as `positionals` in {@link resolveArgs}.
|
|
25
|
+
*/
|
|
26
|
+
positionals: string[];
|
|
27
|
+
/**
|
|
28
|
+
* Rest arguments, same as `rest` in {@link resolveArgs}.
|
|
29
|
+
*/
|
|
30
|
+
rest: string[];
|
|
31
|
+
/**
|
|
32
|
+
* Validation errors, same as `errors` in {@link resolveArgs}.
|
|
33
|
+
*/
|
|
34
|
+
error: AggregateError | undefined;
|
|
35
|
+
/**
|
|
36
|
+
* Argument tokens, same as `tokens` which is parsed by {@link parseArgs}.
|
|
37
|
+
*/
|
|
38
|
+
tokens: ArgToken[];
|
|
39
|
+
}; /**
|
|
40
|
+
* Parse command line arguments.
|
|
41
|
+
* This function is a convenient API, that is used {@link parseArgs} and {@link resolveArgs} in internal.
|
|
42
|
+
* @example
|
|
43
|
+
* ```js
|
|
44
|
+
* import { parse } from 'args-tokens'
|
|
45
|
+
*
|
|
46
|
+
* const { values, positionals } = parse(process.argv.slice(2))
|
|
47
|
+
* console.log('values', values)
|
|
48
|
+
* console.log('positionals', positionals)
|
|
49
|
+
* ```
|
|
50
|
+
* @param args - command line arguments
|
|
51
|
+
* @param options - parse options, about details see {@link ParseOptions}
|
|
52
|
+
* @returns An object that contains the values of the arguments, positional arguments, {@link AggregateError | validation errors}, and {@link ArgToken | argument tokens}.
|
|
53
|
+
*/
|
|
55
54
|
|
|
56
|
-
//#endregion
|
|
55
|
+
declare function parse<O extends ArgOptions>(args: string[], options?: ParseOptions<O>): ParsedArgs<O>; //#endregion
|
|
57
56
|
export { ArgOptionSchema, ArgOptions, ArgToken, ArgValues, OptionResolveError, OptionResolveErrorType, ParseOptions, ParsedArgs, ParserOptions, ResolveArgsOptions, parse, parseArgs, resolveArgs };
|
package/lib/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { parseArgs } from "./parser-
|
|
2
|
-
import { OptionResolveError, resolveArgs } from "./resolver-
|
|
1
|
+
import { parseArgs } from "./parser-Dr4iAGaX.js";
|
|
2
|
+
import { OptionResolveError, resolveArgs } from "./resolver-B89Gv70Z.js";
|
|
3
3
|
|
|
4
4
|
//#region src/parse.ts
|
|
5
5
|
const DEFAULT_OPTIONS = {
|
|
@@ -12,10 +12,25 @@ const DEFAULT_OPTIONS = {
|
|
|
12
12
|
short: "v"
|
|
13
13
|
}
|
|
14
14
|
};
|
|
15
|
+
/**
|
|
16
|
+
* Parse command line arguments.
|
|
17
|
+
* This function is a convenient API, that is used {@link parseArgs} and {@link resolveArgs} in internal.
|
|
18
|
+
* @example
|
|
19
|
+
* ```js
|
|
20
|
+
* import { parse } from 'args-tokens'
|
|
21
|
+
*
|
|
22
|
+
* const { values, positionals } = parse(process.argv.slice(2))
|
|
23
|
+
* console.log('values', values)
|
|
24
|
+
* console.log('positionals', positionals)
|
|
25
|
+
* ```
|
|
26
|
+
* @param args - command line arguments
|
|
27
|
+
* @param options - parse options, about details see {@link ParseOptions}
|
|
28
|
+
* @returns An object that contains the values of the arguments, positional arguments, {@link AggregateError | validation errors}, and {@link ArgToken | argument tokens}.
|
|
29
|
+
*/
|
|
15
30
|
function parse(args, options = {}) {
|
|
16
|
-
const { options: argOptions, allowCompatible = false
|
|
31
|
+
const { options: argOptions, allowCompatible = false } = options;
|
|
17
32
|
const tokens = parseArgs(args, { allowCompatible });
|
|
18
|
-
return Object.assign(Object.create(null), resolveArgs(argOptions || DEFAULT_OPTIONS, tokens
|
|
33
|
+
return Object.assign(Object.create(null), resolveArgs(argOptions || DEFAULT_OPTIONS, tokens), { tokens });
|
|
19
34
|
}
|
|
20
35
|
|
|
21
36
|
//#endregion
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
|
|
2
1
|
//#region src/parser.ts
|
|
3
2
|
const HYPHEN_CHAR = "-";
|
|
4
3
|
const HYPHEN_CODE = HYPHEN_CHAR.codePointAt(0);
|
|
@@ -7,6 +6,22 @@ const EQUAL_CODE = EQUAL_CHAR.codePointAt(0);
|
|
|
7
6
|
const TERMINATOR = "--";
|
|
8
7
|
const SHORT_OPTION_PREFIX = HYPHEN_CHAR;
|
|
9
8
|
const LONG_OPTION_PREFIX = "--";
|
|
9
|
+
/**
|
|
10
|
+
* Parse command line arguments.
|
|
11
|
+
* @example
|
|
12
|
+
* ```js
|
|
13
|
+
* import { parseArgs } from 'args-tokens' // for Node.js and Bun
|
|
14
|
+
* // import { parseArgs } from 'jsr:@kazupon/args-tokens' // for Deno
|
|
15
|
+
*
|
|
16
|
+
* const tokens = parseArgs(['--foo', 'bar', '-x', '--bar=baz'])
|
|
17
|
+
* // do something with using tokens
|
|
18
|
+
* // ...
|
|
19
|
+
* console.log('tokens:', tokens)
|
|
20
|
+
* ```
|
|
21
|
+
* @param args command line arguments
|
|
22
|
+
* @param options parse options
|
|
23
|
+
* @returns Argument tokens.
|
|
24
|
+
*/
|
|
10
25
|
function parseArgs(args, options = {}) {
|
|
11
26
|
const { allowCompatible = false } = options;
|
|
12
27
|
const tokens = [];
|
|
@@ -120,6 +135,11 @@ function parseArgs(args, options = {}) {
|
|
|
120
135
|
}
|
|
121
136
|
return tokens;
|
|
122
137
|
}
|
|
138
|
+
/**
|
|
139
|
+
* Check if `arg` is a short option (e.g. `-f`).
|
|
140
|
+
* @param arg the argument to check
|
|
141
|
+
* @returns whether `arg` is a short option.
|
|
142
|
+
*/
|
|
123
143
|
function isShortOption(arg) {
|
|
124
144
|
return arg.length === 2 && arg.codePointAt(0) === HYPHEN_CODE && arg.codePointAt(1) !== HYPHEN_CODE;
|
|
125
145
|
}
|
|
@@ -150,6 +170,11 @@ function isLongOption(arg) {
|
|
|
150
170
|
function isLongOptionAndValue(arg) {
|
|
151
171
|
return hasLongOptionPrefix(arg) && arg.includes(EQUAL_CHAR, 3);
|
|
152
172
|
}
|
|
173
|
+
/**
|
|
174
|
+
* Check if `arg` is a long option prefix (e.g. `--`).
|
|
175
|
+
* @param arg the argument to check
|
|
176
|
+
* @returns whether `arg` is a long option prefix.
|
|
177
|
+
*/
|
|
153
178
|
function hasLongOptionPrefix(arg) {
|
|
154
179
|
return arg.length > 2 && ~arg.indexOf(LONG_OPTION_PREFIX);
|
|
155
180
|
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
//#region src/parser.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Entry point of argument parser.
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* forked from `nodejs/node` (`pkgjs/parseargs`)
|
|
8
|
+
* repository url: https://github.com/nodejs/node (https://github.com/pkgjs/parseargs)
|
|
9
|
+
* code url: https://github.com/nodejs/node/blob/main/lib/internal/util/parse_args/parse_args.js
|
|
10
|
+
*
|
|
11
|
+
* @author kazuya kawaguchi (a.k.a. kazupon)
|
|
12
|
+
* @license MIT
|
|
13
|
+
*/
|
|
14
|
+
/**
|
|
15
|
+
* Argument token Kind.
|
|
16
|
+
* - `option`: option token, support short option (e.g. `-x`) and long option (e.g. `--foo`)
|
|
17
|
+
* - `option-terminator`: option terminator (`--`) token, see guideline 10 in https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html
|
|
18
|
+
* - `positional`: positional token
|
|
19
|
+
*/type ArgTokenKind = "option" | "option-terminator" | "positional";
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Argument token.
|
|
23
|
+
*/
|
|
24
|
+
interface ArgToken {
|
|
25
|
+
/**
|
|
26
|
+
* Argument token kind.
|
|
27
|
+
*/
|
|
28
|
+
kind: ArgTokenKind;
|
|
29
|
+
/**
|
|
30
|
+
* Argument token index, e.g `--foo bar` => `--foo` index is 0, `bar` index is 1.
|
|
31
|
+
*/
|
|
32
|
+
index: number;
|
|
33
|
+
/**
|
|
34
|
+
* Option name, e.g. `--foo` => `foo`, `-x` => `x`.
|
|
35
|
+
*/
|
|
36
|
+
name?: string;
|
|
37
|
+
/**
|
|
38
|
+
* Raw option name, e.g. `--foo` => `--foo`, `-x` => `-x`.
|
|
39
|
+
*/
|
|
40
|
+
rawName?: string;
|
|
41
|
+
/**
|
|
42
|
+
* Option value, e.g. `--foo=bar` => `bar`, `-x=bar` => `bar`.
|
|
43
|
+
* If the `allowCompatible` option is `true`, short option value will be same as Node.js `parseArgs` behavior.
|
|
44
|
+
*/
|
|
45
|
+
value?: string;
|
|
46
|
+
/**
|
|
47
|
+
* Inline value, e.g. `--foo=bar` => `true`, `-x=bar` => `true`.
|
|
48
|
+
*/
|
|
49
|
+
inlineValue?: boolean;
|
|
50
|
+
} /**
|
|
51
|
+
* Parser Options.
|
|
52
|
+
*/
|
|
53
|
+
|
|
54
|
+
interface ParserOptions {
|
|
55
|
+
/**
|
|
56
|
+
* [Node.js parseArgs](https://nodejs.org/api/util.html#parseargs-tokens) tokens compatible mode.
|
|
57
|
+
* @default false
|
|
58
|
+
*/
|
|
59
|
+
allowCompatible?: boolean;
|
|
60
|
+
} /**
|
|
61
|
+
* Parse command line arguments.
|
|
62
|
+
* @example
|
|
63
|
+
* ```js
|
|
64
|
+
* import { parseArgs } from 'args-tokens' // for Node.js and Bun
|
|
65
|
+
* // import { parseArgs } from 'jsr:@kazupon/args-tokens' // for Deno
|
|
66
|
+
*
|
|
67
|
+
* const tokens = parseArgs(['--foo', 'bar', '-x', '--bar=baz'])
|
|
68
|
+
* // do something with using tokens
|
|
69
|
+
* // ...
|
|
70
|
+
* console.log('tokens:', tokens)
|
|
71
|
+
* ```
|
|
72
|
+
* @param args command line arguments
|
|
73
|
+
* @param options parse options
|
|
74
|
+
* @returns Argument tokens.
|
|
75
|
+
*/
|
|
76
|
+
|
|
77
|
+
declare function parseArgs(args: string[], options?: ParserOptions): ArgToken[]; /**
|
|
78
|
+
* Check if `arg` is a short option (e.g. `-f`).
|
|
79
|
+
* @param arg the argument to check
|
|
80
|
+
* @returns whether `arg` is a short option.
|
|
81
|
+
*/
|
|
82
|
+
|
|
83
|
+
declare function isShortOption(arg: string): boolean; /**
|
|
84
|
+
* Check if `arg` is a long option prefix (e.g. `--`).
|
|
85
|
+
* @param arg the argument to check
|
|
86
|
+
* @returns whether `arg` is a long option prefix.
|
|
87
|
+
*/
|
|
88
|
+
|
|
89
|
+
declare function hasLongOptionPrefix(arg: string): boolean; //#endregion
|
|
90
|
+
export { ArgToken, ParserOptions, hasLongOptionPrefix as hasLongOptionPrefix$1, isShortOption as isShortOption$1, parseArgs as parseArgs$1 };
|
package/lib/parser.d.ts
CHANGED
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
import { ArgToken, ParserOptions, hasLongOptionPrefix$1 as hasLongOptionPrefix, isShortOption$1 as isShortOption, parseArgs$1 as parseArgs } from "./parser.d-
|
|
2
|
-
|
|
1
|
+
import { ArgToken, ParserOptions, hasLongOptionPrefix$1 as hasLongOptionPrefix, isShortOption$1 as isShortOption, parseArgs$1 as parseArgs } from "./parser.d-Cheh2e-O.js";
|
|
3
2
|
export { ArgToken, ParserOptions, hasLongOptionPrefix, isShortOption, parseArgs };
|
package/lib/parser.js
CHANGED
|
@@ -1,7 +1,14 @@
|
|
|
1
|
-
import { hasLongOptionPrefix, isShortOption } from "./parser-
|
|
1
|
+
import { hasLongOptionPrefix, isShortOption } from "./parser-Dr4iAGaX.js";
|
|
2
2
|
|
|
3
3
|
//#region src/resolver.ts
|
|
4
|
-
|
|
4
|
+
/**
|
|
5
|
+
* Resolve command line arguments.
|
|
6
|
+
* @param options - An options that contains {@link ArgOptionSchema | options schema}.
|
|
7
|
+
* @param tokens - An array of {@link ArgToken | tokens}.
|
|
8
|
+
* @param resolveArgsOptions - An options that contains {@link resolveArgsOptions | resolve arguments options}.
|
|
9
|
+
* @returns An object that contains the values of the arguments, positional arguments, rest arguments, and {@link AggregateError | validation errors}.
|
|
10
|
+
*/
|
|
11
|
+
function resolveArgs(options, tokens, { optionGrouping = false } = {}) {
|
|
5
12
|
const positionals = [];
|
|
6
13
|
const rest = [];
|
|
7
14
|
const longOptionTokens = [];
|
|
@@ -100,7 +107,7 @@ function resolveArgs(options, tokens, { optionGrouping = false, allowNegative =
|
|
|
100
107
|
const values = Object.create(null);
|
|
101
108
|
const errors = [];
|
|
102
109
|
function checkTokenName(option, schema, token) {
|
|
103
|
-
return token.name === (schema.type === "boolean" ?
|
|
110
|
+
return token.name === (schema.type === "boolean" ? schema.negatable && token.name?.startsWith("no-") ? `no-${option}` : option : option);
|
|
104
111
|
}
|
|
105
112
|
for (const [option, schema] of Object.entries(options)) {
|
|
106
113
|
if (schema.required) {
|
|
@@ -126,7 +133,7 @@ function resolveArgs(options, tokens, { optionGrouping = false, allowNegative =
|
|
|
126
133
|
continue;
|
|
127
134
|
}
|
|
128
135
|
}
|
|
129
|
-
values[option] = resolveOptionValue(token, schema
|
|
136
|
+
values[option] = resolveOptionValue(token, schema);
|
|
130
137
|
continue;
|
|
131
138
|
}
|
|
132
139
|
}
|
|
@@ -162,6 +169,10 @@ function resolveArgs(options, tokens, { optionGrouping = false, allowNegative =
|
|
|
162
169
|
function createRequireError(option, schema) {
|
|
163
170
|
return new OptionResolveError(`Option '--${option}' ${schema.short ? `or '-${schema.short}' ` : ""}is required`, option, "required", schema);
|
|
164
171
|
}
|
|
172
|
+
/**
|
|
173
|
+
* An error that occurs when resolving options.
|
|
174
|
+
* This error is thrown when the option is not valid.
|
|
175
|
+
*/
|
|
165
176
|
var OptionResolveError = class extends Error {
|
|
166
177
|
name;
|
|
167
178
|
schema;
|
|
@@ -198,9 +209,9 @@ function isNumeric(str) {
|
|
|
198
209
|
function createTypeError(option, schema) {
|
|
199
210
|
return new OptionResolveError(`Option '--${option}' ${schema.short ? `or '-${schema.short}' ` : ""}should be '${schema.type}'`, option, "type", schema);
|
|
200
211
|
}
|
|
201
|
-
function resolveOptionValue(token, schema
|
|
212
|
+
function resolveOptionValue(token, schema) {
|
|
202
213
|
if (token.value) return schema.type === "number" ? +token.value : token.value;
|
|
203
|
-
if (schema.type === "boolean") return
|
|
214
|
+
if (schema.type === "boolean") return schema.negatable && token.name.startsWith("no-") ? false : true;
|
|
204
215
|
return schema.type === "number" ? +(schema.default || "") : schema.default;
|
|
205
216
|
}
|
|
206
217
|
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { ArgToken } from "./parser.d-Cheh2e-O.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
|
+
interface ArgOptionSchema {
|
|
14
|
+
/**
|
|
15
|
+
* Type of argument.
|
|
16
|
+
*/
|
|
17
|
+
type: "string" | "boolean" | "number" | "enum";
|
|
18
|
+
/**
|
|
19
|
+
* A single character alias for the option.
|
|
20
|
+
*/
|
|
21
|
+
short?: string;
|
|
22
|
+
/**
|
|
23
|
+
* A description of the argument.
|
|
24
|
+
*/
|
|
25
|
+
description?: string;
|
|
26
|
+
/**
|
|
27
|
+
* Whether the argument is required or not.
|
|
28
|
+
*/
|
|
29
|
+
required?: true;
|
|
30
|
+
/**
|
|
31
|
+
* Whether the negatable option for `boolean` type
|
|
32
|
+
*/
|
|
33
|
+
negatable?: boolean;
|
|
34
|
+
/**
|
|
35
|
+
* The allowed values of the argument, and string only. This property is only used when the type is 'enum'.
|
|
36
|
+
*/
|
|
37
|
+
choices?: string[];
|
|
38
|
+
/**
|
|
39
|
+
* The default value of the argument.
|
|
40
|
+
* if the type is 'enum', the default value must be one of the allowed values.
|
|
41
|
+
*/
|
|
42
|
+
default?: string | boolean | number;
|
|
43
|
+
} /**
|
|
44
|
+
* An object that contains {@link ArgOptionSchema | options schema}.
|
|
45
|
+
*/
|
|
46
|
+
|
|
47
|
+
interface ArgOptions {
|
|
48
|
+
[option: string]: ArgOptionSchema;
|
|
49
|
+
} /**
|
|
50
|
+
* An object that contains the values of the arguments.
|
|
51
|
+
*/
|
|
52
|
+
|
|
53
|
+
type ArgValues<T> = T extends ArgOptions ? ResolveArgValues<T, { [Option in keyof T]: ExtractOptionValue<T[Option]> }> : {
|
|
54
|
+
[option: string]: string | boolean | number | undefined;
|
|
55
|
+
}; /**
|
|
56
|
+
* @internal
|
|
57
|
+
*/
|
|
58
|
+
|
|
59
|
+
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[] ? O["choices"][number] : never : string | boolean | number; /**
|
|
60
|
+
* @internal
|
|
61
|
+
*/
|
|
62
|
+
|
|
63
|
+
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;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* @internal
|
|
67
|
+
*/
|
|
68
|
+
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] };
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* An options for {@link resolveArgs | resolve arguments}.
|
|
72
|
+
*/
|
|
73
|
+
interface ResolveArgsOptions {
|
|
74
|
+
/**
|
|
75
|
+
* Whether to group short options.
|
|
76
|
+
* @default false
|
|
77
|
+
* @see guideline 5 in https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap12.html
|
|
78
|
+
*/
|
|
79
|
+
optionGrouping?: boolean;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Resolve command line arguments.
|
|
84
|
+
* @param options - An options that contains {@link ArgOptionSchema | options schema}.
|
|
85
|
+
* @param tokens - An array of {@link ArgToken | tokens}.
|
|
86
|
+
* @param resolveArgsOptions - An options that contains {@link resolveArgsOptions | resolve arguments options}.
|
|
87
|
+
* @returns An object that contains the values of the arguments, positional arguments, rest arguments, and {@link AggregateError | validation errors}.
|
|
88
|
+
*/
|
|
89
|
+
declare function resolveArgs<T extends ArgOptions>(options: T, tokens: ArgToken[], {
|
|
90
|
+
optionGrouping
|
|
91
|
+
}?: ResolveArgsOptions): {
|
|
92
|
+
values: ArgValues<T>;
|
|
93
|
+
positionals: string[];
|
|
94
|
+
rest: string[];
|
|
95
|
+
error: AggregateError | undefined;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* An error type for {@link OptionResolveError}.
|
|
100
|
+
*/
|
|
101
|
+
type OptionResolveErrorType = "type" | "required";
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* An error that occurs when resolving options.
|
|
105
|
+
* This error is thrown when the option is not valid.
|
|
106
|
+
*/
|
|
107
|
+
declare class OptionResolveError extends Error {
|
|
108
|
+
name: string;
|
|
109
|
+
schema: ArgOptionSchema;
|
|
110
|
+
type: OptionResolveErrorType;
|
|
111
|
+
constructor(message: string, name: string, type: OptionResolveErrorType, schema: ArgOptionSchema);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
//#endregion
|
|
115
|
+
export { ArgOptionSchema, ArgOptions, ArgValues, ExtractOptionValue, FilterArgs, OptionResolveError as OptionResolveError$1, OptionResolveErrorType, ResolveArgValues, ResolveArgsOptions, resolveArgs as resolveArgs$1 };
|
package/lib/resolver.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import "./parser.d-
|
|
2
|
-
import { ArgOptionSchema, ArgOptions, ArgValues, ExtractOptionValue, FilterArgs, OptionResolveError$1 as OptionResolveError, OptionResolveErrorType, ResolveArgValues, ResolveArgsOptions, resolveArgs$1 as resolveArgs } from "./resolver.d-
|
|
3
|
-
|
|
1
|
+
import "./parser.d-Cheh2e-O.js";
|
|
2
|
+
import { ArgOptionSchema, ArgOptions, ArgValues, ExtractOptionValue, FilterArgs, OptionResolveError$1 as OptionResolveError, OptionResolveErrorType, ResolveArgValues, ResolveArgsOptions, resolveArgs$1 as resolveArgs } from "./resolver.d-D6XfwWOY.js";
|
|
4
3
|
export { ArgOptionSchema, ArgOptions, ArgValues, ExtractOptionValue, FilterArgs, OptionResolveError, OptionResolveErrorType, ResolveArgValues, ResolveArgsOptions, 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.16.0",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "kazuya kawaguchi",
|
|
7
7
|
"email": "kawakazu80@gmail.com"
|
|
@@ -68,29 +68,29 @@
|
|
|
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.
|
|
71
|
+
"@types/node": "^22.15.2",
|
|
72
72
|
"@vitest/eslint-plugin": "^1.1.43",
|
|
73
73
|
"bumpp": "^10.1.0",
|
|
74
|
-
"deno": "^2.2.
|
|
75
|
-
"eslint": "^9.25.
|
|
74
|
+
"deno": "^2.2.12",
|
|
75
|
+
"eslint": "^9.25.1",
|
|
76
76
|
"eslint-config-prettier": "^10.1.2",
|
|
77
77
|
"eslint-plugin-jsonc": "^2.20.0",
|
|
78
78
|
"eslint-plugin-promise": "^7.2.1",
|
|
79
79
|
"eslint-plugin-regexp": "^2.7.0",
|
|
80
80
|
"eslint-plugin-unicorn": "^58.0.0",
|
|
81
|
-
"eslint-plugin-yml": "^1.
|
|
81
|
+
"eslint-plugin-yml": "^1.18.0",
|
|
82
82
|
"gh-changelogen": "^0.2.8",
|
|
83
83
|
"jsr": "^0.13.4",
|
|
84
|
+
"jsr-exports-lint": "^0.2.0",
|
|
84
85
|
"knip": "^5.50.5",
|
|
85
86
|
"lint-staged": "^15.5.1",
|
|
86
87
|
"mitata": "^1.0.34",
|
|
87
88
|
"pkg-pr-new": "^0.0.43",
|
|
88
89
|
"prettier": "^3.5.3",
|
|
89
|
-
"tsdown": "^0.
|
|
90
|
-
"tsdown-jsr-exports-lint": "^0.1.4",
|
|
90
|
+
"tsdown": "^0.10.0",
|
|
91
91
|
"typescript": "^5.8.3",
|
|
92
|
-
"typescript-eslint": "^8.
|
|
93
|
-
"vitest": "^3.1.
|
|
92
|
+
"typescript-eslint": "^8.31.0",
|
|
93
|
+
"vitest": "^3.1.2"
|
|
94
94
|
},
|
|
95
95
|
"prettier": "@kazupon/prettier-config",
|
|
96
96
|
"lint-staged": {
|
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
//#region src/parser.d.ts
|
|
3
|
-
/**
|
|
4
|
-
* Entry point of argument parser.
|
|
5
|
-
* @module
|
|
6
|
-
*/
|
|
7
|
-
/**
|
|
8
|
-
* forked from `nodejs/node` (`pkgjs/parseargs`)
|
|
9
|
-
* repository url: https://github.com/nodejs/node (https://github.com/pkgjs/parseargs)
|
|
10
|
-
* code url: https://github.com/nodejs/node/blob/main/lib/internal/util/parse_args/parse_args.js
|
|
11
|
-
*
|
|
12
|
-
* @author kazuya kawaguchi (a.k.a. kazupon)
|
|
13
|
-
* @license MIT
|
|
14
|
-
*/
|
|
15
|
-
/**
|
|
16
|
-
* Argument token Kind.
|
|
17
|
-
* - `option`: option token, support short option (e.g. `-x`) and long option (e.g. `--foo`)
|
|
18
|
-
* - `option-terminator`: option terminator (`--`) token, see guideline 10 in https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html
|
|
19
|
-
* - `positional`: positional token
|
|
20
|
-
*/
|
|
21
|
-
/**
|
|
22
|
-
* Argument token Kind.
|
|
23
|
-
* - `option`: option token, support short option (e.g. `-x`) and long option (e.g. `--foo`)
|
|
24
|
-
* - `option-terminator`: option terminator (`--`) token, see guideline 10 in https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html
|
|
25
|
-
* - `positional`: positional token
|
|
26
|
-
*/
|
|
27
|
-
type ArgTokenKind = "option" | "option-terminator" | "positional";
|
|
28
|
-
/**
|
|
29
|
-
* Argument token.
|
|
30
|
-
*/
|
|
31
|
-
interface ArgToken {
|
|
32
|
-
/**
|
|
33
|
-
* Argument token kind.
|
|
34
|
-
*/
|
|
35
|
-
kind: ArgTokenKind;
|
|
36
|
-
/**
|
|
37
|
-
* Argument token index, e.g `--foo bar` => `--foo` index is 0, `bar` index is 1.
|
|
38
|
-
*/
|
|
39
|
-
index: number;
|
|
40
|
-
/**
|
|
41
|
-
* Option name, e.g. `--foo` => `foo`, `-x` => `x`.
|
|
42
|
-
*/
|
|
43
|
-
name?: string;
|
|
44
|
-
/**
|
|
45
|
-
* Raw option name, e.g. `--foo` => `--foo`, `-x` => `-x`.
|
|
46
|
-
*/
|
|
47
|
-
rawName?: string;
|
|
48
|
-
/**
|
|
49
|
-
* Option value, e.g. `--foo=bar` => `bar`, `-x=bar` => `bar`.
|
|
50
|
-
* If the `allowCompatible` option is `true`, short option value will be same as Node.js `parseArgs` behavior.
|
|
51
|
-
*/
|
|
52
|
-
value?: string;
|
|
53
|
-
/**
|
|
54
|
-
* Inline value, e.g. `--foo=bar` => `true`, `-x=bar` => `true`.
|
|
55
|
-
*/
|
|
56
|
-
inlineValue?: boolean;
|
|
57
|
-
}
|
|
58
|
-
/**
|
|
59
|
-
* Parser Options.
|
|
60
|
-
*/
|
|
61
|
-
interface ParserOptions {
|
|
62
|
-
/**
|
|
63
|
-
* [Node.js parseArgs](https://nodejs.org/api/util.html#parseargs-tokens) tokens compatible mode.
|
|
64
|
-
* @default false
|
|
65
|
-
*/
|
|
66
|
-
allowCompatible?: boolean;
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* Parse command line arguments.
|
|
70
|
-
* @example
|
|
71
|
-
* ```js
|
|
72
|
-
* import { parseArgs } from 'args-tokens' // for Node.js and Bun
|
|
73
|
-
* // import { parseArgs } from 'jsr:@kazupon/args-tokens' // for Deno
|
|
74
|
-
*
|
|
75
|
-
* const tokens = parseArgs(['--foo', 'bar', '-x', '--bar=baz'])
|
|
76
|
-
* // do something with using tokens
|
|
77
|
-
* // ...
|
|
78
|
-
* console.log('tokens:', tokens)
|
|
79
|
-
* ```
|
|
80
|
-
* @param args command line arguments
|
|
81
|
-
* @param options parse options
|
|
82
|
-
* @returns Argument tokens.
|
|
83
|
-
*/
|
|
84
|
-
declare function parseArgs(args: string[], options?: ParserOptions): ArgToken[];
|
|
85
|
-
/**
|
|
86
|
-
* Check if `arg` is a short option (e.g. `-f`).
|
|
87
|
-
* @param arg the argument to check
|
|
88
|
-
* @returns whether `arg` is a short option.
|
|
89
|
-
*/
|
|
90
|
-
declare function isShortOption(arg: string): boolean;
|
|
91
|
-
/**
|
|
92
|
-
* Check if `arg` is a long option prefix (e.g. `--`).
|
|
93
|
-
* @param arg the argument to check
|
|
94
|
-
* @returns whether `arg` is a long option prefix.
|
|
95
|
-
*/
|
|
96
|
-
declare function hasLongOptionPrefix(arg: string): boolean;
|
|
97
|
-
|
|
98
|
-
//#endregion
|
|
99
|
-
export { ArgToken, ParserOptions, hasLongOptionPrefix as hasLongOptionPrefix$1, isShortOption as isShortOption$1, parseArgs as parseArgs$1 };
|
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
import { ArgToken } from "./parser.d-nUUBO6hL.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
|
-
interface ArgOptionSchema {
|
|
14
|
-
/**
|
|
15
|
-
* Type of argument.
|
|
16
|
-
*/
|
|
17
|
-
type: "string" | "boolean" | "number" | "enum";
|
|
18
|
-
/**
|
|
19
|
-
* A single character alias for the option.
|
|
20
|
-
*/
|
|
21
|
-
short?: string;
|
|
22
|
-
/**
|
|
23
|
-
* A description of the argument.
|
|
24
|
-
*/
|
|
25
|
-
description?: string;
|
|
26
|
-
/**
|
|
27
|
-
* Whether the argument is required or not.
|
|
28
|
-
*/
|
|
29
|
-
required?: true;
|
|
30
|
-
/**
|
|
31
|
-
* The allowed values of the argument, and string only. This property is only used when the type is 'enum'.
|
|
32
|
-
*/
|
|
33
|
-
choices?: string[];
|
|
34
|
-
/**
|
|
35
|
-
* The default value of the argument.
|
|
36
|
-
* if the type is 'enum', the default value must be one of the allowed values.
|
|
37
|
-
*/
|
|
38
|
-
default?: string | boolean | number;
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* An object that contains {@link ArgOptionSchema | options schema}.
|
|
42
|
-
*/
|
|
43
|
-
interface ArgOptions {
|
|
44
|
-
[option: string]: ArgOptionSchema;
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* An object that contains the values of the arguments.
|
|
48
|
-
*/
|
|
49
|
-
type ArgValues<T> = T extends ArgOptions ? ResolveArgValues<T, { [Option in keyof T] : ExtractOptionValue<T[Option]> }> : {
|
|
50
|
-
[option: string]: string | boolean | number | undefined
|
|
51
|
-
};
|
|
52
|
-
/**
|
|
53
|
-
* @internal
|
|
54
|
-
*/
|
|
55
|
-
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[] ? O["choices"][number] : never : string | boolean | number;
|
|
56
|
-
/**
|
|
57
|
-
* @internal
|
|
58
|
-
*/
|
|
59
|
-
type ResolveArgValues<
|
|
60
|
-
O extends ArgOptions,
|
|
61
|
-
V extends Record<keyof O, unknown>
|
|
62
|
-
> = { -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;
|
|
63
|
-
/**
|
|
64
|
-
* @internal
|
|
65
|
-
*/
|
|
66
|
-
type FilterArgs<
|
|
67
|
-
O extends ArgOptions,
|
|
68
|
-
V extends Record<keyof O, unknown>,
|
|
69
|
-
K extends keyof ArgOptionSchema
|
|
70
|
-
> = { [Option in keyof O as O[Option][K] extends {} ? Option : never] : V[Option] };
|
|
71
|
-
/**
|
|
72
|
-
* An options for {@link resolveArgs | resolve arguments}.
|
|
73
|
-
*/
|
|
74
|
-
interface ResolveArgsOptions {
|
|
75
|
-
/**
|
|
76
|
-
* Whether to group short options.
|
|
77
|
-
* @default false
|
|
78
|
-
* @see guideline 5 in https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap12.html
|
|
79
|
-
*/
|
|
80
|
-
optionGrouping?: boolean;
|
|
81
|
-
/**
|
|
82
|
-
* Whether to set the flag value of options prefixed with `--no-` to negative.
|
|
83
|
-
* @default false
|
|
84
|
-
*/
|
|
85
|
-
allowNegative?: boolean;
|
|
86
|
-
}
|
|
87
|
-
/**
|
|
88
|
-
* Resolve command line arguments.
|
|
89
|
-
* @param options - An options that contains {@link ArgOptionSchema | options schema}.
|
|
90
|
-
* @param tokens - An array of {@link ArgToken | tokens}.
|
|
91
|
-
* @param resolveArgsOptions - An options that contains {@link resolveArgsOptions | resolve arguments options}.
|
|
92
|
-
* @returns An object that contains the values of the arguments, positional arguments, rest arguments, and {@link AggregateError | validation errors}.
|
|
93
|
-
*/
|
|
94
|
-
declare function resolveArgs<T extends ArgOptions>(options: T, tokens: ArgToken[], { optionGrouping, allowNegative }?: ResolveArgsOptions): {
|
|
95
|
-
values: ArgValues<T>
|
|
96
|
-
positionals: string[]
|
|
97
|
-
rest: string[]
|
|
98
|
-
error: AggregateError | undefined
|
|
99
|
-
};
|
|
100
|
-
/**
|
|
101
|
-
* An error type for {@link OptionResolveError}.
|
|
102
|
-
*/
|
|
103
|
-
type OptionResolveErrorType = "type" | "required";
|
|
104
|
-
/**
|
|
105
|
-
* An error that occurs when resolving options.
|
|
106
|
-
* This error is thrown when the option is not valid.
|
|
107
|
-
*/
|
|
108
|
-
declare class OptionResolveError extends Error {
|
|
109
|
-
name: string;
|
|
110
|
-
schema: ArgOptionSchema;
|
|
111
|
-
type: OptionResolveErrorType;
|
|
112
|
-
constructor(message: string, name: string, type: OptionResolveErrorType, schema: ArgOptionSchema);
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
//#endregion
|
|
116
|
-
export { ArgOptionSchema, ArgOptions, ArgValues, ExtractOptionValue, FilterArgs, OptionResolveError as OptionResolveError$1, OptionResolveErrorType, ResolveArgValues, ResolveArgsOptions, resolveArgs as resolveArgs$1 };
|