@bemoje/cli 4.0.0 → 5.0.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/README.md +382 -372
- package/lib/index.cjs +442 -290
- package/lib/index.d.cts +247 -47
- package/lib/index.d.ts +247 -47
- package/lib/index.js +442 -290
- package/package.json +5 -2
package/lib/index.d.cts
CHANGED
|
@@ -894,6 +894,31 @@ type SimpleMerge<Destination, Source> = Simplify<{
|
|
|
894
894
|
/**
|
|
895
895
|
Merge two types into a new type. Keys of the second type overrides keys of the first type.
|
|
896
896
|
|
|
897
|
+
This is different from the TypeScript `&` (intersection) operator. With `&`, conflicting property types are intersected, which often results in `never`. For example, `{a: string} & {a: number}` makes `a` become `string & number`, which resolves to `never`. With `Merge`, the second type's keys cleanly override the first, so `Merge<{a: string}, {a: number}>` gives `{a: number}` as expected. `Merge` also produces a flattened type (via `Simplify`), making it more readable in IDE tooltips compared to `A & B`.
|
|
898
|
+
|
|
899
|
+
@example
|
|
900
|
+
```
|
|
901
|
+
import type {Merge} from 'type-fest';
|
|
902
|
+
|
|
903
|
+
type Foo = {
|
|
904
|
+
a: string;
|
|
905
|
+
b: number;
|
|
906
|
+
};
|
|
907
|
+
|
|
908
|
+
type Bar = {
|
|
909
|
+
a: number; // Conflicts with Foo['a']
|
|
910
|
+
c: boolean;
|
|
911
|
+
};
|
|
912
|
+
|
|
913
|
+
// With `&`, `a` becomes `string & number` which is `never`. Not what you want.
|
|
914
|
+
type WithIntersection = (Foo & Bar)['a'];
|
|
915
|
+
//=> never
|
|
916
|
+
|
|
917
|
+
// With `Merge`, `a` is cleanly overridden to `number`.
|
|
918
|
+
type WithMerge = Merge<Foo, Bar>['a'];
|
|
919
|
+
//=> number
|
|
920
|
+
```
|
|
921
|
+
|
|
897
922
|
@example
|
|
898
923
|
```
|
|
899
924
|
import type {Merge} from 'type-fest';
|
|
@@ -1083,6 +1108,180 @@ type ApplyDefaultOptions<
|
|
|
1083
1108
|
]: SpecifiedOptions[Key]
|
|
1084
1109
|
}> & Required<Options>>>>;
|
|
1085
1110
|
|
|
1111
|
+
/**
|
|
1112
|
+
@see {@link SomeExtend}
|
|
1113
|
+
*/
|
|
1114
|
+
type SomeExtendOptions = {
|
|
1115
|
+
/**
|
|
1116
|
+
Consider `never` elements to match the target type only if the target type itself is `never` (or `any`).
|
|
1117
|
+
|
|
1118
|
+
- When set to `true` (default), `never` is _not_ treated as a bottom type, instead, it is treated as a type that matches only itself (or `any`).
|
|
1119
|
+
- When set to `false`, `never` is treated as a bottom type, and behaves as it normally would.
|
|
1120
|
+
|
|
1121
|
+
@default true
|
|
1122
|
+
|
|
1123
|
+
@example
|
|
1124
|
+
```
|
|
1125
|
+
import type {SomeExtend} from 'type-fest';
|
|
1126
|
+
|
|
1127
|
+
type A = SomeExtend<[1, 2, never], string, {strictNever: true}>;
|
|
1128
|
+
//=> false
|
|
1129
|
+
|
|
1130
|
+
type B = SomeExtend<[1, 2, never], string, {strictNever: false}>;
|
|
1131
|
+
//=> true
|
|
1132
|
+
|
|
1133
|
+
type C = SomeExtend<[1, never], never, {strictNever: true}>;
|
|
1134
|
+
//=> true
|
|
1135
|
+
|
|
1136
|
+
type D = SomeExtend<[1, never], never, {strictNever: false}>;
|
|
1137
|
+
//=> true
|
|
1138
|
+
|
|
1139
|
+
type E = SomeExtend<[never], any, {strictNever: true}>;
|
|
1140
|
+
//=> true
|
|
1141
|
+
|
|
1142
|
+
type F = SomeExtend<[never], any, {strictNever: false}>;
|
|
1143
|
+
//=> true
|
|
1144
|
+
```
|
|
1145
|
+
*/
|
|
1146
|
+
strictNever?: boolean;
|
|
1147
|
+
};
|
|
1148
|
+
|
|
1149
|
+
type DefaultSomeExtendOptions = {
|
|
1150
|
+
strictNever: true;
|
|
1151
|
+
};
|
|
1152
|
+
|
|
1153
|
+
/**
|
|
1154
|
+
Returns a boolean for whether some element in an array type extends another type.
|
|
1155
|
+
|
|
1156
|
+
@example
|
|
1157
|
+
```
|
|
1158
|
+
import type {SomeExtend} from 'type-fest';
|
|
1159
|
+
|
|
1160
|
+
type A = SomeExtend<['1', '2', 3], number>;
|
|
1161
|
+
//=> true
|
|
1162
|
+
|
|
1163
|
+
type B = SomeExtend<[1, 2, 3], string>;
|
|
1164
|
+
//=> false
|
|
1165
|
+
|
|
1166
|
+
type C = SomeExtend<[string, number | string], number>;
|
|
1167
|
+
//=> boolean
|
|
1168
|
+
|
|
1169
|
+
type D = SomeExtend<[true, boolean, true], false>;
|
|
1170
|
+
//=> boolean
|
|
1171
|
+
```
|
|
1172
|
+
|
|
1173
|
+
Note: Behaviour of optional elements depend on the `exactOptionalPropertyTypes` compiler option. When the option is disabled, the target type must include `undefined` for a successful match.
|
|
1174
|
+
|
|
1175
|
+
```
|
|
1176
|
+
// @exactOptionalPropertyTypes: true
|
|
1177
|
+
import type {SomeExtend} from 'type-fest';
|
|
1178
|
+
|
|
1179
|
+
type A = SomeExtend<[1?, 2?, '3'?], string>;
|
|
1180
|
+
//=> true
|
|
1181
|
+
```
|
|
1182
|
+
|
|
1183
|
+
```
|
|
1184
|
+
// @exactOptionalPropertyTypes: false
|
|
1185
|
+
import type {SomeExtend} from 'type-fest';
|
|
1186
|
+
|
|
1187
|
+
type A = SomeExtend<[1?, 2?, '3'?], string>;
|
|
1188
|
+
//=> boolean
|
|
1189
|
+
|
|
1190
|
+
type B = SomeExtend<[1?, 2?, '3'?], string | undefined>;
|
|
1191
|
+
//=> true
|
|
1192
|
+
```
|
|
1193
|
+
|
|
1194
|
+
@see {@link SomeExtendOptions}
|
|
1195
|
+
|
|
1196
|
+
@category Utilities
|
|
1197
|
+
@category Array
|
|
1198
|
+
*/
|
|
1199
|
+
type SomeExtend<TArray extends UnknownArray, Type, Options extends SomeExtendOptions = {}> =
|
|
1200
|
+
_SomeExtend<CollapseRestElement<TArray>, Type, ApplyDefaultOptions<SomeExtendOptions, DefaultSomeExtendOptions, Options>>;
|
|
1201
|
+
|
|
1202
|
+
type _SomeExtend<TArray extends UnknownArray, Type, Options extends Required<SomeExtendOptions>> = IfNotAnyOrNever<TArray,
|
|
1203
|
+
TArray extends readonly [infer First, ...infer Rest]
|
|
1204
|
+
? IsNever<First> extends true
|
|
1205
|
+
? Or<Or<IsNever<Type>, IsAny<Type>>, Not<Options['strictNever']>> extends true
|
|
1206
|
+
// If target `Type` is also `never`, or is `any`, or `strictNever` is disabled, return `true`.
|
|
1207
|
+
? true
|
|
1208
|
+
: _SomeExtend<Rest, Type, Options>
|
|
1209
|
+
: First extends Type
|
|
1210
|
+
? true
|
|
1211
|
+
: _SomeExtend<Rest, Type, Options>
|
|
1212
|
+
: false,
|
|
1213
|
+
false, false>;
|
|
1214
|
+
|
|
1215
|
+
/**
|
|
1216
|
+
Returns a boolean for whether any of the given elements is `true`.
|
|
1217
|
+
|
|
1218
|
+
Use-cases:
|
|
1219
|
+
- Check if at least one condition in a list of booleans is met.
|
|
1220
|
+
|
|
1221
|
+
@example
|
|
1222
|
+
```
|
|
1223
|
+
import type {OrAll} from 'type-fest';
|
|
1224
|
+
|
|
1225
|
+
type FFT = OrAll<[false, false, true]>;
|
|
1226
|
+
//=> true
|
|
1227
|
+
|
|
1228
|
+
type FFF = OrAll<[false, false, false]>;
|
|
1229
|
+
//=> false
|
|
1230
|
+
```
|
|
1231
|
+
|
|
1232
|
+
Note: When `boolean` is passed as an element, it is distributed into separate cases, and the final result is a union of those cases.
|
|
1233
|
+
For example, `OrAll<[false, boolean]>` expands to `OrAll<[false, true]> | OrAll<[false, false]>`, which simplifies to `true | false` (i.e., `boolean`).
|
|
1234
|
+
|
|
1235
|
+
@example
|
|
1236
|
+
```
|
|
1237
|
+
import type {OrAll} from 'type-fest';
|
|
1238
|
+
|
|
1239
|
+
type A = OrAll<[false, boolean]>;
|
|
1240
|
+
//=> boolean
|
|
1241
|
+
|
|
1242
|
+
type B = OrAll<[true, boolean]>;
|
|
1243
|
+
//=> true
|
|
1244
|
+
```
|
|
1245
|
+
|
|
1246
|
+
Note: If `never` is passed as an element, it is treated as `false` and the result is computed accordingly.
|
|
1247
|
+
|
|
1248
|
+
@example
|
|
1249
|
+
```
|
|
1250
|
+
import type {OrAll} from 'type-fest';
|
|
1251
|
+
|
|
1252
|
+
type A = OrAll<[never, never, true]>;
|
|
1253
|
+
//=> true
|
|
1254
|
+
|
|
1255
|
+
type B = OrAll<[never, never, false]>;
|
|
1256
|
+
//=> false
|
|
1257
|
+
|
|
1258
|
+
type C = OrAll<[never, never, never]>;
|
|
1259
|
+
//=> false
|
|
1260
|
+
|
|
1261
|
+
type D = OrAll<[never, never, boolean]>;
|
|
1262
|
+
//=> boolean
|
|
1263
|
+
```
|
|
1264
|
+
|
|
1265
|
+
Note: If `any` is passed as an element, it is treated as `boolean` and the result is computed accordingly.
|
|
1266
|
+
|
|
1267
|
+
@example
|
|
1268
|
+
```
|
|
1269
|
+
import type {OrAll} from 'type-fest';
|
|
1270
|
+
|
|
1271
|
+
type A = OrAll<[false, any]>;
|
|
1272
|
+
//=> boolean
|
|
1273
|
+
|
|
1274
|
+
type B = OrAll<[true, any]>;
|
|
1275
|
+
//=> true
|
|
1276
|
+
```
|
|
1277
|
+
|
|
1278
|
+
Note: `OrAll<[]>` evaluates to `false` because there are no `true` elements in an empty tuple. See [Wikipedia: Clause (logic) > Empty clauses](https://en.wikipedia.org/wiki/Clause_(logic)#Empty_clauses:~:text=The%20truth%20evaluation%20of%20an%20empty%20disjunctive%20clause%20is%20always%20false.).
|
|
1279
|
+
|
|
1280
|
+
@see {@link Or}
|
|
1281
|
+
@see {@link AndAll}
|
|
1282
|
+
*/
|
|
1283
|
+
type OrAll<T extends readonly boolean[]> = SomeExtend<T, true>;
|
|
1284
|
+
|
|
1086
1285
|
/**
|
|
1087
1286
|
Returns a boolean for whether either of two given types is true.
|
|
1088
1287
|
|
|
@@ -1156,17 +1355,11 @@ type G = Or<never, never>;
|
|
|
1156
1355
|
//=> false
|
|
1157
1356
|
```
|
|
1158
1357
|
|
|
1358
|
+
@see {@link OrAll}
|
|
1159
1359
|
@see {@link And}
|
|
1160
1360
|
@see {@link Xor}
|
|
1161
1361
|
*/
|
|
1162
|
-
type Or<A extends boolean, B extends boolean> =
|
|
1163
|
-
_Or<If<IsNever<A>, false, A>, If<IsNever<B>, false, B>>; // `never` is treated as `false`
|
|
1164
|
-
|
|
1165
|
-
type _Or<A extends boolean, B extends boolean> = A extends true
|
|
1166
|
-
? true
|
|
1167
|
-
: B extends true
|
|
1168
|
-
? true
|
|
1169
|
-
: false;
|
|
1362
|
+
type Or<A extends boolean, B extends boolean> = OrAll<[A, B]>;
|
|
1170
1363
|
|
|
1171
1364
|
/**
|
|
1172
1365
|
@see {@link AllExtend}
|
|
@@ -1265,18 +1458,18 @@ type B = AllExtend<[1?, 2?, 3?], number | undefined>;
|
|
|
1265
1458
|
type AllExtend<TArray extends UnknownArray, Type, Options extends AllExtendOptions = {}> =
|
|
1266
1459
|
_AllExtend<CollapseRestElement<TArray>, Type, ApplyDefaultOptions<AllExtendOptions, DefaultAllExtendOptions, Options>>;
|
|
1267
1460
|
|
|
1268
|
-
type _AllExtend<TArray extends UnknownArray, Type, Options extends Required<AllExtendOptions>> = IfNotAnyOrNever<TArray,
|
|
1461
|
+
type _AllExtend<TArray extends UnknownArray, Type, Options extends Required<AllExtendOptions>> = IfNotAnyOrNever<TArray,
|
|
1269
1462
|
TArray extends readonly [infer First, ...infer Rest]
|
|
1270
1463
|
? IsNever<First> extends true
|
|
1271
|
-
? Or<IsNever<Type>, Not<Options['strictNever']>> extends true
|
|
1272
|
-
// If target `Type` is also `never`
|
|
1464
|
+
? Or<Or<IsNever<Type>, IsAny<Type>>, Not<Options['strictNever']>> extends true
|
|
1465
|
+
// If target `Type` is also `never`, or is `any`, or `strictNever` is disabled, recurse further.
|
|
1273
1466
|
? _AllExtend<Rest, Type, Options>
|
|
1274
1467
|
: false
|
|
1275
1468
|
: First extends Type
|
|
1276
1469
|
? _AllExtend<Rest, Type, Options>
|
|
1277
1470
|
: false
|
|
1278
|
-
: true
|
|
1279
|
-
|
|
1471
|
+
: true,
|
|
1472
|
+
false, false>;
|
|
1280
1473
|
|
|
1281
1474
|
/**
|
|
1282
1475
|
Filter out keys from an object.
|
|
@@ -1402,11 +1595,7 @@ type Foo = {
|
|
|
1402
1595
|
};
|
|
1403
1596
|
|
|
1404
1597
|
type SomeRequired = SetRequired<Foo, 'b' | 'c'>;
|
|
1405
|
-
|
|
1406
|
-
// a?: number;
|
|
1407
|
-
// b: string; // Was already required and still is.
|
|
1408
|
-
// c: boolean; // Is now required.
|
|
1409
|
-
// }
|
|
1598
|
+
//=> {a?: number; b: string; c: boolean}
|
|
1410
1599
|
|
|
1411
1600
|
// Set specific indices in an array to be required.
|
|
1412
1601
|
type ArrayExample = SetRequired<[number?, number?, number?], 0 | 1>;
|
|
@@ -2128,7 +2317,9 @@ declare class Help implements IHelp {
|
|
|
2128
2317
|
declare class Command<A extends Arguments = [], O extends Options = {
|
|
2129
2318
|
help?: boolean;
|
|
2130
2319
|
debug?: boolean;
|
|
2131
|
-
}, Subs extends SubCommands = SubCommands
|
|
2320
|
+
}, Subs extends SubCommands = SubCommands,
|
|
2321
|
+
/** what the action returns (see `setAction`), which `execute()` gives back */
|
|
2322
|
+
R = unknown> implements ICommand {
|
|
2132
2323
|
/** parent command in the hierarchy, undefined for root command */
|
|
2133
2324
|
parent?: Command<Arguments, Options & O>;
|
|
2134
2325
|
/** the command name used to invoke it */
|
|
@@ -2152,7 +2343,7 @@ declare class Command<A extends Arguments = [], O extends Options = {
|
|
|
2152
2343
|
/** subcommands registered with this command */
|
|
2153
2344
|
commands: Subs;
|
|
2154
2345
|
/** main action handler executed when command is invoked */
|
|
2155
|
-
protected action?: ActionHandler<A, O, Subs>;
|
|
2346
|
+
protected action?: ActionHandler<A, O, Subs, R>;
|
|
2156
2347
|
/** option-driven actions (e.g., --help, --version) executed when their conditions match */
|
|
2157
2348
|
protected hooks: HookDefinition<Arguments, Options & O>[];
|
|
2158
2349
|
constructor(name: string, parent?: ICommand);
|
|
@@ -2197,34 +2388,36 @@ declare class Command<A extends Arguments = [], O extends Options = {
|
|
|
2197
2388
|
addArgument<const Opts extends OptionalArgumentOptionsWithDefaultValue>(usage: AllowedArgumentUsage<this, `[${string}]`>, options: Opts): Command<[...A, InferAddedArgumentType<Opts>], O, Subs>;
|
|
2198
2389
|
/** add optional argument, eg.: `[name]` */
|
|
2199
2390
|
addArgument<const Opts extends OptionalArgumentOptions>(usage: AllowedArgumentUsage<this, `[${string}]`>, options?: Opts): Command<[...A, InferAddedArgumentType<Opts> | undefined], O, Subs>;
|
|
2391
|
+
/** add optional variadic option with defaults (before the optional string option, whose `[path]` also matches `[patterns...]`), eg.: `-e, --exclude [patterns...]` */
|
|
2392
|
+
addOption<Long extends string>(flags: `-${string}, --${Long} [${string}...]`, options?: OptionalVariadicOptionOptions): Command<A, Simplify<{
|
|
2393
|
+
[K in CamelCase<Long>]: string[];
|
|
2394
|
+
} & Omit<O, CamelCase<Long>>>, Subs>;
|
|
2200
2395
|
/** add optional string option, eg.: `-o, --output [path]` */
|
|
2201
2396
|
addOption<Long extends string>(flags: `-${string}, --${Long} [${string}]`, options?: SetFieldType<OptionalOptionOptions, 'defaultValue', undefined | never>): Command<A, Simplify<{
|
|
2202
2397
|
[K in CamelCase<Long>]?: string;
|
|
2203
|
-
} & O
|
|
2398
|
+
} & Omit<O, CamelCase<Long>>>, Subs>;
|
|
2204
2399
|
/** add optional string option with default, eg.: `-o, --output [path]` */
|
|
2205
2400
|
addOption<Long extends string>(flags: `-${string}, --${Long} [${string}]`, options: SetFieldType<SetRequired<OptionalOptionOptions, 'defaultValue'>, 'defaultValue', string>): Command<A, Simplify<{
|
|
2206
2401
|
[K in CamelCase<Long>]: string;
|
|
2207
|
-
} & O
|
|
2402
|
+
} & Omit<O, CamelCase<Long>>>, Subs>;
|
|
2208
2403
|
/** add required variadic option, eg.: `-i, --include <patterns...>` */
|
|
2209
2404
|
addOption<Long extends string>(flags: `-${string}, --${Long} <${string}...>`, options?: RequiredVariadicOptionOptions): Command<A, Simplify<{
|
|
2210
2405
|
[K in CamelCase<Long>]: string[];
|
|
2211
|
-
} & O
|
|
2212
|
-
/** add optional variadic option with defaults, eg.: `-e, --exclude [patterns...]` */
|
|
2213
|
-
addOption<Long extends string>(flags: `-${string}, --${Long} [${string}...]`, options?: OptionalVariadicOptionOptions): Command<A, Simplify<{
|
|
2214
|
-
[K in CamelCase<Long>]: string[];
|
|
2215
|
-
} & O>, Subs>;
|
|
2406
|
+
} & Omit<O, CamelCase<Long>>>, Subs>;
|
|
2216
2407
|
/** add required string option, eg.: `-f, --file <path>` */
|
|
2217
2408
|
addOption<Long extends string>(flags: `-${string}, --${Long} <${string}>`, options?: RequiredOptionOptions): Command<A, Simplify<{
|
|
2218
2409
|
[K in CamelCase<Long>]: string;
|
|
2219
|
-
} & O
|
|
2410
|
+
} & Omit<O, CamelCase<Long>>>, Subs>;
|
|
2220
2411
|
/** add boolean flag option with default, eg.: `-v, --verbose` */
|
|
2221
2412
|
addOption<Long extends string>(flags: `-${string}, --${Long}`, options: SetFieldType<SetRequired<BooleanOptionOptions, 'defaultValue'>, 'defaultValue', boolean>): Command<A, Simplify<{
|
|
2222
2413
|
[K in CamelCase<Long>]: boolean;
|
|
2223
|
-
} & O
|
|
2414
|
+
} & Omit<O, CamelCase<Long>>>, Subs>;
|
|
2224
2415
|
/** add boolean flag option, eg.: `-v, --verbose` */
|
|
2225
2416
|
addOption<Long extends string>(flags: `-${string}, --${Long}`, options?: SetFieldType<BooleanOptionOptions, 'defaultValue', undefined | never>): Command<A, Simplify<{
|
|
2226
2417
|
[K in CamelCase<Long>]?: boolean;
|
|
2227
|
-
} & O
|
|
2418
|
+
} & Omit<O, CamelCase<Long>>>, Subs>;
|
|
2419
|
+
/** adds a built-in option with its hook; it goes without its short flag when an option already has that */
|
|
2420
|
+
protected addBuiltinOption<Long extends string>(flags: `-${string}, --${Long}`, description: string, action: HookActionHandler<Arguments, Options>): this;
|
|
2228
2421
|
/**
|
|
2229
2422
|
* register an action to be invoked when an option is set to true or string value.
|
|
2230
2423
|
*
|
|
@@ -2234,12 +2427,13 @@ declare class Command<A extends Arguments = [], O extends Options = {
|
|
|
2234
2427
|
*/
|
|
2235
2428
|
addOptionHook(optionName: keyof O, action: HookActionHandler<Arguments, O>): this;
|
|
2236
2429
|
/** parses command-line arguments with subcommand support and type-safe validation. */
|
|
2237
|
-
parseArgv(argv?: string[]): ParseArgvResult<Arguments, Options & O
|
|
2430
|
+
parseArgv(argv?: string[]): ParseArgvResult<Arguments, Options & O, SubCommands, ActionResult<Subs, R>>;
|
|
2238
2431
|
/**
|
|
2239
2432
|
* sets the main action handler for this command, which is executed after any matching option hooks when the command is invoked.
|
|
2240
|
-
* The handler receives parsed arguments and options with correct typings.
|
|
2433
|
+
* The handler receives parsed arguments and options with correct typings. It may return a value (or a promise of one):
|
|
2434
|
+
* `execute()` of the parse result gives it back, typed.
|
|
2241
2435
|
*/
|
|
2242
|
-
setAction(fn: ActionHandler<A, O, Subs>):
|
|
2436
|
+
setAction<Result>(fn: ActionHandler<A, O, Subs, Result>): Command<A, O, Subs, Awaited<Result>>;
|
|
2243
2437
|
/** returns a new Command instance. Override this method in subclasses. */
|
|
2244
2438
|
protected createSubcommand(name: string): Command<[], O, {}>;
|
|
2245
2439
|
}
|
|
@@ -2277,7 +2471,7 @@ interface Argument {
|
|
|
2277
2471
|
interface Option {
|
|
2278
2472
|
type: 'boolean' | 'string';
|
|
2279
2473
|
flags: string;
|
|
2280
|
-
short
|
|
2474
|
+
short?: string;
|
|
2281
2475
|
long: string;
|
|
2282
2476
|
name: string;
|
|
2283
2477
|
argName?: string;
|
|
@@ -2487,8 +2681,11 @@ interface IHelp {
|
|
|
2487
2681
|
*/
|
|
2488
2682
|
render(): string;
|
|
2489
2683
|
}
|
|
2490
|
-
/**
|
|
2491
|
-
|
|
2684
|
+
/**
|
|
2685
|
+
* Action handler function type, which receives parsed arguments and options as well as metadata about the command execution context.
|
|
2686
|
+
* What it returns (`R`, or a promise of it) is what `execute()` of the parse result gives back.
|
|
2687
|
+
*/
|
|
2688
|
+
type ActionHandler<A extends Arguments, O extends Options, Subs extends SubCommands, R = unknown> = (...args: [
|
|
2492
2689
|
...A,
|
|
2493
2690
|
O,
|
|
2494
2691
|
{
|
|
@@ -2501,7 +2698,13 @@ type ActionHandler<A extends Arguments, O extends Options, Subs extends SubComma
|
|
|
2501
2698
|
cmd: Command<A, O, Subs>;
|
|
2502
2699
|
logger: Logger;
|
|
2503
2700
|
}
|
|
2504
|
-
]) => Promise<
|
|
2701
|
+
]) => Promise<R> | R;
|
|
2702
|
+
/**
|
|
2703
|
+
* What `execute()` gives for a command whose action returns `R`: the action's result, or undefined when a hook (such as
|
|
2704
|
+
* --help) or a validation error stopped it before the action, or when the command has no action. A command with
|
|
2705
|
+
* subcommands gives `unknown`: the subcommand that matched has a result type of its own.
|
|
2706
|
+
*/
|
|
2707
|
+
type ActionResult<Subs extends SubCommands, R> = string extends keyof Subs ? Awaited<R> | undefined : [keyof Subs] extends [never] ? Awaited<R> | undefined : unknown;
|
|
2505
2708
|
/** Predicate function type for hooks, which receives the same metadata as action handlers and returns a boolean indicating whether the hook's action should be executed. */
|
|
2506
2709
|
type HookPredicate<A extends Arguments = Arguments, O extends Options = Options, Subs extends SubCommands = SubCommands> = (data: {
|
|
2507
2710
|
path: string[];
|
|
@@ -2521,6 +2724,7 @@ type HookActionHandler<A extends Arguments = Arguments, O extends Options = Opti
|
|
|
2521
2724
|
opts: O;
|
|
2522
2725
|
errors?: string[];
|
|
2523
2726
|
cmd: Command<A, O, Subs>;
|
|
2727
|
+
logger: Logger;
|
|
2524
2728
|
}) => Promise<void> | void | never;
|
|
2525
2729
|
/** Hook definition type, which includes the option name that triggers the hook, a predicate function to determine when the hook should run, and an action handler to execute when triggered. */
|
|
2526
2730
|
type HookDefinition<A extends Arguments = Arguments, O extends Options = Options, Subs extends SubCommands = SubCommands> = {
|
|
@@ -2531,7 +2735,7 @@ type HookDefinition<A extends Arguments = Arguments, O extends Options = Options
|
|
|
2531
2735
|
/**
|
|
2532
2736
|
* @see Command.prototype.parseArgv
|
|
2533
2737
|
*/
|
|
2534
|
-
type ParseArgvResult<A extends Arguments = Arguments, O extends Options = Options, Subs extends SubCommands = SubCommands> = {
|
|
2738
|
+
type ParseArgvResult<A extends Arguments = Arguments, O extends Options = Options, Subs extends SubCommands = SubCommands, R = unknown> = {
|
|
2535
2739
|
/** The command or subcommand instance */
|
|
2536
2740
|
get cmd(): Command<A, O, Subs>;
|
|
2537
2741
|
/** The part of argv that makes out a subcommand path, or empty array when root command */
|
|
@@ -2548,8 +2752,8 @@ type ParseArgvResult<A extends Arguments = Arguments, O extends Options = Option
|
|
|
2548
2752
|
errors?: string[];
|
|
2549
2753
|
/** Names of all triggered hooks whose predicate returned true */
|
|
2550
2754
|
hooks: HookDefinition<A, O, Subs>[];
|
|
2551
|
-
/** Calls the action handler with its expected args */
|
|
2552
|
-
execute: () => Promise<
|
|
2755
|
+
/** Calls the action handler with its expected args, and gives back what it returned (see `ActionResult`) */
|
|
2756
|
+
execute: () => Promise<R>;
|
|
2553
2757
|
};
|
|
2554
2758
|
/** required variadic argument */
|
|
2555
2759
|
type RequiredVariadicArgumentUsage = `<${string}...>`;
|
|
@@ -2603,18 +2807,14 @@ type BooleanOptionOptions = ExtendAddOptionOptionsBase<{
|
|
|
2603
2807
|
defaultValueDescription?: string;
|
|
2604
2808
|
}>;
|
|
2605
2809
|
/** Required string option. Usage: `-f, --file <path>` */
|
|
2606
|
-
type RequiredOptionOptions = ExtendAddOptionOptionsBase
|
|
2607
|
-
env?: undefined;
|
|
2608
|
-
}>;
|
|
2810
|
+
type RequiredOptionOptions = ExtendAddOptionOptionsBase;
|
|
2609
2811
|
/** Optional string option with default. Usage: `-o, --output [path]` */
|
|
2610
2812
|
type OptionalOptionOptions = ExtendAddOptionOptionsBase<{
|
|
2611
2813
|
defaultValue?: string;
|
|
2612
2814
|
defaultValueDescription?: string;
|
|
2613
2815
|
}>;
|
|
2614
2816
|
/** Required option accepting variadic values. Usage: `-i, --include <patterns...>` */
|
|
2615
|
-
type RequiredVariadicOptionOptions = ExtendAddOptionOptionsBase
|
|
2616
|
-
env?: undefined;
|
|
2617
|
-
}>;
|
|
2817
|
+
type RequiredVariadicOptionOptions = ExtendAddOptionOptionsBase;
|
|
2618
2818
|
/** Optional option accepting variadic values with defaults. Usage: `-e, --exclude [patterns...]` */
|
|
2619
2819
|
type OptionalVariadicOptionOptions = ExtendAddOptionOptionsBase<{
|
|
2620
2820
|
defaultValue?: string[];
|
|
@@ -2655,4 +2855,4 @@ declare function findOption<C extends ICommand>(cmd: C, nameOrShortOrLong: strin
|
|
|
2655
2855
|
*/
|
|
2656
2856
|
declare function findCommand<C extends Command = Command>(cmd: ICommand, nameOrAlias: string): C | undefined;
|
|
2657
2857
|
|
|
2658
|
-
export { type ActionHandler, type AllowedArgumentUsage, type Argument, type ArgumentOptions, type ArgumentUsage, type Arguments, type BooleanOptionOptions, Command, Help, type HookActionHandler, type HookDefinition, type HookPredicate, type ICommand, type IHelp, type InferAddOptionResult, type InferAddedArgumentType, type Logger, type Option, type OptionOptions, type OptionUsage, type OptionalArgumentOptions, type OptionalArgumentOptionsWithDefaultValue, type OptionalArgumentUsage, type OptionalOptionOptions, type OptionalVariadicArgumentOptions, type OptionalVariadicArgumentUsage, type OptionalVariadicOptionOptions, type Options, type ParseArgvResult, type RequiredArgumentOptions, type RequiredArgumentUsage, type RequiredOptionOptions, type RequiredVariadicArgumentOptions, type RequiredVariadicArgumentUsage, type RequiredVariadicOptionOptions, type SubCommands, findCommand, findOption, getCommandAncestors, getCommandAndAncestors, parseOptionFlags };
|
|
2858
|
+
export { type ActionHandler, type ActionResult, type AllowedArgumentUsage, type Argument, type ArgumentOptions, type ArgumentUsage, type Arguments, type BooleanOptionOptions, Command, Help, type HookActionHandler, type HookDefinition, type HookPredicate, type ICommand, type IHelp, type InferAddOptionResult, type InferAddedArgumentType, type Logger, type Option, type OptionOptions, type OptionUsage, type OptionalArgumentOptions, type OptionalArgumentOptionsWithDefaultValue, type OptionalArgumentUsage, type OptionalOptionOptions, type OptionalVariadicArgumentOptions, type OptionalVariadicArgumentUsage, type OptionalVariadicOptionOptions, type Options, type ParseArgvResult, type RequiredArgumentOptions, type RequiredArgumentUsage, type RequiredOptionOptions, type RequiredVariadicArgumentOptions, type RequiredVariadicArgumentUsage, type RequiredVariadicOptionOptions, type SubCommands, findCommand, findOption, getCommandAncestors, getCommandAndAncestors, parseOptionFlags };
|