@gunshi/definition 0.27.0-alpha.6 → 0.27.0-alpha.7
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 +153 -91
- package/lib/index.js +2 -0
- package/package.json +2 -2
package/lib/index.d.ts
CHANGED
|
@@ -1,151 +1,186 @@
|
|
|
1
|
-
//#region ../../node_modules/.pnpm/args-tokens@0.
|
|
1
|
+
//#region ../../node_modules/.pnpm/args-tokens@0.22.0/node_modules/args-tokens/lib/parser-Cbxholql.d.ts
|
|
2
2
|
//#region src/parser.d.ts
|
|
3
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
|
-
type ArgTokenKind =
|
|
22
|
-
/**
|
|
23
|
-
* Argument token.
|
|
24
|
-
*/
|
|
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
|
+
type ArgTokenKind = 'option' | 'option-terminator' | 'positional';
|
|
22
|
+
/**
|
|
23
|
+
* Argument token.
|
|
24
|
+
*/
|
|
25
25
|
interface ArgToken {
|
|
26
26
|
/**
|
|
27
|
-
|
|
28
|
-
|
|
27
|
+
* Argument token kind.
|
|
28
|
+
*/
|
|
29
29
|
kind: ArgTokenKind;
|
|
30
30
|
/**
|
|
31
|
-
|
|
32
|
-
|
|
31
|
+
* Argument token index, e.g `--foo bar` => `--foo` index is 0, `bar` index is 1.
|
|
32
|
+
*/
|
|
33
33
|
index: number;
|
|
34
34
|
/**
|
|
35
|
-
|
|
36
|
-
|
|
35
|
+
* Option name, e.g. `--foo` => `foo`, `-x` => `x`.
|
|
36
|
+
*/
|
|
37
37
|
name?: string;
|
|
38
38
|
/**
|
|
39
|
-
|
|
40
|
-
|
|
39
|
+
* Raw option name, e.g. `--foo` => `--foo`, `-x` => `-x`.
|
|
40
|
+
*/
|
|
41
41
|
rawName?: string;
|
|
42
42
|
/**
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
43
|
+
* Option value, e.g. `--foo=bar` => `bar`, `-x=bar` => `bar`.
|
|
44
|
+
* If the `allowCompatible` option is `true`, short option value will be same as Node.js `parseArgs` behavior.
|
|
45
|
+
*/
|
|
46
46
|
value?: string;
|
|
47
47
|
/**
|
|
48
|
-
|
|
49
|
-
|
|
48
|
+
* Inline value, e.g. `--foo=bar` => `true`, `-x=bar` => `true`.
|
|
49
|
+
*/
|
|
50
50
|
inlineValue?: boolean;
|
|
51
51
|
}
|
|
52
52
|
/**
|
|
53
|
-
* Parser Options.
|
|
54
|
-
*/
|
|
53
|
+
* Parser Options.
|
|
54
|
+
*/
|
|
55
55
|
//#endregion
|
|
56
|
-
//#region ../../node_modules/.pnpm/args-tokens@0.
|
|
56
|
+
//#region ../../node_modules/.pnpm/args-tokens@0.22.0/node_modules/args-tokens/lib/resolver-BoS-UnqX.d.ts
|
|
57
57
|
//#region src/resolver.d.ts
|
|
58
58
|
|
|
59
59
|
/**
|
|
60
|
-
* An argument schema
|
|
61
|
-
* This schema is similar to the schema of the `node:utils`.
|
|
62
|
-
* difference is that:
|
|
63
|
-
* - `required` property and `description` property are added
|
|
64
|
-
* - `type` is not only 'string' and 'boolean', but also 'number', 'enum', 'positional', 'custom' too.
|
|
65
|
-
* - `default` property type, not support multiple types
|
|
66
|
-
*/
|
|
60
|
+
* An argument schema
|
|
61
|
+
* This schema is similar to the schema of the `node:utils`.
|
|
62
|
+
* difference is that:
|
|
63
|
+
* - `required` property and `description` property are added
|
|
64
|
+
* - `type` is not only 'string' and 'boolean', but also 'number', 'enum', 'positional', 'custom' too.
|
|
65
|
+
* - `default` property type, not support multiple types
|
|
66
|
+
*/
|
|
67
67
|
interface ArgSchema {
|
|
68
68
|
/**
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
type:
|
|
69
|
+
* Type of argument.
|
|
70
|
+
*/
|
|
71
|
+
type: 'string' | 'boolean' | 'number' | 'enum' | 'positional' | 'custom';
|
|
72
72
|
/**
|
|
73
|
-
|
|
74
|
-
|
|
73
|
+
* A single character alias for the argument.
|
|
74
|
+
*/
|
|
75
75
|
short?: string;
|
|
76
76
|
/**
|
|
77
|
-
|
|
78
|
-
|
|
77
|
+
* A description of the argument.
|
|
78
|
+
*/
|
|
79
79
|
description?: string;
|
|
80
80
|
/**
|
|
81
|
-
|
|
82
|
-
|
|
81
|
+
* Whether the argument is required or not.
|
|
82
|
+
*/
|
|
83
83
|
required?: true;
|
|
84
84
|
/**
|
|
85
|
-
|
|
86
|
-
|
|
85
|
+
* Whether the argument allow multiple values or not.
|
|
86
|
+
*/
|
|
87
87
|
multiple?: true;
|
|
88
88
|
/**
|
|
89
|
-
|
|
90
|
-
|
|
89
|
+
* Whether the negatable option for `boolean` type
|
|
90
|
+
*/
|
|
91
91
|
negatable?: boolean;
|
|
92
92
|
/**
|
|
93
|
-
|
|
94
|
-
|
|
93
|
+
* The allowed values of the argument, and string only. This property is only used when the type is 'enum'.
|
|
94
|
+
*/
|
|
95
95
|
choices?: string[] | readonly string[];
|
|
96
96
|
/**
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
97
|
+
* The default value of the argument.
|
|
98
|
+
* if the type is 'enum', the default value must be one of the allowed values.
|
|
99
|
+
*/
|
|
100
100
|
default?: string | boolean | number;
|
|
101
101
|
/**
|
|
102
|
-
|
|
103
|
-
|
|
102
|
+
* Whether to convert the argument name to kebab-case.
|
|
103
|
+
*/
|
|
104
104
|
toKebab?: true;
|
|
105
105
|
/**
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
106
|
+
* A function to parse the value of the argument. if the type is 'custom', this function is required.
|
|
107
|
+
* If argument value will be invalid, this function have to throw an error.
|
|
108
|
+
* @param value
|
|
109
|
+
* @returns Parsed value
|
|
110
|
+
* @throws An Error, If the value is invalid. Error type should be `Error` or extends it
|
|
111
|
+
*/
|
|
113
112
|
parse?: (value: string) => any;
|
|
114
113
|
}
|
|
115
114
|
/**
|
|
116
|
-
* An object that contains {@link ArgSchema | argument schema}.
|
|
117
|
-
*/
|
|
115
|
+
* An object that contains {@link ArgSchema | argument schema}.
|
|
116
|
+
*/
|
|
118
117
|
interface Args {
|
|
119
118
|
[option: string]: ArgSchema;
|
|
120
119
|
}
|
|
121
120
|
/**
|
|
122
|
-
* An object that contains the values of the arguments.
|
|
123
|
-
*/
|
|
121
|
+
* An object that contains the values of the arguments.
|
|
122
|
+
*/
|
|
124
123
|
type ArgValues<T> = T extends Args ? ResolveArgValues<T, { [Arg in keyof T]: ExtractOptionValue<T[Arg]> }> : {
|
|
125
124
|
[option: string]: string | boolean | number | (string | boolean | number)[] | undefined;
|
|
126
125
|
};
|
|
127
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
128
126
|
type IsFunction<T> = T extends ((...args: any[]) => any) ? true : false;
|
|
129
127
|
/**
|
|
130
|
-
* @internal
|
|
131
|
-
*/
|
|
132
|
-
type ExtractOptionValue<A extends ArgSchema> = A[
|
|
133
|
-
type ResolveOptionValue<A extends ArgSchema, T> = A[
|
|
128
|
+
* @internal
|
|
129
|
+
*/
|
|
130
|
+
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>;
|
|
131
|
+
type ResolveOptionValue<A extends ArgSchema, T> = A['multiple'] extends true ? T[] : T;
|
|
134
132
|
/**
|
|
135
|
-
* @internal
|
|
136
|
-
*/
|
|
137
|
-
type ResolveArgValues<A extends Args, V extends Record<keyof A, unknown>> = { -readonly [Arg in keyof A]?: V[Arg] } & FilterArgs<A, V,
|
|
133
|
+
* @internal
|
|
134
|
+
*/
|
|
135
|
+
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;
|
|
138
136
|
/**
|
|
139
|
-
* @internal
|
|
140
|
-
*/
|
|
137
|
+
* @internal
|
|
138
|
+
*/
|
|
141
139
|
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] };
|
|
142
140
|
/**
|
|
143
|
-
* @internal
|
|
144
|
-
*/
|
|
145
|
-
type FilterPositionalArgs<A extends Args, V extends Record<keyof A, unknown>> = { [Arg in keyof A as A[Arg][
|
|
141
|
+
* @internal
|
|
142
|
+
*/
|
|
143
|
+
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] };
|
|
144
|
+
/**
|
|
145
|
+
* An arguments for {@link resolveArgs | resolve arguments}.
|
|
146
|
+
*/
|
|
147
|
+
|
|
146
148
|
/**
|
|
147
|
-
*
|
|
148
|
-
|
|
149
|
+
* Tracks which arguments were explicitly provided by the user.
|
|
150
|
+
*
|
|
151
|
+
* Each property indicates whether the corresponding argument was explicitly
|
|
152
|
+
* provided (true) or is using a default value or not provided (false).
|
|
153
|
+
*/
|
|
154
|
+
type ArgExplicitlyProvided<A extends Args> = { [K in keyof A]: boolean };
|
|
155
|
+
/**
|
|
156
|
+
* Resolve command line arguments.
|
|
157
|
+
* @param args - An arguments that contains {@link ArgSchema | arguments schema}.
|
|
158
|
+
* @param tokens - An array of {@link ArgToken | tokens}.
|
|
159
|
+
* @param resolveArgs - An arguments that contains {@link ResolveArgs | resolve arguments}.
|
|
160
|
+
* @returns An object that contains the values of the arguments, positional arguments, rest arguments, {@link AggregateError | validation errors}, and explicit provision status.
|
|
161
|
+
*
|
|
162
|
+
* @example
|
|
163
|
+
* ```typescript
|
|
164
|
+
* // passed tokens: --port 3000
|
|
165
|
+
*
|
|
166
|
+
* const { values, explicit } = resolveArgs({
|
|
167
|
+
* port: {
|
|
168
|
+
* type: 'number',
|
|
169
|
+
* default: 8080
|
|
170
|
+
* },
|
|
171
|
+
* host: {
|
|
172
|
+
* type: 'string',
|
|
173
|
+
* default: 'localhost'
|
|
174
|
+
* }
|
|
175
|
+
* }, parsedTokens)
|
|
176
|
+
*
|
|
177
|
+
* values.port // 3000
|
|
178
|
+
* values.host // 'localhost'
|
|
179
|
+
*
|
|
180
|
+
* explicit.port // true (explicitly provided)
|
|
181
|
+
* explicit.host // false (not provided, fallback to default)
|
|
182
|
+
* ```
|
|
183
|
+
*/
|
|
149
184
|
//#endregion
|
|
150
185
|
//#region ../gunshi/src/types.d.ts
|
|
151
186
|
type Awaitable<T> = T | Promise<T>;
|
|
@@ -197,6 +232,11 @@ type GunshiParamsConstraint = GunshiParams<any> | {
|
|
|
197
232
|
* @internal
|
|
198
233
|
*/
|
|
199
234
|
type ExtractArgs<G> = G extends GunshiParams<any> ? G['args'] : Args;
|
|
235
|
+
/**
|
|
236
|
+
* Type helper to extract explicitly provided argument flags from G
|
|
237
|
+
* @internal
|
|
238
|
+
*/
|
|
239
|
+
type ExtractArgExplicitlyProvided<G> = ArgExplicitlyProvided<ExtractArgs<G>>;
|
|
200
240
|
/**
|
|
201
241
|
* Type helper to extract extensions from G
|
|
202
242
|
* @internal
|
|
@@ -333,6 +373,15 @@ interface CommandContext<G extends GunshiParamsConstraint = DefaultGunshiParams>
|
|
|
333
373
|
* The command arguments is same {@link Command.args}.
|
|
334
374
|
*/
|
|
335
375
|
args: ExtractArgs<G>;
|
|
376
|
+
/**
|
|
377
|
+
* Whether arguments were explicitly provided by the user.
|
|
378
|
+
*
|
|
379
|
+
* - `true`: The argument was explicitly provided via command line
|
|
380
|
+
* - `false`: The argument was not explicitly provided. This means either:
|
|
381
|
+
* - The value comes from a default value defined in the argument schema
|
|
382
|
+
* - The value is `undefined` (no explicit input and no default value)
|
|
383
|
+
*/
|
|
384
|
+
explicit: ExtractArgExplicitlyProvided<G>;
|
|
336
385
|
/**
|
|
337
386
|
* Command values, that is the values of the command that is executed.
|
|
338
387
|
* Resolve values with `resolveArgs` from command arguments and {@link Command.args}.
|
|
@@ -427,6 +476,19 @@ interface Command<G extends GunshiParamsConstraint = DefaultGunshiParams> {
|
|
|
427
476
|
* If you will set to `true`, All {@link Command.args} names will be converted to kebab-case.
|
|
428
477
|
*/
|
|
429
478
|
toKebab?: boolean;
|
|
479
|
+
/**
|
|
480
|
+
* Whether this is an internal command.
|
|
481
|
+
* Internal commands are not shown in help usage.
|
|
482
|
+
* @default false
|
|
483
|
+
* @since v0.27.0
|
|
484
|
+
*/
|
|
485
|
+
internal?: boolean;
|
|
486
|
+
/**
|
|
487
|
+
* Whether this command is an entry command.
|
|
488
|
+
* @default undefined
|
|
489
|
+
* @since v0.27.0
|
|
490
|
+
*/
|
|
491
|
+
entry?: boolean;
|
|
430
492
|
}
|
|
431
493
|
/**
|
|
432
494
|
* Lazy command interface.
|
package/lib/index.js
CHANGED
|
@@ -19,6 +19,8 @@ function lazy(loader, definition) {
|
|
|
19
19
|
lazyCommand.description = definition.description;
|
|
20
20
|
lazyCommand.args = definition.args;
|
|
21
21
|
lazyCommand.examples = definition.examples;
|
|
22
|
+
lazyCommand.internal = definition.internal;
|
|
23
|
+
lazyCommand.entry = definition.entry;
|
|
22
24
|
if ("resource" in definition) lazyCommand.resource = definition.resource;
|
|
23
25
|
lazyCommand.toKebab = definition.toKebab;
|
|
24
26
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gunshi/definition",
|
|
3
3
|
"description": "utilities for gunshi command definition",
|
|
4
|
-
"version": "0.27.0-alpha.
|
|
4
|
+
"version": "0.27.0-alpha.7",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "kazuya kawaguchi",
|
|
7
7
|
"email": "kawakazu80@gmail.com"
|
|
@@ -57,7 +57,7 @@
|
|
|
57
57
|
"jsr-exports-lint": "^0.4.1",
|
|
58
58
|
"publint": "^0.3.12",
|
|
59
59
|
"tsdown": "^0.12.9",
|
|
60
|
-
"gunshi": "0.27.0-alpha.
|
|
60
|
+
"gunshi": "0.27.0-alpha.7"
|
|
61
61
|
},
|
|
62
62
|
"scripts": {
|
|
63
63
|
"build": "tsdown",
|