args-tokens 0.21.0 → 0.22.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 +37 -37
- package/lib/parser-Cbxholql.d.ts +91 -0
- package/lib/parser.d.ts +1 -1
- package/lib/resolver-BoS-UnqX.d.ts +173 -0
- package/lib/resolver.d.ts +3 -3
- package/lib/utils.d.ts +9 -9
- package/package.json +3 -3
- package/lib/parser-FiQIAw-2.d.ts +0 -91
- package/lib/resolver-C87QZIMT.d.ts +0 -175
package/lib/index.d.ts
CHANGED
|
@@ -1,62 +1,62 @@
|
|
|
1
|
-
import { ArgToken, ParserOptions, parseArgs$1 as parseArgs } from "./parser-
|
|
2
|
-
import { ArgResolveError$1 as ArgResolveError, ArgResolveErrorType, ArgSchema, ArgValues, Args,
|
|
1
|
+
import { ArgToken, ParserOptions, parseArgs$1 as parseArgs } from "./parser-Cbxholql.js";
|
|
2
|
+
import { ArgExplicitlyProvided, ArgResolveError$1 as ArgResolveError, ArgResolveErrorType, ArgSchema, ArgValues, Args, ResolveArgs, resolveArgs$1 as resolveArgs } from "./resolver-BoS-UnqX.js";
|
|
3
3
|
|
|
4
4
|
//#region src/parse.d.ts
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
|
-
* Parse options for {@link parse} function.
|
|
8
|
-
*/
|
|
7
|
+
* Parse options for {@link parse} function.
|
|
8
|
+
*/
|
|
9
9
|
interface ParseOptions<A extends Args> extends ParserOptions, ResolveArgs {
|
|
10
10
|
/**
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
* Command line arguments, about details see {@link Args}.
|
|
12
|
+
*/
|
|
13
13
|
args?: A;
|
|
14
14
|
}
|
|
15
15
|
/**
|
|
16
|
-
* Parsed command line arguments.
|
|
17
|
-
*/
|
|
16
|
+
* Parsed command line arguments.
|
|
17
|
+
*/
|
|
18
18
|
type ParsedArgs<A extends Args> = {
|
|
19
19
|
/**
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
* Parsed values, same as `values` in {@link resolveArgs}.
|
|
21
|
+
*/
|
|
22
22
|
values: ArgValues<A>;
|
|
23
23
|
/**
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
* Positional arguments, same as `positionals` in {@link resolveArgs}.
|
|
25
|
+
*/
|
|
26
26
|
positionals: string[];
|
|
27
27
|
/**
|
|
28
|
-
|
|
29
|
-
|
|
28
|
+
* Rest arguments, same as `rest` in {@link resolveArgs}.
|
|
29
|
+
*/
|
|
30
30
|
rest: string[];
|
|
31
31
|
/**
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
* Validation errors, same as `errors` in {@link resolveArgs}.
|
|
33
|
+
*/
|
|
34
34
|
error: AggregateError | undefined;
|
|
35
35
|
/**
|
|
36
|
-
|
|
37
|
-
|
|
36
|
+
* Argument tokens, same as `tokens` which is parsed by {@link parseArgs}.
|
|
37
|
+
*/
|
|
38
38
|
tokens: ArgToken[];
|
|
39
39
|
/**
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
explicit:
|
|
40
|
+
* Explicit provision status, same as `explicit` in {@link resolveArgs}.
|
|
41
|
+
* Indicates which arguments were explicitly provided vs using default values.
|
|
42
|
+
*/
|
|
43
|
+
explicit: ArgExplicitlyProvided<A>;
|
|
44
44
|
};
|
|
45
45
|
/**
|
|
46
|
-
* Parse command line arguments.
|
|
47
|
-
* This function is a convenient API, that is used {@link parseArgs} and {@link resolveArgs} in internal.
|
|
48
|
-
* @example
|
|
49
|
-
* ```js
|
|
50
|
-
* import { parse } from 'args-tokens'
|
|
51
|
-
*
|
|
52
|
-
* const { values, positionals } = parse(process.argv.slice(2))
|
|
53
|
-
* console.log('values', values)
|
|
54
|
-
* console.log('positionals', positionals)
|
|
55
|
-
* ```
|
|
56
|
-
* @param args - command line arguments
|
|
57
|
-
* @param options - parse options, about details see {@link ParseOptions}
|
|
58
|
-
* @returns An object that contains the values of the arguments, positional arguments, {@link AggregateError | validation errors}, and {@link ArgToken | argument tokens}.
|
|
59
|
-
*/
|
|
46
|
+
* Parse command line arguments.
|
|
47
|
+
* This function is a convenient API, that is used {@link parseArgs} and {@link resolveArgs} in internal.
|
|
48
|
+
* @example
|
|
49
|
+
* ```js
|
|
50
|
+
* import { parse } from 'args-tokens'
|
|
51
|
+
*
|
|
52
|
+
* const { values, positionals } = parse(process.argv.slice(2))
|
|
53
|
+
* console.log('values', values)
|
|
54
|
+
* console.log('positionals', positionals)
|
|
55
|
+
* ```
|
|
56
|
+
* @param args - command line arguments
|
|
57
|
+
* @param options - parse options, about details see {@link ParseOptions}
|
|
58
|
+
* @returns An object that contains the values of the arguments, positional arguments, {@link AggregateError | validation errors}, and {@link ArgToken | argument tokens}.
|
|
59
|
+
*/
|
|
60
60
|
declare function parse<A extends Args>(args: string[], options?: ParseOptions<A>): ParsedArgs<A>;
|
|
61
61
|
//#endregion
|
|
62
|
-
export { ArgResolveError, ArgResolveErrorType, ArgSchema, ArgToken, ArgValues, Args, ParseOptions, ParsedArgs, ParserOptions, ResolveArgs, parse, parseArgs, resolveArgs };
|
|
62
|
+
export { ArgExplicitlyProvided, ArgResolveError, ArgResolveErrorType, ArgSchema, ArgToken, ArgValues, Args, ParseOptions, ParsedArgs, ParserOptions, ResolveArgs, parse, parseArgs, resolveArgs };
|
|
@@ -0,0 +1,91 @@
|
|
|
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
|
+
*/
|
|
20
|
+
type ArgTokenKind = 'option' | 'option-terminator' | 'positional';
|
|
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
|
+
/**
|
|
52
|
+
* Parser Options.
|
|
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
|
+
/**
|
|
62
|
+
* Parse command line arguments.
|
|
63
|
+
* @example
|
|
64
|
+
* ```js
|
|
65
|
+
* import { parseArgs } from 'args-tokens' // for Node.js and Bun
|
|
66
|
+
* // import { parseArgs } from 'jsr:@kazupon/args-tokens' // for Deno
|
|
67
|
+
*
|
|
68
|
+
* const tokens = parseArgs(['--foo', 'bar', '-x', '--bar=baz'])
|
|
69
|
+
* // do something with using tokens
|
|
70
|
+
* // ...
|
|
71
|
+
* console.log('tokens:', tokens)
|
|
72
|
+
* ```
|
|
73
|
+
* @param args command line arguments
|
|
74
|
+
* @param options parse options
|
|
75
|
+
* @returns Argument tokens.
|
|
76
|
+
*/
|
|
77
|
+
declare function parseArgs(args: string[], options?: ParserOptions): ArgToken[];
|
|
78
|
+
/**
|
|
79
|
+
* Check if `arg` is a short option (e.g. `-f`).
|
|
80
|
+
* @param arg the argument to check
|
|
81
|
+
* @returns whether `arg` is a short option.
|
|
82
|
+
*/
|
|
83
|
+
declare function isShortOption(arg: string): boolean;
|
|
84
|
+
/**
|
|
85
|
+
* Check if `arg` is a long option prefix (e.g. `--`).
|
|
86
|
+
* @param arg the argument to check
|
|
87
|
+
* @returns whether `arg` is a long option prefix.
|
|
88
|
+
*/
|
|
89
|
+
declare function hasLongOptionPrefix(arg: string): boolean;
|
|
90
|
+
//#endregion
|
|
91
|
+
export { ArgToken, ParserOptions, hasLongOptionPrefix as hasLongOptionPrefix$1, isShortOption as isShortOption$1, parseArgs as parseArgs$1 };
|
package/lib/parser.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { ArgToken, ParserOptions, hasLongOptionPrefix$1 as hasLongOptionPrefix, isShortOption$1 as isShortOption, parseArgs$1 as parseArgs } from "./parser-
|
|
1
|
+
import { ArgToken, ParserOptions, hasLongOptionPrefix$1 as hasLongOptionPrefix, isShortOption$1 as isShortOption, parseArgs$1 as parseArgs } from "./parser-Cbxholql.js";
|
|
2
2
|
export { ArgToken, ParserOptions, hasLongOptionPrefix, isShortOption, parseArgs };
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { ArgToken } from "./parser-Cbxholql.js";
|
|
2
|
+
|
|
3
|
+
//#region src/resolver.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* An argument schema
|
|
7
|
+
* This schema is similar to the schema of the `node:utils`.
|
|
8
|
+
* difference is that:
|
|
9
|
+
* - `required` property and `description` property are added
|
|
10
|
+
* - `type` is not only 'string' and 'boolean', but also 'number', 'enum', 'positional', 'custom' too.
|
|
11
|
+
* - `default` property type, not support multiple types
|
|
12
|
+
*/
|
|
13
|
+
interface ArgSchema {
|
|
14
|
+
/**
|
|
15
|
+
* Type of argument.
|
|
16
|
+
*/
|
|
17
|
+
type: 'string' | 'boolean' | 'number' | 'enum' | 'positional' | 'custom';
|
|
18
|
+
/**
|
|
19
|
+
* A single character alias for the argument.
|
|
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 argument allow multiple values or not.
|
|
32
|
+
*/
|
|
33
|
+
multiple?: true;
|
|
34
|
+
/**
|
|
35
|
+
* Whether the negatable option for `boolean` type
|
|
36
|
+
*/
|
|
37
|
+
negatable?: boolean;
|
|
38
|
+
/**
|
|
39
|
+
* The allowed values of the argument, and string only. This property is only used when the type is 'enum'.
|
|
40
|
+
*/
|
|
41
|
+
choices?: string[] | readonly string[];
|
|
42
|
+
/**
|
|
43
|
+
* The default value of the argument.
|
|
44
|
+
* if the type is 'enum', the default value must be one of the allowed values.
|
|
45
|
+
*/
|
|
46
|
+
default?: string | boolean | number;
|
|
47
|
+
/**
|
|
48
|
+
* Whether to convert the argument name to kebab-case.
|
|
49
|
+
*/
|
|
50
|
+
toKebab?: true;
|
|
51
|
+
/**
|
|
52
|
+
* A function to parse the value of the argument. if the type is 'custom', this function is required.
|
|
53
|
+
* If argument value will be invalid, this function have to throw an error.
|
|
54
|
+
* @param value
|
|
55
|
+
* @returns Parsed value
|
|
56
|
+
* @throws An Error, If the value is invalid. Error type should be `Error` or extends it
|
|
57
|
+
*/
|
|
58
|
+
parse?: (value: string) => any;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* An object that contains {@link ArgSchema | argument schema}.
|
|
62
|
+
*/
|
|
63
|
+
interface Args {
|
|
64
|
+
[option: string]: ArgSchema;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* An object that contains the values of the arguments.
|
|
68
|
+
*/
|
|
69
|
+
type ArgValues<T> = T extends Args ? ResolveArgValues<T, { [Arg in keyof T]: ExtractOptionValue<T[Arg]> }> : {
|
|
70
|
+
[option: string]: string | boolean | number | (string | boolean | number)[] | undefined;
|
|
71
|
+
};
|
|
72
|
+
type IsFunction<T> = T extends ((...args: any[]) => any) ? true : false;
|
|
73
|
+
/**
|
|
74
|
+
* @internal
|
|
75
|
+
*/
|
|
76
|
+
type ExtractOptionValue<A extends ArgSchema> = A['type'] extends 'string' ? ResolveOptionValue<A, string> : A['type'] extends 'boolean' ? ResolveOptionValue<A, boolean> : A['type'] extends 'number' ? ResolveOptionValue<A, number> : A['type'] extends 'positional' ? ResolveOptionValue<A, string> : A['type'] extends 'enum' ? A['choices'] extends string[] | readonly string[] ? ResolveOptionValue<A, A['choices'][number]> : never : A['type'] extends 'custom' ? IsFunction<A['parse']> extends true ? ResolveOptionValue<A, ReturnType<NonNullable<A['parse']>>> : never : ResolveOptionValue<A, string | boolean | number>;
|
|
77
|
+
type ResolveOptionValue<A extends ArgSchema, T> = A['multiple'] extends true ? T[] : T;
|
|
78
|
+
/**
|
|
79
|
+
* @internal
|
|
80
|
+
*/
|
|
81
|
+
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'> & FilterPositionalArgs<A, V> extends infer P ? { [K in keyof P]: P[K] } : never;
|
|
82
|
+
/**
|
|
83
|
+
* @internal
|
|
84
|
+
*/
|
|
85
|
+
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] };
|
|
86
|
+
/**
|
|
87
|
+
* @internal
|
|
88
|
+
*/
|
|
89
|
+
type FilterPositionalArgs<A extends Args, V extends Record<keyof A, unknown>> = { [Arg in keyof A as A[Arg]['type'] extends 'positional' ? Arg : never]: V[Arg] };
|
|
90
|
+
/**
|
|
91
|
+
* An arguments for {@link resolveArgs | resolve arguments}.
|
|
92
|
+
*/
|
|
93
|
+
interface ResolveArgs {
|
|
94
|
+
/**
|
|
95
|
+
* Whether to group short arguments.
|
|
96
|
+
* @default false
|
|
97
|
+
* @see guideline 5 in https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap12.html
|
|
98
|
+
*/
|
|
99
|
+
shortGrouping?: boolean;
|
|
100
|
+
/**
|
|
101
|
+
* Skip positional arguments index.
|
|
102
|
+
* @default -1
|
|
103
|
+
*/
|
|
104
|
+
skipPositional?: number;
|
|
105
|
+
/**
|
|
106
|
+
* Whether to convert the argument name to kebab-case. This option is applied to all arguments as `toKebab: true`, if set to `true`.
|
|
107
|
+
* @default false
|
|
108
|
+
*/
|
|
109
|
+
toKebab?: boolean;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Tracks which arguments were explicitly provided by the user.
|
|
113
|
+
*
|
|
114
|
+
* Each property indicates whether the corresponding argument was explicitly
|
|
115
|
+
* provided (true) or is using a default value or not provided (false).
|
|
116
|
+
*/
|
|
117
|
+
type ArgExplicitlyProvided<A extends Args> = { [K in keyof A]: boolean };
|
|
118
|
+
/**
|
|
119
|
+
* Resolve command line arguments.
|
|
120
|
+
* @param args - An arguments that contains {@link ArgSchema | arguments schema}.
|
|
121
|
+
* @param tokens - An array of {@link ArgToken | tokens}.
|
|
122
|
+
* @param resolveArgs - An arguments that contains {@link ResolveArgs | resolve arguments}.
|
|
123
|
+
* @returns An object that contains the values of the arguments, positional arguments, rest arguments, {@link AggregateError | validation errors}, and explicit provision status.
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
* ```typescript
|
|
127
|
+
* // passed tokens: --port 3000
|
|
128
|
+
*
|
|
129
|
+
* const { values, explicit } = resolveArgs({
|
|
130
|
+
* port: {
|
|
131
|
+
* type: 'number',
|
|
132
|
+
* default: 8080
|
|
133
|
+
* },
|
|
134
|
+
* host: {
|
|
135
|
+
* type: 'string',
|
|
136
|
+
* default: 'localhost'
|
|
137
|
+
* }
|
|
138
|
+
* }, parsedTokens)
|
|
139
|
+
*
|
|
140
|
+
* values.port // 3000
|
|
141
|
+
* values.host // 'localhost'
|
|
142
|
+
*
|
|
143
|
+
* explicit.port // true (explicitly provided)
|
|
144
|
+
* explicit.host // false (not provided, fallback to default)
|
|
145
|
+
* ```
|
|
146
|
+
*/
|
|
147
|
+
declare function resolveArgs<A extends Args>(args: A, tokens: ArgToken[], {
|
|
148
|
+
shortGrouping,
|
|
149
|
+
skipPositional,
|
|
150
|
+
toKebab
|
|
151
|
+
}?: ResolveArgs): {
|
|
152
|
+
values: ArgValues<A>;
|
|
153
|
+
positionals: string[];
|
|
154
|
+
rest: string[];
|
|
155
|
+
error: AggregateError | undefined;
|
|
156
|
+
explicit: ArgExplicitlyProvided<A>;
|
|
157
|
+
};
|
|
158
|
+
/**
|
|
159
|
+
* An error type for {@link ArgResolveError}.
|
|
160
|
+
*/
|
|
161
|
+
type ArgResolveErrorType = 'type' | 'required';
|
|
162
|
+
/**
|
|
163
|
+
* An error that occurs when resolving arguments.
|
|
164
|
+
* This error is thrown when the argument is not valid.
|
|
165
|
+
*/
|
|
166
|
+
declare class ArgResolveError extends Error {
|
|
167
|
+
name: string;
|
|
168
|
+
schema: ArgSchema;
|
|
169
|
+
type: ArgResolveErrorType;
|
|
170
|
+
constructor(message: string, name: string, type: ArgResolveErrorType, schema: ArgSchema);
|
|
171
|
+
}
|
|
172
|
+
//#endregion
|
|
173
|
+
export { ArgExplicitlyProvided, ArgResolveError as ArgResolveError$1, ArgResolveErrorType, ArgSchema, ArgValues, Args, ExtractOptionValue, FilterArgs, FilterPositionalArgs, ResolveArgValues, ResolveArgs, resolveArgs as resolveArgs$1 };
|
package/lib/resolver.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import "./parser-
|
|
2
|
-
import { ArgResolveError$1 as ArgResolveError, ArgResolveErrorType, ArgSchema, ArgValues, Args,
|
|
3
|
-
export { ArgResolveError, ArgResolveErrorType, ArgSchema, ArgValues, Args,
|
|
1
|
+
import "./parser-Cbxholql.js";
|
|
2
|
+
import { ArgExplicitlyProvided, ArgResolveError$1 as ArgResolveError, ArgResolveErrorType, ArgSchema, ArgValues, Args, ExtractOptionValue, FilterArgs, FilterPositionalArgs, ResolveArgValues, ResolveArgs, resolveArgs$1 as resolveArgs } from "./resolver-BoS-UnqX.js";
|
|
3
|
+
export { ArgExplicitlyProvided, ArgResolveError, ArgResolveErrorType, ArgSchema, ArgValues, Args, ExtractOptionValue, FilterArgs, FilterPositionalArgs, ResolveArgValues, ResolveArgs, resolveArgs };
|
package/lib/utils.d.ts
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
//#region src/utils.d.ts
|
|
2
2
|
/**
|
|
3
|
-
* Entry point of utils.
|
|
4
|
-
*
|
|
5
|
-
* Note that this entry point is used by gunshi to import utility functions.
|
|
6
|
-
*
|
|
7
|
-
* @module
|
|
8
|
-
*/
|
|
3
|
+
* Entry point of utils.
|
|
4
|
+
*
|
|
5
|
+
* Note that this entry point is used by gunshi to import utility functions.
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
9
|
/**
|
|
10
|
-
* @author kazuya kawaguchi (a.k.a. kazupon)
|
|
11
|
-
* @license MIT
|
|
12
|
-
*/
|
|
10
|
+
* @author kazuya kawaguchi (a.k.a. kazupon)
|
|
11
|
+
* @license MIT
|
|
12
|
+
*/
|
|
13
13
|
declare function kebabnize(str: string): string;
|
|
14
14
|
//#endregion
|
|
15
15
|
export { kebabnize };
|
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.22.0",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "kazuya kawaguchi",
|
|
7
7
|
"email": "kawakazu80@gmail.com"
|
|
@@ -75,7 +75,7 @@
|
|
|
75
75
|
"@kazupon/eslint-config": "^0.31.1",
|
|
76
76
|
"@kazupon/prettier-config": "^0.1.1",
|
|
77
77
|
"@types/node": "^22.16.0",
|
|
78
|
-
"@typescript/native-preview": "7.0.0-dev.
|
|
78
|
+
"@typescript/native-preview": "7.0.0-dev.20250710.1",
|
|
79
79
|
"@vitest/eslint-plugin": "^1.3.4",
|
|
80
80
|
"bumpp": "^10.2.0",
|
|
81
81
|
"deno": "^2.4.1",
|
|
@@ -98,7 +98,7 @@
|
|
|
98
98
|
"typescript": "^5.8.3",
|
|
99
99
|
"typescript-eslint": "^8.36.0",
|
|
100
100
|
"vitest": "^3.2.4",
|
|
101
|
-
"zod": "^
|
|
101
|
+
"zod": "^4.0.0"
|
|
102
102
|
},
|
|
103
103
|
"prettier": "@kazupon/prettier-config",
|
|
104
104
|
"lint-staged": {
|
package/lib/parser-FiQIAw-2.d.ts
DELETED
|
@@ -1,91 +0,0 @@
|
|
|
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
|
-
*/
|
|
20
|
-
type ArgTokenKind = "option" | "option-terminator" | "positional";
|
|
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
|
-
/**
|
|
52
|
-
* Parser Options.
|
|
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
|
-
/**
|
|
62
|
-
* Parse command line arguments.
|
|
63
|
-
* @example
|
|
64
|
-
* ```js
|
|
65
|
-
* import { parseArgs } from 'args-tokens' // for Node.js and Bun
|
|
66
|
-
* // import { parseArgs } from 'jsr:@kazupon/args-tokens' // for Deno
|
|
67
|
-
*
|
|
68
|
-
* const tokens = parseArgs(['--foo', 'bar', '-x', '--bar=baz'])
|
|
69
|
-
* // do something with using tokens
|
|
70
|
-
* // ...
|
|
71
|
-
* console.log('tokens:', tokens)
|
|
72
|
-
* ```
|
|
73
|
-
* @param args command line arguments
|
|
74
|
-
* @param options parse options
|
|
75
|
-
* @returns Argument tokens.
|
|
76
|
-
*/
|
|
77
|
-
declare function parseArgs(args: string[], options?: ParserOptions): ArgToken[];
|
|
78
|
-
/**
|
|
79
|
-
* Check if `arg` is a short option (e.g. `-f`).
|
|
80
|
-
* @param arg the argument to check
|
|
81
|
-
* @returns whether `arg` is a short option.
|
|
82
|
-
*/
|
|
83
|
-
declare function isShortOption(arg: string): boolean;
|
|
84
|
-
/**
|
|
85
|
-
* Check if `arg` is a long option prefix (e.g. `--`).
|
|
86
|
-
* @param arg the argument to check
|
|
87
|
-
* @returns whether `arg` is a long option prefix.
|
|
88
|
-
*/
|
|
89
|
-
declare function hasLongOptionPrefix(arg: string): boolean;
|
|
90
|
-
//#endregion
|
|
91
|
-
export { ArgToken, ParserOptions, hasLongOptionPrefix as hasLongOptionPrefix$1, isShortOption as isShortOption$1, parseArgs as parseArgs$1 };
|
|
@@ -1,175 +0,0 @@
|
|
|
1
|
-
import { ArgToken } from "./parser-FiQIAw-2.js";
|
|
2
|
-
|
|
3
|
-
//#region src/resolver.d.ts
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* An argument schema
|
|
7
|
-
* This schema is similar to the schema of the `node:utils`.
|
|
8
|
-
* difference is that:
|
|
9
|
-
* - `required` property and `description` property are added
|
|
10
|
-
* - `type` is not only 'string' and 'boolean', but also 'number', 'enum', 'positional', 'custom' too.
|
|
11
|
-
* - `default` property type, not support multiple types
|
|
12
|
-
*/
|
|
13
|
-
interface ArgSchema {
|
|
14
|
-
/**
|
|
15
|
-
* Type of argument.
|
|
16
|
-
*/
|
|
17
|
-
type: "string" | "boolean" | "number" | "enum" | "positional" | "custom";
|
|
18
|
-
/**
|
|
19
|
-
* A single character alias for the argument.
|
|
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 argument allow multiple values or not.
|
|
32
|
-
*/
|
|
33
|
-
multiple?: true;
|
|
34
|
-
/**
|
|
35
|
-
* Whether the negatable option for `boolean` type
|
|
36
|
-
*/
|
|
37
|
-
negatable?: boolean;
|
|
38
|
-
/**
|
|
39
|
-
* The allowed values of the argument, and string only. This property is only used when the type is 'enum'.
|
|
40
|
-
*/
|
|
41
|
-
choices?: string[] | readonly string[];
|
|
42
|
-
/**
|
|
43
|
-
* The default value of the argument.
|
|
44
|
-
* if the type is 'enum', the default value must be one of the allowed values.
|
|
45
|
-
*/
|
|
46
|
-
default?: string | boolean | number;
|
|
47
|
-
/**
|
|
48
|
-
* Whether to convert the argument name to kebab-case.
|
|
49
|
-
*/
|
|
50
|
-
toKebab?: true;
|
|
51
|
-
/**
|
|
52
|
-
* A function to parse the value of the argument. if the type is 'custom', this function is required.
|
|
53
|
-
* If argument value will be invalid, this function have to throw an error.
|
|
54
|
-
* @param value
|
|
55
|
-
* @returns Parsed value
|
|
56
|
-
* @throws An Error, If the value is invalid. Error type should be `Error` or extends it
|
|
57
|
-
*/
|
|
58
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
59
|
-
parse?: (value: string) => any;
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* An object that contains {@link ArgSchema | argument schema}.
|
|
63
|
-
*/
|
|
64
|
-
interface Args {
|
|
65
|
-
[option: string]: ArgSchema;
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* An object that contains the values of the arguments.
|
|
69
|
-
*/
|
|
70
|
-
type ArgValues<T> = T extends Args ? ResolveArgValues<T, { [Arg in keyof T]: ExtractOptionValue<T[Arg]> }> : {
|
|
71
|
-
[option: string]: string | boolean | number | (string | boolean | number)[] | undefined;
|
|
72
|
-
};
|
|
73
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
74
|
-
type IsFunction<T> = T extends ((...args: any[]) => any) ? true : false;
|
|
75
|
-
/**
|
|
76
|
-
* @internal
|
|
77
|
-
*/
|
|
78
|
-
type ExtractOptionValue<A extends ArgSchema> = A["type"] extends "string" ? ResolveOptionValue<A, string> : A["type"] extends "boolean" ? ResolveOptionValue<A, boolean> : A["type"] extends "number" ? ResolveOptionValue<A, number> : A["type"] extends "positional" ? ResolveOptionValue<A, string> : A["type"] extends "enum" ? A["choices"] extends string[] | readonly string[] ? ResolveOptionValue<A, A["choices"][number]> : never : A["type"] extends "custom" ? IsFunction<A["parse"]> extends true ? ResolveOptionValue<A, ReturnType<NonNullable<A["parse"]>>> : never : ResolveOptionValue<A, string | boolean | number>;
|
|
79
|
-
type ResolveOptionValue<A extends ArgSchema, T> = A["multiple"] extends true ? T[] : T;
|
|
80
|
-
/**
|
|
81
|
-
* @internal
|
|
82
|
-
*/
|
|
83
|
-
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"> & FilterPositionalArgs<A, V> extends infer P ? { [K in keyof P]: P[K] } : never;
|
|
84
|
-
/**
|
|
85
|
-
* @internal
|
|
86
|
-
*/
|
|
87
|
-
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] };
|
|
88
|
-
/**
|
|
89
|
-
* @internal
|
|
90
|
-
*/
|
|
91
|
-
type FilterPositionalArgs<A extends Args, V extends Record<keyof A, unknown>> = { [Arg in keyof A as A[Arg]["type"] extends "positional" ? Arg : never]: V[Arg] };
|
|
92
|
-
/**
|
|
93
|
-
* An arguments for {@link resolveArgs | resolve arguments}.
|
|
94
|
-
*/
|
|
95
|
-
interface ResolveArgs {
|
|
96
|
-
/**
|
|
97
|
-
* Whether to group short arguments.
|
|
98
|
-
* @default false
|
|
99
|
-
* @see guideline 5 in https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap12.html
|
|
100
|
-
*/
|
|
101
|
-
shortGrouping?: boolean;
|
|
102
|
-
/**
|
|
103
|
-
* Skip positional arguments index.
|
|
104
|
-
* @default -1
|
|
105
|
-
*/
|
|
106
|
-
skipPositional?: number;
|
|
107
|
-
/**
|
|
108
|
-
* Whether to convert the argument name to kebab-case. This option is applied to all arguments as `toKebab: true`, if set to `true`.
|
|
109
|
-
* @default false
|
|
110
|
-
*/
|
|
111
|
-
toKebab?: boolean;
|
|
112
|
-
}
|
|
113
|
-
/**
|
|
114
|
-
* Tracks which arguments were explicitly provided by the user.
|
|
115
|
-
*
|
|
116
|
-
* Each property indicates whether the corresponding argument was explicitly
|
|
117
|
-
* provided (true) or is using a default value or not provided (false).
|
|
118
|
-
*/
|
|
119
|
-
type ExplicitlyProvided<A extends Args> = { readonly [K in keyof A]: boolean };
|
|
120
|
-
/**
|
|
121
|
-
* Resolve command line arguments.
|
|
122
|
-
* @param args - An arguments that contains {@link ArgSchema | arguments schema}.
|
|
123
|
-
* @param tokens - An array of {@link ArgToken | tokens}.
|
|
124
|
-
* @param resolveArgs - An arguments that contains {@link ResolveArgs | resolve arguments}.
|
|
125
|
-
* @returns An object that contains the values of the arguments, positional arguments, rest arguments, {@link AggregateError | validation errors}, and explicit provision status.
|
|
126
|
-
*
|
|
127
|
-
* @example
|
|
128
|
-
* ```typescript
|
|
129
|
-
* // passed tokens: --port 3000
|
|
130
|
-
*
|
|
131
|
-
* const { values, explicit } = resolveArgs({
|
|
132
|
-
* port: {
|
|
133
|
-
* type: 'number',
|
|
134
|
-
* default: 8080
|
|
135
|
-
* },
|
|
136
|
-
* host: {
|
|
137
|
-
* type: 'string',
|
|
138
|
-
* default: 'localhost'
|
|
139
|
-
* }
|
|
140
|
-
* }, parsedTokens)
|
|
141
|
-
*
|
|
142
|
-
* values.port // 3000
|
|
143
|
-
* values.host // 'localhost'
|
|
144
|
-
*
|
|
145
|
-
* explicit.port // true (explicitly provided)
|
|
146
|
-
* explicit.host // false (not provided, fallback to default)
|
|
147
|
-
* ```
|
|
148
|
-
*/
|
|
149
|
-
declare function resolveArgs<A extends Args>(args: A, tokens: ArgToken[], {
|
|
150
|
-
shortGrouping,
|
|
151
|
-
skipPositional,
|
|
152
|
-
toKebab
|
|
153
|
-
}?: ResolveArgs): {
|
|
154
|
-
values: ArgValues<A>;
|
|
155
|
-
positionals: string[];
|
|
156
|
-
rest: string[];
|
|
157
|
-
error: AggregateError | undefined;
|
|
158
|
-
explicit: ExplicitlyProvided<A>;
|
|
159
|
-
};
|
|
160
|
-
/**
|
|
161
|
-
* An error type for {@link ArgResolveError}.
|
|
162
|
-
*/
|
|
163
|
-
type ArgResolveErrorType = "type" | "required";
|
|
164
|
-
/**
|
|
165
|
-
* An error that occurs when resolving arguments.
|
|
166
|
-
* This error is thrown when the argument is not valid.
|
|
167
|
-
*/
|
|
168
|
-
declare class ArgResolveError extends Error {
|
|
169
|
-
name: string;
|
|
170
|
-
schema: ArgSchema;
|
|
171
|
-
type: ArgResolveErrorType;
|
|
172
|
-
constructor(message: string, name: string, type: ArgResolveErrorType, schema: ArgSchema);
|
|
173
|
-
}
|
|
174
|
-
//#endregion
|
|
175
|
-
export { ArgResolveError as ArgResolveError$1, ArgResolveErrorType, ArgSchema, ArgValues, Args, ExplicitlyProvided, ExtractOptionValue, FilterArgs, FilterPositionalArgs, ResolveArgValues, ResolveArgs, resolveArgs as resolveArgs$1 };
|