args-tokens 0.14.0 â 0.15.1
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/README.md +10 -0
- package/lib/index.d.ts +15 -8
- package/lib/index.js +3 -3
- package/lib/parser.d-nUUBO6hL.d.ts +99 -0
- package/lib/parser.d.ts +2 -82
- package/lib/{resolver-BFy1oVrL.js â resolver-D4hvuBLP.js} +34 -7
- package/lib/resolver.d-D_30OItP.d.ts +116 -0
- package/lib/resolver.d.ts +3 -81
- package/lib/resolver.js +1 -1
- package/package.json +16 -15
package/README.md
CHANGED
|
@@ -313,6 +313,16 @@ This project is inspired by:
|
|
|
313
313
|
- [`util.parseArgs`](https://nodejs.org/api/util.html#utilparseargsconfig), created by Node.js contributors and [OpenJS Foundation](https://openjsf.org/)
|
|
314
314
|
- [`pkgjs/parseargs`](https://github.com/pkgjs/parseargs), created by Node.js CLI package maintainers and Node.js community.
|
|
315
315
|
|
|
316
|
+
## đ¤ Sponsors
|
|
317
|
+
|
|
318
|
+
The development of Gunish is supported by my OSS sponsors!
|
|
319
|
+
|
|
320
|
+
<p align="center">
|
|
321
|
+
<a href="https://cdn.jsdelivr.net/gh/kazupon/sponsors/sponsors.svg">
|
|
322
|
+
<img src='https://cdn.jsdelivr.net/gh/kazupon/sponsors/sponsors.svg'/>
|
|
323
|
+
</a>
|
|
324
|
+
</p>
|
|
325
|
+
|
|
316
326
|
## ÂŠī¸ License
|
|
317
327
|
|
|
318
328
|
[MIT](http://opensource.org/licenses/MIT)
|
package/lib/index.d.ts
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
import { ParserOptions } from
|
|
2
|
-
|
|
3
|
-
import { ArgOptions, ArgValues } from './resolver.js';
|
|
4
|
-
export { ArgOptionSchema, OptionResolveError, OptionResolveErrorType, resolveArgs } from './resolver.js';
|
|
1
|
+
import { ArgToken, ParserOptions, parseArgs$1 as parseArgs } from "./parser.d-nUUBO6hL.js";
|
|
2
|
+
import { ArgOptionSchema, ArgOptions, ArgValues, OptionResolveError$1 as OptionResolveError, OptionResolveErrorType, ResolveArgsOptions, resolveArgs$1 as resolveArgs } from "./resolver.d-D_30OItP.js";
|
|
5
3
|
|
|
4
|
+
//#region src/parse.d.ts
|
|
6
5
|
/**
|
|
7
6
|
* Parse options for {@link parse} function.
|
|
8
7
|
*/
|
|
9
|
-
interface ParseOptions<O extends ArgOptions> extends ParserOptions {
|
|
8
|
+
interface ParseOptions<O extends ArgOptions> extends ParserOptions, ResolveArgsOptions {
|
|
10
9
|
/**
|
|
11
10
|
* Command line options, about details see {@link ArgOptions}.
|
|
12
11
|
*/
|
|
@@ -25,9 +24,17 @@ type ParsedArgs<T extends ArgOptions> = {
|
|
|
25
24
|
*/
|
|
26
25
|
positionals: string[]
|
|
27
26
|
/**
|
|
27
|
+
* Rest arguments, same as `rest` in {@link resolveArgs}.
|
|
28
|
+
*/
|
|
29
|
+
rest: string[]
|
|
30
|
+
/**
|
|
28
31
|
* Validation errors, same as `errors` in {@link resolveArgs}.
|
|
29
32
|
*/
|
|
30
33
|
error: AggregateError | undefined
|
|
34
|
+
/**
|
|
35
|
+
* Argument tokens, same as `tokens` which is parsed by {@link parseArgs}.
|
|
36
|
+
*/
|
|
37
|
+
tokens: ArgToken[]
|
|
31
38
|
};
|
|
32
39
|
/**
|
|
33
40
|
* Parse command line arguments.
|
|
@@ -42,9 +49,9 @@ type ParsedArgs<T extends ArgOptions> = {
|
|
|
42
49
|
* ```
|
|
43
50
|
* @param args - command line arguments
|
|
44
51
|
* @param options - parse options, about details see {@link ParseOptions}
|
|
45
|
-
* @returns An object that contains the values of the arguments, positional arguments,
|
|
52
|
+
* @returns An object that contains the values of the arguments, positional arguments, {@link AggregateError | validation errors}, and {@link ArgToken | argument tokens}.
|
|
46
53
|
*/
|
|
47
54
|
declare function parse<O extends ArgOptions>(args: string[], options?: ParseOptions<O>): ParsedArgs<O>;
|
|
48
55
|
|
|
49
|
-
|
|
50
|
-
export
|
|
56
|
+
//#endregion
|
|
57
|
+
export { ArgOptionSchema, ArgOptions, ArgToken, ArgValues, OptionResolveError, OptionResolveErrorType, ParseOptions, ParsedArgs, ParserOptions, ResolveArgsOptions, parse, parseArgs, resolveArgs };
|
package/lib/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { parseArgs } from "./parser-DxH6Mf-o.js";
|
|
2
|
-
import { OptionResolveError, resolveArgs } from "./resolver-
|
|
2
|
+
import { OptionResolveError, resolveArgs } from "./resolver-D4hvuBLP.js";
|
|
3
3
|
|
|
4
4
|
//#region src/parse.ts
|
|
5
5
|
const DEFAULT_OPTIONS = {
|
|
@@ -13,9 +13,9 @@ const DEFAULT_OPTIONS = {
|
|
|
13
13
|
}
|
|
14
14
|
};
|
|
15
15
|
function parse(args, options = {}) {
|
|
16
|
-
const { options: argOptions, allowCompatible = false } = options;
|
|
16
|
+
const { options: argOptions, allowCompatible = false, allowNegative = false } = options;
|
|
17
17
|
const tokens = parseArgs(args, { allowCompatible });
|
|
18
|
-
return resolveArgs(argOptions || DEFAULT_OPTIONS, tokens);
|
|
18
|
+
return Object.assign(Object.create(null), resolveArgs(argOptions || DEFAULT_OPTIONS, tokens, { allowNegative }), { tokens });
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
//#endregion
|
|
@@ -0,0 +1,99 @@
|
|
|
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 };
|
package/lib/parser.d.ts
CHANGED
|
@@ -1,83 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
* Entry point of argument parser.
|
|
3
|
-
* @module
|
|
4
|
-
*/
|
|
5
|
-
/**
|
|
6
|
-
* Argument token Kind.
|
|
7
|
-
* - `option`: option token, support short option (e.g. `-x`) and long option (e.g. `--foo`)
|
|
8
|
-
* - `option-terminator`: option terminator (`--`) token, see guideline 10 in https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html
|
|
9
|
-
* - `positional`: positional token
|
|
10
|
-
*/
|
|
11
|
-
type ArgTokenKind = "option" | "option-terminator" | "positional";
|
|
12
|
-
/**
|
|
13
|
-
* Argument token.
|
|
14
|
-
*/
|
|
15
|
-
interface ArgToken {
|
|
16
|
-
/**
|
|
17
|
-
* Argument token kind.
|
|
18
|
-
*/
|
|
19
|
-
kind: ArgTokenKind;
|
|
20
|
-
/**
|
|
21
|
-
* Argument token index, e.g `--foo bar` => `--foo` index is 0, `bar` index is 1.
|
|
22
|
-
*/
|
|
23
|
-
index: number;
|
|
24
|
-
/**
|
|
25
|
-
* Option name, e.g. `--foo` => `foo`, `-x` => `x`.
|
|
26
|
-
*/
|
|
27
|
-
name?: string;
|
|
28
|
-
/**
|
|
29
|
-
* Raw option name, e.g. `--foo` => `--foo`, `-x` => `-x`.
|
|
30
|
-
*/
|
|
31
|
-
rawName?: string;
|
|
32
|
-
/**
|
|
33
|
-
* Option value, e.g. `--foo=bar` => `bar`, `-x=bar` => `bar`.
|
|
34
|
-
* If the `allowCompatible` option is `true`, short option value will be same as Node.js `parseArgs` behavior.
|
|
35
|
-
*/
|
|
36
|
-
value?: string;
|
|
37
|
-
/**
|
|
38
|
-
* Inline value, e.g. `--foo=bar` => `true`, `-x=bar` => `true`.
|
|
39
|
-
*/
|
|
40
|
-
inlineValue?: boolean;
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* Parser Options.
|
|
44
|
-
*/
|
|
45
|
-
interface ParserOptions {
|
|
46
|
-
/**
|
|
47
|
-
* [Node.js parseArgs](https://nodejs.org/api/util.html#parseargs-tokens) tokens compatible mode.
|
|
48
|
-
* @default false
|
|
49
|
-
*/
|
|
50
|
-
allowCompatible?: boolean;
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* Parse command line arguments.
|
|
54
|
-
* @example
|
|
55
|
-
* ```js
|
|
56
|
-
* import { parseArgs } from 'args-tokens' // for Node.js and Bun
|
|
57
|
-
* // import { parseArgs } from 'jsr:@kazupon/args-tokens' // for Deno
|
|
58
|
-
*
|
|
59
|
-
* const tokens = parseArgs(['--foo', 'bar', '-x', '--bar=baz'])
|
|
60
|
-
* // do something with using tokens
|
|
61
|
-
* // ...
|
|
62
|
-
* console.log('tokens:', tokens)
|
|
63
|
-
* ```
|
|
64
|
-
* @param args command line arguments
|
|
65
|
-
* @param options parse options
|
|
66
|
-
* @returns Argument tokens.
|
|
67
|
-
*/
|
|
68
|
-
declare function parseArgs(args: string[], options?: ParserOptions): ArgToken[];
|
|
69
|
-
/**
|
|
70
|
-
* Check if `arg` is a short option (e.g. `-f`).
|
|
71
|
-
* @param arg the argument to check
|
|
72
|
-
* @returns whether `arg` is a short option.
|
|
73
|
-
*/
|
|
74
|
-
declare function isShortOption(arg: string): boolean;
|
|
75
|
-
/**
|
|
76
|
-
* Check if `arg` is a long option prefix (e.g. `--`).
|
|
77
|
-
* @param arg the argument to check
|
|
78
|
-
* @returns whether `arg` is a long option prefix.
|
|
79
|
-
*/
|
|
80
|
-
declare function hasLongOptionPrefix(arg: string): boolean;
|
|
1
|
+
import { ArgToken, ParserOptions, hasLongOptionPrefix$1 as hasLongOptionPrefix, isShortOption$1 as isShortOption, parseArgs$1 as parseArgs } from "./parser.d-nUUBO6hL.js";
|
|
81
2
|
|
|
82
|
-
export { hasLongOptionPrefix, isShortOption, parseArgs };
|
|
83
|
-
export type { ArgToken, ParserOptions };
|
|
3
|
+
export { ArgToken, ParserOptions, hasLongOptionPrefix, isShortOption, parseArgs };
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { hasLongOptionPrefix, isShortOption } from "./parser-DxH6Mf-o.js";
|
|
2
2
|
|
|
3
3
|
//#region src/resolver.ts
|
|
4
|
-
function resolveArgs(options, tokens) {
|
|
4
|
+
function resolveArgs(options, tokens, { optionGrouping = false, allowNegative = false } = {}) {
|
|
5
5
|
const positionals = [];
|
|
6
|
+
const rest = [];
|
|
6
7
|
const longOptionTokens = [];
|
|
7
8
|
const shortOptionTokens = [];
|
|
8
9
|
let currentLongOption;
|
|
@@ -34,19 +35,36 @@ function resolveArgs(options, tokens) {
|
|
|
34
35
|
* analyze phase to resolve value
|
|
35
36
|
* separate tokens into positionals, long and short options, after that resolve values
|
|
36
37
|
*/
|
|
38
|
+
const schemas = Object.values(options);
|
|
39
|
+
let terminated = false;
|
|
37
40
|
for (let i = 0; i < tokens.length; i++) {
|
|
38
41
|
const token = tokens[i];
|
|
39
42
|
if (token.kind === "positional") {
|
|
40
|
-
|
|
43
|
+
if (terminated && token.value) {
|
|
44
|
+
rest.push(token.value);
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
if (currentShortOption) {
|
|
48
|
+
const found = schemas.find((schema) => schema.short === currentShortOption.name && schema.type === "boolean");
|
|
49
|
+
if (found) positionals.push(token.value);
|
|
50
|
+
} else if (currentLongOption) {
|
|
51
|
+
const found = options[currentLongOption.name]?.type === "boolean";
|
|
52
|
+
if (found) positionals.push(token.value);
|
|
53
|
+
} else positionals.push(token.value);
|
|
41
54
|
applyLongOptionValue(token.value);
|
|
42
55
|
applyShortOptionValue(token.value);
|
|
43
56
|
} else if (token.kind === "option") if (token.rawName) {
|
|
44
57
|
if (hasLongOptionPrefix(token.rawName)) {
|
|
58
|
+
applyLongOptionValue();
|
|
45
59
|
if (token.inlineValue) longOptionTokens.push({ ...token });
|
|
46
60
|
else currentLongOption = { ...token };
|
|
47
61
|
applyShortOptionValue();
|
|
48
62
|
} else if (isShortOption(token.rawName)) if (currentShortOption) {
|
|
49
|
-
if (currentShortOption.index === token.index)
|
|
63
|
+
if (currentShortOption.index === token.index) if (optionGrouping) {
|
|
64
|
+
currentShortOption.value = token.value;
|
|
65
|
+
shortOptionTokens.push({ ...currentShortOption });
|
|
66
|
+
currentShortOption = { ...token };
|
|
67
|
+
} else expandableShortOptions.push({ ...token });
|
|
50
68
|
else {
|
|
51
69
|
currentShortOption.value = toShortValue();
|
|
52
70
|
shortOptionTokens.push({ ...currentShortOption });
|
|
@@ -66,6 +84,7 @@ function resolveArgs(options, tokens) {
|
|
|
66
84
|
applyLongOptionValue();
|
|
67
85
|
}
|
|
68
86
|
else {
|
|
87
|
+
if (token.kind === "option-terminator") terminated = true;
|
|
69
88
|
applyLongOptionValue();
|
|
70
89
|
applyShortOptionValue();
|
|
71
90
|
}
|
|
@@ -80,6 +99,9 @@ function resolveArgs(options, tokens) {
|
|
|
80
99
|
*/
|
|
81
100
|
const values = Object.create(null);
|
|
82
101
|
const errors = [];
|
|
102
|
+
function checkTokenName(option, schema, token) {
|
|
103
|
+
return token.name === (schema.type === "boolean" ? allowNegative && token.name?.startsWith("no-") ? `no-${option}` : option : option);
|
|
104
|
+
}
|
|
83
105
|
for (const [option, schema] of Object.entries(options)) {
|
|
84
106
|
if (schema.required) {
|
|
85
107
|
const found = longOptionTokens.find((token) => token.name === option) || schema.short && shortOptionTokens.find((token) => token.name === schema.short);
|
|
@@ -90,7 +112,7 @@ function resolveArgs(options, tokens) {
|
|
|
90
112
|
}
|
|
91
113
|
for (let i = 0; i < longOptionTokens.length; i++) {
|
|
92
114
|
const token = longOptionTokens[i];
|
|
93
|
-
if (option
|
|
115
|
+
if (checkTokenName(option, schema, token) && token.rawName != void 0 && hasLongOptionPrefix(token.rawName)) {
|
|
94
116
|
const invalid = validateRequire(token, option, schema);
|
|
95
117
|
if (invalid) {
|
|
96
118
|
errors.push(invalid);
|
|
@@ -104,7 +126,7 @@ function resolveArgs(options, tokens) {
|
|
|
104
126
|
continue;
|
|
105
127
|
}
|
|
106
128
|
}
|
|
107
|
-
values[option] = resolveOptionValue(token, schema);
|
|
129
|
+
values[option] = resolveOptionValue(token, schema, allowNegative);
|
|
108
130
|
continue;
|
|
109
131
|
}
|
|
110
132
|
}
|
|
@@ -133,6 +155,7 @@ function resolveArgs(options, tokens) {
|
|
|
133
155
|
return {
|
|
134
156
|
values,
|
|
135
157
|
positionals,
|
|
158
|
+
rest,
|
|
136
159
|
error: errors.length > 0 ? new AggregateError(errors) : void 0
|
|
137
160
|
};
|
|
138
161
|
}
|
|
@@ -163,6 +186,10 @@ function validateValue(token, option, schema) {
|
|
|
163
186
|
if (typeof token.value !== "string") return createTypeError(option, schema);
|
|
164
187
|
break;
|
|
165
188
|
}
|
|
189
|
+
case "enum": {
|
|
190
|
+
if (schema.choices && !schema.choices.includes(token.value)) return new OptionResolveError(`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);
|
|
191
|
+
break;
|
|
192
|
+
}
|
|
166
193
|
}
|
|
167
194
|
}
|
|
168
195
|
function isNumeric(str) {
|
|
@@ -171,9 +198,9 @@ function isNumeric(str) {
|
|
|
171
198
|
function createTypeError(option, schema) {
|
|
172
199
|
return new OptionResolveError(`Option '--${option}' ${schema.short ? `or '-${schema.short}' ` : ""}should be '${schema.type}'`, option, "type", schema);
|
|
173
200
|
}
|
|
174
|
-
function resolveOptionValue(token, schema) {
|
|
201
|
+
function resolveOptionValue(token, schema, allowNegative = false) {
|
|
175
202
|
if (token.value) return schema.type === "number" ? +token.value : token.value;
|
|
176
|
-
if (schema.type === "boolean") return true;
|
|
203
|
+
if (schema.type === "boolean") return allowNegative && token.name.startsWith("no-") ? false : true;
|
|
177
204
|
return schema.type === "number" ? +(schema.default || "") : schema.default;
|
|
178
205
|
}
|
|
179
206
|
|
|
@@ -0,0 +1,116 @@
|
|
|
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 };
|
package/lib/resolver.d.ts
CHANGED
|
@@ -1,82 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import "./parser.d-nUUBO6hL.js";
|
|
2
|
+
import { ArgOptionSchema, ArgOptions, ArgValues, ExtractOptionValue, FilterArgs, OptionResolveError$1 as OptionResolveError, OptionResolveErrorType, ResolveArgValues, ResolveArgsOptions, resolveArgs$1 as resolveArgs } from "./resolver.d-D_30OItP.js";
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
* An option schema for an argument.
|
|
5
|
-
* This schema is similar to the schema of the `node:utils`.
|
|
6
|
-
* difference is that:
|
|
7
|
-
* - `multiple` property is not supported
|
|
8
|
-
* - `required` property and `description` property are added
|
|
9
|
-
* - `default` property type, not support multiple types
|
|
10
|
-
*/
|
|
11
|
-
interface ArgOptionSchema {
|
|
12
|
-
/**
|
|
13
|
-
* Type of argument.
|
|
14
|
-
*/
|
|
15
|
-
type: "string" | "boolean" | "number";
|
|
16
|
-
/**
|
|
17
|
-
* A single character alias for the option.
|
|
18
|
-
*/
|
|
19
|
-
short?: string;
|
|
20
|
-
/**
|
|
21
|
-
* A description of the argument.
|
|
22
|
-
*/
|
|
23
|
-
description?: string;
|
|
24
|
-
/**
|
|
25
|
-
* Whether the argument is required or not.
|
|
26
|
-
*/
|
|
27
|
-
required?: true;
|
|
28
|
-
/**
|
|
29
|
-
* The default value of the argument.
|
|
30
|
-
*/
|
|
31
|
-
default?: string | boolean | number;
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* An object that contains {@link ArgOptionSchema | options schema}.
|
|
35
|
-
*/
|
|
36
|
-
interface ArgOptions {
|
|
37
|
-
[option: string]: ArgOptionSchema;
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* An object that contains the values of the arguments.
|
|
41
|
-
*/
|
|
42
|
-
type ArgValues<T> = T extends ArgOptions ? ResolveArgValues<T, { [Option in keyof T] : ExtractOptionValue<T[Option]> }> : {
|
|
43
|
-
[option: string]: string | boolean | number | undefined
|
|
44
|
-
};
|
|
45
|
-
type ExtractOptionValue<O extends ArgOptionSchema> = O["type"] extends "string" ? string : O["type"] extends "boolean" ? boolean : O["type"] extends "number" ? number : string | boolean | number;
|
|
46
|
-
type ResolveArgValues<
|
|
47
|
-
O extends ArgOptions,
|
|
48
|
-
V extends Record<keyof O, unknown>
|
|
49
|
-
> = { -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;
|
|
50
|
-
type FilterArgs<
|
|
51
|
-
O extends ArgOptions,
|
|
52
|
-
V extends Record<keyof O, unknown>,
|
|
53
|
-
K extends keyof ArgOptionSchema
|
|
54
|
-
> = { [Option in keyof O as O[Option][K] extends {} ? Option : never] : V[Option] };
|
|
55
|
-
/**
|
|
56
|
-
* Resolve command line arguments.
|
|
57
|
-
* @param options - An options that contains {@link ArgOptionSchema | options schema}.
|
|
58
|
-
* @param tokens - An array of {@link ArgToken | tokens}.
|
|
59
|
-
* @returns An object that contains the values of the arguments, positional arguments, and {@link AggregateError | validation errors}.
|
|
60
|
-
*/
|
|
61
|
-
declare function resolveArgs<T extends ArgOptions>(options: T, tokens: ArgToken[]): {
|
|
62
|
-
values: ArgValues<T>
|
|
63
|
-
positionals: string[]
|
|
64
|
-
error: AggregateError | undefined
|
|
65
|
-
};
|
|
66
|
-
/**
|
|
67
|
-
* An error type for {@link OptionResolveError}.
|
|
68
|
-
*/
|
|
69
|
-
type OptionResolveErrorType = "type" | "required";
|
|
70
|
-
/**
|
|
71
|
-
* An error that occurs when resolving options.
|
|
72
|
-
* This error is thrown when the option is not valid.
|
|
73
|
-
*/
|
|
74
|
-
declare class OptionResolveError extends Error {
|
|
75
|
-
name: string;
|
|
76
|
-
schema: ArgOptionSchema;
|
|
77
|
-
type: OptionResolveErrorType;
|
|
78
|
-
constructor(message: string, name: string, type: OptionResolveErrorType, schema: ArgOptionSchema);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
export { OptionResolveError, resolveArgs };
|
|
82
|
-
export type { ArgOptionSchema, ArgOptions, ArgValues, OptionResolveErrorType };
|
|
4
|
+
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.15.1",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "kazuya kawaguchi",
|
|
7
7
|
"email": "kawakazu80@gmail.com"
|
|
@@ -65,15 +65,15 @@
|
|
|
65
65
|
}
|
|
66
66
|
},
|
|
67
67
|
"devDependencies": {
|
|
68
|
-
"@eslint/markdown": "^6.
|
|
69
|
-
"@kazupon/eslint-config": "^0.
|
|
68
|
+
"@eslint/markdown": "^6.4.0",
|
|
69
|
+
"@kazupon/eslint-config": "^0.29.0",
|
|
70
70
|
"@kazupon/prettier-config": "^0.1.1",
|
|
71
|
-
"@types/node": "^22.
|
|
72
|
-
"@vitest/eslint-plugin": "^1.1.
|
|
71
|
+
"@types/node": "^22.14.1",
|
|
72
|
+
"@vitest/eslint-plugin": "^1.1.43",
|
|
73
73
|
"bumpp": "^10.1.0",
|
|
74
|
-
"deno": "^2.2.
|
|
75
|
-
"eslint": "^9.
|
|
76
|
-
"eslint-config-prettier": "^10.1.
|
|
74
|
+
"deno": "^2.2.11",
|
|
75
|
+
"eslint": "^9.25.0",
|
|
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",
|
|
@@ -81,15 +81,16 @@
|
|
|
81
81
|
"eslint-plugin-yml": "^1.17.0",
|
|
82
82
|
"gh-changelogen": "^0.2.8",
|
|
83
83
|
"jsr": "^0.13.4",
|
|
84
|
-
"knip": "^5.
|
|
85
|
-
"lint-staged": "^15.5.
|
|
84
|
+
"knip": "^5.50.5",
|
|
85
|
+
"lint-staged": "^15.5.1",
|
|
86
86
|
"mitata": "^1.0.34",
|
|
87
|
-
"pkg-pr-new": "^0.0.
|
|
87
|
+
"pkg-pr-new": "^0.0.43",
|
|
88
88
|
"prettier": "^3.5.3",
|
|
89
|
-
"tsdown": "^0.
|
|
90
|
-
"
|
|
91
|
-
"typescript
|
|
92
|
-
"
|
|
89
|
+
"tsdown": "^0.9.1",
|
|
90
|
+
"tsdown-jsr-exports-lint": "^0.1.4",
|
|
91
|
+
"typescript": "^5.8.3",
|
|
92
|
+
"typescript-eslint": "^8.30.1",
|
|
93
|
+
"vitest": "^3.1.1"
|
|
93
94
|
},
|
|
94
95
|
"prettier": "@kazupon/prettier-config",
|
|
95
96
|
"lint-staged": {
|