@clerc/core 0.40.0 → 0.42.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 +9 -0
- package/dist/index.d.ts +175 -375
- package/dist/index.js +795 -1
- package/package.json +13 -13
package/dist/index.d.ts
CHANGED
|
@@ -1,232 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
type MaybeArray$1<T> = T | T[];
|
|
4
|
-
type AlphabetLowercase = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z";
|
|
5
|
-
type Numeric = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9";
|
|
6
|
-
type AlphaNumeric = AlphabetLowercase | Uppercase<AlphabetLowercase> | Numeric;
|
|
7
|
-
type CamelCase<Word extends string> = Word extends `${infer FirstCharacter}${infer Rest}` ? (FirstCharacter extends AlphaNumeric ? `${FirstCharacter}${CamelCase<Rest>}` : Capitalize<CamelCase<Rest>>) : Word;
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
Matches any [primitive value](https://developer.mozilla.org/en-US/docs/Glossary/Primitive).
|
|
11
|
-
|
|
12
|
-
@category Type
|
|
13
|
-
*/
|
|
14
|
-
type Primitive =
|
|
15
|
-
| null
|
|
16
|
-
| undefined
|
|
17
|
-
| string
|
|
18
|
-
| number
|
|
19
|
-
| boolean
|
|
20
|
-
| symbol
|
|
21
|
-
| bigint;
|
|
22
|
-
|
|
23
|
-
declare global {
|
|
24
|
-
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions -- It has to be an `interface` so that it can be merged.
|
|
25
|
-
interface SymbolConstructor {
|
|
26
|
-
readonly observable: symbol;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
Useful to flatten the type output to improve type hints shown in editors. And also to transform an interface into a type to aide with assignability.
|
|
32
|
-
|
|
33
|
-
@example
|
|
34
|
-
```
|
|
35
|
-
import type {Simplify} from 'type-fest';
|
|
36
|
-
|
|
37
|
-
type PositionProps = {
|
|
38
|
-
top: number;
|
|
39
|
-
left: number;
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
type SizeProps = {
|
|
43
|
-
width: number;
|
|
44
|
-
height: number;
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
// In your editor, hovering over `Props` will show a flattened object with all the properties.
|
|
48
|
-
type Props = Simplify<PositionProps & SizeProps>;
|
|
49
|
-
```
|
|
50
|
-
|
|
51
|
-
Sometimes it is desired to pass a value as a function argument that has a different type. At first inspection it may seem assignable, and then you discover it is not because the `value`'s type definition was defined as an interface. In the following example, `fn` requires an argument of type `Record<string, unknown>`. If the value is defined as a literal, then it is assignable. And if the `value` is defined as type using the `Simplify` utility the value is assignable. But if the `value` is defined as an interface, it is not assignable because the interface is not sealed and elsewhere a non-string property could be added to the interface.
|
|
52
|
-
|
|
53
|
-
If the type definition must be an interface (perhaps it was defined in a third-party npm package), then the `value` can be defined as `const value: Simplify<SomeInterface> = ...`. Then `value` will be assignable to the `fn` argument. Or the `value` can be cast as `Simplify<SomeInterface>` if you can't re-declare the `value`.
|
|
54
|
-
|
|
55
|
-
@example
|
|
56
|
-
```
|
|
57
|
-
import type {Simplify} from 'type-fest';
|
|
58
|
-
|
|
59
|
-
interface SomeInterface {
|
|
60
|
-
foo: number;
|
|
61
|
-
bar?: string;
|
|
62
|
-
baz: number | undefined;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
type SomeType = {
|
|
66
|
-
foo: number;
|
|
67
|
-
bar?: string;
|
|
68
|
-
baz: number | undefined;
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
const literal = {foo: 123, bar: 'hello', baz: 456};
|
|
72
|
-
const someType: SomeType = literal;
|
|
73
|
-
const someInterface: SomeInterface = literal;
|
|
74
|
-
|
|
75
|
-
function fn(object: Record<string, unknown>): void {}
|
|
76
|
-
|
|
77
|
-
fn(literal); // Good: literal object type is sealed
|
|
78
|
-
fn(someType); // Good: type is sealed
|
|
79
|
-
fn(someInterface); // Error: Index signature for type 'string' is missing in type 'someInterface'. Because `interface` can be re-opened
|
|
80
|
-
fn(someInterface as Simplify<SomeInterface>); // Good: transform an `interface` into a `type`
|
|
81
|
-
```
|
|
82
|
-
|
|
83
|
-
@link https://github.com/microsoft/TypeScript/issues/15300
|
|
84
|
-
|
|
85
|
-
@category Object
|
|
86
|
-
*/
|
|
87
|
-
type Simplify<T> = {[KeyType in keyof T]: T[KeyType]} & {};
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
Omit any index signatures from the given object type, leaving only explicitly defined properties.
|
|
91
|
-
|
|
92
|
-
This is the counterpart of `PickIndexSignature`.
|
|
93
|
-
|
|
94
|
-
Use-cases:
|
|
95
|
-
- Remove overly permissive signatures from third-party types.
|
|
96
|
-
|
|
97
|
-
This type was taken from this [StackOverflow answer](https://stackoverflow.com/a/68261113/420747).
|
|
98
|
-
|
|
99
|
-
It relies on the fact that an empty object (`{}`) is assignable to an object with just an index signature, like `Record<string, unknown>`, but not to an object with explicitly defined keys, like `Record<'foo' | 'bar', unknown>`.
|
|
100
|
-
|
|
101
|
-
(The actual value type, `unknown`, is irrelevant and could be any type. Only the key type matters.)
|
|
102
|
-
|
|
103
|
-
```
|
|
104
|
-
const indexed: Record<string, unknown> = {}; // Allowed
|
|
105
|
-
|
|
106
|
-
const keyed: Record<'foo', unknown> = {}; // Error
|
|
107
|
-
// => TS2739: Type '{}' is missing the following properties from type 'Record<"foo" | "bar", unknown>': foo, bar
|
|
108
|
-
```
|
|
109
|
-
|
|
110
|
-
Instead of causing a type error like the above, you can also use a [conditional type](https://www.typescriptlang.org/docs/handbook/2/conditional-types.html) to test whether a type is assignable to another:
|
|
111
|
-
|
|
112
|
-
```
|
|
113
|
-
type Indexed = {} extends Record<string, unknown>
|
|
114
|
-
? '✅ `{}` is assignable to `Record<string, unknown>`'
|
|
115
|
-
: '❌ `{}` is NOT assignable to `Record<string, unknown>`';
|
|
116
|
-
// => '✅ `{}` is assignable to `Record<string, unknown>`'
|
|
117
|
-
|
|
118
|
-
type Keyed = {} extends Record<'foo' | 'bar', unknown>
|
|
119
|
-
? "✅ `{}` is assignable to `Record<'foo' | 'bar', unknown>`"
|
|
120
|
-
: "❌ `{}` is NOT assignable to `Record<'foo' | 'bar', unknown>`";
|
|
121
|
-
// => "❌ `{}` is NOT assignable to `Record<'foo' | 'bar', unknown>`"
|
|
122
|
-
```
|
|
123
|
-
|
|
124
|
-
Using a [mapped type](https://www.typescriptlang.org/docs/handbook/2/mapped-types.html#further-exploration), you can then check for each `KeyType` of `ObjectType`...
|
|
125
|
-
|
|
126
|
-
```
|
|
127
|
-
import type {OmitIndexSignature} from 'type-fest';
|
|
128
|
-
|
|
129
|
-
type OmitIndexSignature<ObjectType> = {
|
|
130
|
-
[KeyType in keyof ObjectType // Map each key of `ObjectType`...
|
|
131
|
-
]: ObjectType[KeyType]; // ...to its original value, i.e. `OmitIndexSignature<Foo> == Foo`.
|
|
132
|
-
};
|
|
133
|
-
```
|
|
134
|
-
|
|
135
|
-
...whether an empty object (`{}`) would be assignable to an object with that `KeyType` (`Record<KeyType, unknown>`)...
|
|
136
|
-
|
|
137
|
-
```
|
|
138
|
-
import type {OmitIndexSignature} from 'type-fest';
|
|
139
|
-
|
|
140
|
-
type OmitIndexSignature<ObjectType> = {
|
|
141
|
-
[KeyType in keyof ObjectType
|
|
142
|
-
// Is `{}` assignable to `Record<KeyType, unknown>`?
|
|
143
|
-
as {} extends Record<KeyType, unknown>
|
|
144
|
-
? ... // ✅ `{}` is assignable to `Record<KeyType, unknown>`
|
|
145
|
-
: ... // ❌ `{}` is NOT assignable to `Record<KeyType, unknown>`
|
|
146
|
-
]: ObjectType[KeyType];
|
|
147
|
-
};
|
|
148
|
-
```
|
|
149
|
-
|
|
150
|
-
If `{}` is assignable, it means that `KeyType` is an index signature and we want to remove it. If it is not assignable, `KeyType` is a "real" key and we want to keep it.
|
|
151
|
-
|
|
152
|
-
```
|
|
153
|
-
import type {OmitIndexSignature} from 'type-fest';
|
|
154
|
-
|
|
155
|
-
type OmitIndexSignature<ObjectType> = {
|
|
156
|
-
[KeyType in keyof ObjectType
|
|
157
|
-
as {} extends Record<KeyType, unknown>
|
|
158
|
-
? never // => Remove this `KeyType`.
|
|
159
|
-
: KeyType // => Keep this `KeyType` as it is.
|
|
160
|
-
]: ObjectType[KeyType];
|
|
161
|
-
};
|
|
162
|
-
```
|
|
163
|
-
|
|
164
|
-
@example
|
|
165
|
-
```
|
|
166
|
-
import type {OmitIndexSignature} from 'type-fest';
|
|
167
|
-
|
|
168
|
-
interface Example {
|
|
169
|
-
// These index signatures will be removed.
|
|
170
|
-
[x: string]: any
|
|
171
|
-
[x: number]: any
|
|
172
|
-
[x: symbol]: any
|
|
173
|
-
[x: `head-${string}`]: string
|
|
174
|
-
[x: `${string}-tail`]: string
|
|
175
|
-
[x: `head-${string}-tail`]: string
|
|
176
|
-
[x: `${bigint}`]: string
|
|
177
|
-
[x: `embedded-${number}`]: string
|
|
178
|
-
|
|
179
|
-
// These explicitly defined keys will remain.
|
|
180
|
-
foo: 'bar';
|
|
181
|
-
qux?: 'baz';
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
type ExampleWithoutIndexSignatures = OmitIndexSignature<Example>;
|
|
185
|
-
// => { foo: 'bar'; qux?: 'baz' | undefined; }
|
|
186
|
-
```
|
|
187
|
-
|
|
188
|
-
@see PickIndexSignature
|
|
189
|
-
@category Object
|
|
190
|
-
*/
|
|
191
|
-
type OmitIndexSignature<ObjectType> = {
|
|
192
|
-
[KeyType in keyof ObjectType as {} extends Record<KeyType, unknown>
|
|
193
|
-
? never
|
|
194
|
-
: KeyType]: ObjectType[KeyType];
|
|
195
|
-
};
|
|
196
|
-
|
|
197
|
-
/**
|
|
198
|
-
Allows creating a union type by combining primitive types and literal types without sacrificing auto-completion in IDEs for the literal type part of the union.
|
|
199
|
-
|
|
200
|
-
Currently, when a union type of a primitive type is combined with literal types, TypeScript loses all information about the combined literals. Thus, when such type is used in an IDE with autocompletion, no suggestions are made for the declared literals.
|
|
201
|
-
|
|
202
|
-
This type is a workaround for [Microsoft/TypeScript#29729](https://github.com/Microsoft/TypeScript/issues/29729). It will be removed as soon as it's not needed anymore.
|
|
203
|
-
|
|
204
|
-
@example
|
|
205
|
-
```
|
|
206
|
-
import type {LiteralUnion} from 'type-fest';
|
|
207
|
-
|
|
208
|
-
// Before
|
|
209
|
-
|
|
210
|
-
type Pet = 'dog' | 'cat' | string;
|
|
211
|
-
|
|
212
|
-
const pet: Pet = '';
|
|
213
|
-
// Start typing in your TypeScript-enabled IDE.
|
|
214
|
-
// You **will not** get auto-completion for `dog` and `cat` literals.
|
|
215
|
-
|
|
216
|
-
// After
|
|
217
|
-
|
|
218
|
-
type Pet2 = LiteralUnion<'dog' | 'cat', string>;
|
|
219
|
-
|
|
220
|
-
const pet: Pet2 = '';
|
|
221
|
-
// You **will** get auto-completion for `dog` and `cat` literals.
|
|
222
|
-
```
|
|
223
|
-
|
|
224
|
-
@category Type
|
|
225
|
-
*/
|
|
226
|
-
type LiteralUnion<
|
|
227
|
-
LiteralType,
|
|
228
|
-
BaseType extends Primitive,
|
|
229
|
-
> = LiteralType | (BaseType & Record<never, never>);
|
|
1
|
+
import { Equals, Dict, CamelCase, MaybeArray as MaybeArray$1 } from '@clerc/utils';
|
|
2
|
+
import { OmitIndexSignature, LiteralUnion, Simplify } from 'type-fest';
|
|
230
3
|
|
|
231
4
|
declare const DOUBLE_DASH = "--";
|
|
232
5
|
type TypeFunction<ReturnType = any> = (value: any) => ReturnType;
|
|
@@ -234,54 +7,60 @@ type TypeFunctionArray<ReturnType> = readonly [TypeFunction<ReturnType>];
|
|
|
234
7
|
type FlagType<ReturnType = any> = TypeFunction<ReturnType> | TypeFunctionArray<ReturnType>;
|
|
235
8
|
type FlagSchemaBase<TF> = {
|
|
236
9
|
/**
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
```
|
|
10
|
+
* Type of the flag as a function that parses the argv string and returns the
|
|
11
|
+
* parsed value.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
*
|
|
15
|
+
* ```
|
|
16
|
+
* type: String
|
|
17
|
+
* ```
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* @example Wrap in an array to accept multiple values. `type: [Boolean]`
|
|
21
|
+
*
|
|
22
|
+
* @example Custom function type that uses moment.js to parse string as date.
|
|
23
|
+
* `type: function CustomDate(value: string) { return moment(value).toDate();
|
|
24
|
+
* }`
|
|
253
25
|
*/
|
|
254
26
|
type: TF;
|
|
255
27
|
/**
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
28
|
+
* A single-character alias for the flag.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
*
|
|
32
|
+
* ```
|
|
33
|
+
* alias: 's'
|
|
34
|
+
* ```
|
|
35
|
+
* ```
|
|
262
36
|
*/
|
|
263
37
|
alias?: string;
|
|
264
38
|
} & Record<PropertyKey, unknown>;
|
|
265
39
|
type FlagSchemaDefault<TF, DefaultType = any> = FlagSchemaBase<TF> & {
|
|
266
40
|
/**
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
41
|
+
* Default value of the flag. Also accepts a function that returns the default
|
|
42
|
+
* value. [Default: undefined]
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
*
|
|
46
|
+
* ```
|
|
47
|
+
* default: 'hello'
|
|
48
|
+
* ```
|
|
49
|
+
* ```
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
*
|
|
53
|
+
* ```
|
|
54
|
+
* default: () => [1, 2, 3]
|
|
55
|
+
* ```
|
|
56
|
+
* ```
|
|
278
57
|
*/
|
|
279
58
|
default: DefaultType | (() => DefaultType);
|
|
280
59
|
};
|
|
281
60
|
type FlagSchema<TF = FlagType> = FlagSchemaBase<TF> | FlagSchemaDefault<TF>;
|
|
282
61
|
type FlagTypeOrSchema<ExtraOptions = Record<string, unknown>> = FlagType | (FlagSchema & ExtraOptions);
|
|
283
62
|
type Flags$1<ExtraOptions = Record<string, unknown>> = Record<string, FlagTypeOrSchema<ExtraOptions>>;
|
|
284
|
-
type InferFlagType<Flag extends FlagTypeOrSchema> = Flag extends
|
|
63
|
+
type InferFlagType<Flag extends FlagTypeOrSchema> = Flag extends TypeFunctionArray<infer T> | FlagSchema<TypeFunctionArray<infer T>> ? Flag extends FlagSchemaDefault<TypeFunctionArray<T>, infer D> ? T[] | D : T[] : Flag extends TypeFunction<infer T> | FlagSchema<TypeFunction<infer T>> ? Flag extends FlagSchemaDefault<TypeFunction<T>, infer D> ? T | D : T | undefined : never;
|
|
285
64
|
interface ParsedFlags<Schemas = Record<string, unknown>> {
|
|
286
65
|
flags: Schemas;
|
|
287
66
|
unknownFlags: Record<string, (string | boolean)[]>;
|
|
@@ -293,7 +72,7 @@ type TypeFlag<Schemas extends Flags$1> = ParsedFlags<{
|
|
|
293
72
|
[flag in keyof Schemas]: InferFlagType<Schemas[flag]>;
|
|
294
73
|
}>;
|
|
295
74
|
|
|
296
|
-
type StripBrackets<Parameter extends string> = Parameter extends `<${infer ParameterName}>` | `[${infer ParameterName}]` ?
|
|
75
|
+
type StripBrackets<Parameter extends string> = Parameter extends `<${infer ParameterName}>` | `[${infer ParameterName}]` ? ParameterName extends `${infer SpreadName}...` ? SpreadName : ParameterName : never;
|
|
297
76
|
type ParameterType<Parameter extends string> = Parameter extends `<${infer _ParameterName}...>` | `[${infer _ParameterName}...]` ? string[] : Parameter extends `<${infer _ParameterName}>` ? string : Parameter extends `[${infer _ParameterName}]` ? string | undefined : never;
|
|
298
77
|
type NonNullableParameters<T extends string[] | undefined> = T extends undefined ? [] : NonNullable<T>;
|
|
299
78
|
type TransformParameters<C extends Command> = {
|
|
@@ -313,10 +92,10 @@ type ParseRaw<C extends Command, GF extends GlobalFlagOptions = {}> = NonNullabl
|
|
|
313
92
|
type ParseParameters<C extends Commands = Commands, N extends keyof C = keyof C> = Equals<TransformParameters<C[N]>, {}> extends true ? N extends keyof C ? TransformParameters<C[N]> : Dict<string | string[] | undefined> : TransformParameters<C[N]>;
|
|
314
93
|
|
|
315
94
|
type Locales = Dict<Dict<string>>;
|
|
316
|
-
type
|
|
95
|
+
type TranslateFunction = (name: string, ...arguments_: string[]) => string | undefined;
|
|
317
96
|
interface I18N {
|
|
318
97
|
add: (locales: Locales) => void;
|
|
319
|
-
t:
|
|
98
|
+
t: TranslateFunction;
|
|
320
99
|
}
|
|
321
100
|
|
|
322
101
|
interface Plugin<T extends Clerc = Clerc, U extends Clerc = Clerc> {
|
|
@@ -369,19 +148,19 @@ interface HandlerContext<C extends Commands = Commands, N extends keyof C = keyo
|
|
|
369
148
|
flags: Simplify<ParseFlag<C, N, GF> & Record<string, any>>;
|
|
370
149
|
cli: Clerc<C, GF>;
|
|
371
150
|
}
|
|
372
|
-
type Handler<C extends Commands = Commands, K extends keyof C = keyof C, GF extends GlobalFlagOptions = {}> = (
|
|
373
|
-
type HandlerInCommand<C extends HandlerContext> = (
|
|
151
|
+
type Handler<C extends Commands = Commands, K extends keyof C = keyof C, GF extends GlobalFlagOptions = {}> = (context: HandlerContext<C, K, GF>) => void;
|
|
152
|
+
type HandlerInCommand<C extends HandlerContext> = (context: {
|
|
374
153
|
[K in keyof C]: C[K];
|
|
375
154
|
}) => void;
|
|
376
155
|
type FallbackType<T, U> = {} extends T ? U : T;
|
|
377
156
|
type InspectorContext<C extends Commands = Commands> = HandlerContext<C> & {
|
|
378
157
|
flags: FallbackType<TypeFlag<NonNullable<C[keyof C]["flags"]>>["flags"], Dict<any>>;
|
|
379
158
|
};
|
|
380
|
-
type Inspector<C extends Commands = Commands> =
|
|
381
|
-
type
|
|
159
|
+
type Inspector<C extends Commands = Commands> = InspectorFunction<C> | InspectorObject<C>;
|
|
160
|
+
type InspectorFunction<C extends Commands = Commands> = (context: InspectorContext<C>, next: () => void) => void;
|
|
382
161
|
interface InspectorObject<C extends Commands = Commands> {
|
|
383
162
|
enforce?: "pre" | "post";
|
|
384
|
-
fn:
|
|
163
|
+
fn: InspectorFunction<C>;
|
|
385
164
|
}
|
|
386
165
|
|
|
387
166
|
declare const Root: unique symbol;
|
|
@@ -400,257 +179,278 @@ declare class Clerc<C extends Commands = {}, GF extends GlobalFlagOptions = {}>
|
|
|
400
179
|
/**
|
|
401
180
|
* Create a new cli
|
|
402
181
|
*
|
|
182
|
+
* @example
|
|
183
|
+
*
|
|
184
|
+
* ```ts
|
|
185
|
+
* const cli = Clerc.create();
|
|
186
|
+
* ```
|
|
187
|
+
*
|
|
403
188
|
* @param name
|
|
404
189
|
* @param description
|
|
405
190
|
* @param version
|
|
406
191
|
* @returns
|
|
407
|
-
* @example
|
|
408
|
-
* ```ts
|
|
409
|
-
* const cli = Clerc.create()
|
|
410
|
-
* ```
|
|
411
192
|
*/
|
|
412
193
|
static create(name?: string, description?: string, version?: string): Clerc<{}, {}>;
|
|
413
194
|
/**
|
|
414
195
|
* Set the name of the cli
|
|
415
196
|
*
|
|
416
|
-
* @param name
|
|
417
|
-
* @returns
|
|
418
197
|
* @example
|
|
198
|
+
*
|
|
419
199
|
* ```ts
|
|
420
|
-
* Clerc.create()
|
|
421
|
-
* .name("test")
|
|
200
|
+
* Clerc.create().name("test");
|
|
422
201
|
* ```
|
|
202
|
+
*
|
|
203
|
+
* @param name
|
|
204
|
+
* @returns
|
|
423
205
|
*/
|
|
424
206
|
name(name: string): this;
|
|
425
207
|
/**
|
|
426
208
|
* Set the script name of the cli
|
|
427
209
|
*
|
|
428
|
-
* @param scriptName
|
|
429
|
-
* @returns
|
|
430
210
|
* @example
|
|
211
|
+
*
|
|
431
212
|
* ```ts
|
|
432
|
-
* Clerc.create()
|
|
433
|
-
* .scriptName("test")
|
|
213
|
+
* Clerc.create().scriptName("test");
|
|
434
214
|
* ```
|
|
215
|
+
*
|
|
216
|
+
* @param scriptName
|
|
217
|
+
* @returns
|
|
435
218
|
*/
|
|
436
219
|
scriptName(scriptName: string): this;
|
|
437
220
|
/**
|
|
438
221
|
* Set the description of the cli
|
|
439
222
|
*
|
|
440
|
-
* @param description
|
|
441
|
-
* @returns
|
|
442
223
|
* @example
|
|
224
|
+
*
|
|
443
225
|
* ```ts
|
|
444
|
-
* Clerc.create()
|
|
445
|
-
* .description("test cli")
|
|
226
|
+
* Clerc.create().description("test cli");
|
|
446
227
|
* ```
|
|
228
|
+
*
|
|
229
|
+
* @param description
|
|
230
|
+
* @returns
|
|
447
231
|
*/
|
|
448
232
|
description(description: string): this;
|
|
449
233
|
/**
|
|
450
234
|
* Set the version of the cli
|
|
451
235
|
*
|
|
452
|
-
* @param version
|
|
453
|
-
* @returns
|
|
454
236
|
* @example
|
|
237
|
+
*
|
|
455
238
|
* ```ts
|
|
456
|
-
* Clerc.create()
|
|
457
|
-
* .version("1.0.0")
|
|
239
|
+
* Clerc.create().version("1.0.0");
|
|
458
240
|
* ```
|
|
241
|
+
*
|
|
242
|
+
* @param version
|
|
243
|
+
* @returns
|
|
459
244
|
*/
|
|
460
245
|
version(version: string): this;
|
|
461
246
|
/**
|
|
462
|
-
* Set the Locale
|
|
463
|
-
*
|
|
247
|
+
* Set the Locale You must call this method once after you created the Clerc
|
|
248
|
+
* instance.
|
|
464
249
|
*
|
|
465
|
-
* @param locale
|
|
466
|
-
* @returns
|
|
467
250
|
* @example
|
|
251
|
+
*
|
|
468
252
|
* ```ts
|
|
469
253
|
* Clerc.create()
|
|
470
254
|
* .locale("en")
|
|
471
255
|
* .command(...)
|
|
472
256
|
* ```
|
|
257
|
+
*
|
|
258
|
+
* @param locale
|
|
259
|
+
* @returns
|
|
473
260
|
*/
|
|
474
261
|
locale(locale: string): this;
|
|
475
262
|
/**
|
|
476
|
-
* Set the fallback Locale
|
|
477
|
-
*
|
|
263
|
+
* Set the fallback Locale You must call this method once after you created
|
|
264
|
+
* the Clerc instance.
|
|
478
265
|
*
|
|
479
|
-
* @param fallbackLocale
|
|
480
|
-
* @returns
|
|
481
266
|
* @example
|
|
267
|
+
*
|
|
482
268
|
* ```ts
|
|
483
269
|
* Clerc.create()
|
|
484
270
|
* .fallbackLocale("en")
|
|
485
271
|
* .command(...)
|
|
486
272
|
* ```
|
|
273
|
+
*
|
|
274
|
+
* @param fallbackLocale
|
|
275
|
+
* @returns
|
|
487
276
|
*/
|
|
488
277
|
fallbackLocale(fallbackLocale: string): this;
|
|
489
278
|
/**
|
|
490
279
|
* Register a error handler
|
|
491
280
|
*
|
|
492
|
-
* @param handler
|
|
493
|
-
* @returns
|
|
494
281
|
* @example
|
|
282
|
+
*
|
|
495
283
|
* ```ts
|
|
496
|
-
* Clerc.create()
|
|
497
|
-
*
|
|
284
|
+
* Clerc.create().errorHandler((err) => {
|
|
285
|
+
* console.log(err);
|
|
286
|
+
* });
|
|
498
287
|
* ```
|
|
288
|
+
*
|
|
289
|
+
* @param handler
|
|
290
|
+
* @returns
|
|
499
291
|
*/
|
|
500
|
-
errorHandler(handler: (
|
|
292
|
+
errorHandler(handler: (error: any) => void): this;
|
|
501
293
|
/**
|
|
502
294
|
* Register a command
|
|
503
295
|
*
|
|
504
|
-
* @param name
|
|
505
|
-
* @param description
|
|
506
|
-
* @param options
|
|
507
|
-
* @returns
|
|
508
296
|
* @example
|
|
297
|
+
*
|
|
509
298
|
* ```ts
|
|
510
|
-
* Clerc.create()
|
|
511
|
-
*
|
|
512
|
-
*
|
|
513
|
-
*
|
|
514
|
-
*
|
|
515
|
-
*
|
|
516
|
-
*
|
|
517
|
-
*
|
|
518
|
-
*
|
|
519
|
-
* })
|
|
299
|
+
* Clerc.create().command("test", "test command", {
|
|
300
|
+
* alias: "t",
|
|
301
|
+
* flags: {
|
|
302
|
+
* foo: {
|
|
303
|
+
* alias: "f",
|
|
304
|
+
* description: "foo flag",
|
|
305
|
+
* },
|
|
306
|
+
* },
|
|
307
|
+
* });
|
|
520
308
|
* ```
|
|
309
|
+
*
|
|
521
310
|
* @example
|
|
311
|
+
*
|
|
522
312
|
* ```ts
|
|
523
|
-
* Clerc.create()
|
|
524
|
-
*
|
|
525
|
-
*
|
|
526
|
-
*
|
|
527
|
-
*
|
|
528
|
-
*
|
|
529
|
-
*
|
|
530
|
-
*
|
|
531
|
-
* })
|
|
313
|
+
* Clerc.create().command("", "root", {
|
|
314
|
+
* flags: {
|
|
315
|
+
* foo: {
|
|
316
|
+
* alias: "f",
|
|
317
|
+
* description: "foo flag",
|
|
318
|
+
* },
|
|
319
|
+
* },
|
|
320
|
+
* });
|
|
532
321
|
* ```
|
|
322
|
+
*
|
|
323
|
+
* @param name
|
|
324
|
+
* @param description
|
|
325
|
+
* @param options
|
|
326
|
+
* @returns
|
|
533
327
|
*/
|
|
534
328
|
command<N extends string | RootType, O extends CommandOptions<[...P], A, F>, P extends string[] = string[], A extends MaybeArray$1<string | RootType> = MaybeArray$1<string | RootType>, F extends Flags = Flags>(c: CommandWithHandler<N, O & CommandOptions<[...P], A, F>>): this & Clerc<C & Record<N, Command<N, O>>, GF>;
|
|
535
329
|
command<N extends string | RootType, O extends CommandOptions<[...P], A, F>, P extends string[] = string[], A extends MaybeArray$1<string | RootType> = MaybeArray$1<string | RootType>, F extends Flags = Flags>(name: N, description: string, options?: O & CommandOptions<[...P], A, F>): this & Clerc<C & Record<N, Command<N, O>>, GF>;
|
|
536
330
|
/**
|
|
537
331
|
* Register a global flag
|
|
538
332
|
*
|
|
333
|
+
* @example
|
|
334
|
+
*
|
|
335
|
+
* ```ts
|
|
336
|
+
* Clerc.create().flag("help", "help", {
|
|
337
|
+
* alias: "h",
|
|
338
|
+
* type: Boolean,
|
|
339
|
+
* });
|
|
340
|
+
* ```
|
|
341
|
+
*
|
|
539
342
|
* @param name
|
|
540
343
|
* @param description
|
|
541
344
|
* @param options
|
|
542
345
|
* @returns
|
|
543
|
-
* @example
|
|
544
|
-
* ```ts
|
|
545
|
-
* Clerc.create()
|
|
546
|
-
* .flag("help", "help", {
|
|
547
|
-
* alias: "h",
|
|
548
|
-
* type: Boolean,
|
|
549
|
-
* })
|
|
550
|
-
* ```
|
|
551
346
|
*/
|
|
552
347
|
flag<N extends string, O extends GlobalFlagOption>(name: N, description: string, options: O): this & Clerc<C, GF & Record<N, O>>;
|
|
553
348
|
/**
|
|
554
349
|
* Register a handler
|
|
555
350
|
*
|
|
556
|
-
* @param name
|
|
557
|
-
* @param handler
|
|
558
|
-
* @returns
|
|
559
351
|
* @example
|
|
352
|
+
*
|
|
560
353
|
* ```ts
|
|
561
354
|
* Clerc.create()
|
|
562
|
-
*
|
|
563
|
-
*
|
|
564
|
-
*
|
|
565
|
-
*
|
|
355
|
+
* .command("test", "test command")
|
|
356
|
+
* .on("test", (ctx) => {
|
|
357
|
+
* console.log(ctx);
|
|
358
|
+
* });
|
|
566
359
|
* ```
|
|
360
|
+
*
|
|
361
|
+
* @param name
|
|
362
|
+
* @param handler
|
|
363
|
+
* @returns
|
|
567
364
|
*/
|
|
568
365
|
on<K extends LiteralUnion<keyof CM, string | RootType>, CM extends this["_commands"] = this["_commands"]>(name: K, handler: Handler<CM, K, this["_flags"]>): this;
|
|
569
366
|
/**
|
|
570
367
|
* Use a plugin
|
|
571
368
|
*
|
|
572
|
-
* @param plugin
|
|
573
|
-
* @returns
|
|
574
369
|
* @example
|
|
370
|
+
*
|
|
575
371
|
* ```ts
|
|
576
|
-
* Clerc.create()
|
|
577
|
-
* .use(plugin)
|
|
372
|
+
* Clerc.create().use(plugin);
|
|
578
373
|
* ```
|
|
374
|
+
*
|
|
375
|
+
* @param plugin
|
|
376
|
+
* @returns
|
|
579
377
|
*/
|
|
580
378
|
use<T extends Clerc, U extends Clerc>(plugin: Plugin<T, U>): this & Clerc<C & U["_commands"]> & U;
|
|
581
379
|
/**
|
|
582
380
|
* Register a inspector
|
|
583
381
|
*
|
|
584
|
-
* @param inspector
|
|
585
|
-
* @returns
|
|
586
382
|
* @example
|
|
383
|
+
*
|
|
587
384
|
* ```ts
|
|
588
|
-
* Clerc.create()
|
|
589
|
-
*
|
|
590
|
-
*
|
|
591
|
-
*
|
|
592
|
-
* })
|
|
385
|
+
* Clerc.create().inspector((ctx, next) => {
|
|
386
|
+
* console.log(ctx);
|
|
387
|
+
* next();
|
|
388
|
+
* });
|
|
593
389
|
* ```
|
|
390
|
+
*
|
|
391
|
+
* @param inspector
|
|
392
|
+
* @returns
|
|
594
393
|
*/
|
|
595
394
|
inspector(inspector: Inspector): this;
|
|
596
395
|
/**
|
|
597
396
|
* Parse the command line arguments
|
|
598
397
|
*
|
|
599
|
-
* @param args
|
|
600
|
-
* @param optionsOrArgv
|
|
601
|
-
* @returns
|
|
602
398
|
* @example
|
|
399
|
+
*
|
|
603
400
|
* ```ts
|
|
604
|
-
* Clerc.create()
|
|
605
|
-
* .parse(process.argv.slice(2)) // Optional
|
|
401
|
+
* Clerc.create().parse(process.argv.slice(2)); // Optional
|
|
606
402
|
* ```
|
|
403
|
+
*
|
|
404
|
+
* @param args
|
|
405
|
+
* @param optionsOrArgv
|
|
406
|
+
* @returns
|
|
607
407
|
*/
|
|
608
408
|
parse(optionsOrArgv?: string[] | ParseOptions): this;
|
|
609
409
|
/**
|
|
610
410
|
* Run matched command
|
|
611
411
|
*
|
|
612
|
-
* @returns
|
|
613
412
|
* @example
|
|
413
|
+
*
|
|
614
414
|
* ```ts
|
|
615
|
-
* Clerc.create()
|
|
616
|
-
* .parse({ run: false })
|
|
617
|
-
* .runMatchedCommand()
|
|
415
|
+
* Clerc.create().parse({ run: false }).runMatchedCommand();
|
|
618
416
|
* ```
|
|
417
|
+
*
|
|
418
|
+
* @returns
|
|
619
419
|
*/
|
|
620
420
|
runMatchedCommand(): this;
|
|
621
421
|
}
|
|
622
422
|
|
|
623
423
|
declare class CommandExistsError extends Error {
|
|
624
424
|
commandName: string;
|
|
625
|
-
constructor(commandName: string, t:
|
|
425
|
+
constructor(commandName: string, t: TranslateFunction);
|
|
626
426
|
}
|
|
627
427
|
declare class NoSuchCommandError extends Error {
|
|
628
428
|
commandName: string;
|
|
629
|
-
constructor(commandName: string, t:
|
|
429
|
+
constructor(commandName: string, t: TranslateFunction);
|
|
630
430
|
}
|
|
631
431
|
declare class NoCommandGivenError extends Error {
|
|
632
|
-
constructor(t:
|
|
432
|
+
constructor(t: TranslateFunction);
|
|
633
433
|
}
|
|
634
434
|
declare class CommandNameConflictError extends Error {
|
|
635
435
|
n1: string;
|
|
636
436
|
n2: string;
|
|
637
|
-
constructor(n1: string, n2: string, t:
|
|
437
|
+
constructor(n1: string, n2: string, t: TranslateFunction);
|
|
638
438
|
}
|
|
639
439
|
declare class ScriptNameNotSetError extends Error {
|
|
640
|
-
constructor(t:
|
|
440
|
+
constructor(t: TranslateFunction);
|
|
641
441
|
}
|
|
642
442
|
declare class DescriptionNotSetError extends Error {
|
|
643
|
-
constructor(t:
|
|
443
|
+
constructor(t: TranslateFunction);
|
|
644
444
|
}
|
|
645
445
|
declare class VersionNotSetError extends Error {
|
|
646
|
-
constructor(t:
|
|
446
|
+
constructor(t: TranslateFunction);
|
|
647
447
|
}
|
|
648
448
|
declare class InvalidCommandNameError extends Error {
|
|
649
449
|
commandName: string;
|
|
650
|
-
constructor(commandName: string, t:
|
|
450
|
+
constructor(commandName: string, t: TranslateFunction);
|
|
651
451
|
}
|
|
652
452
|
declare class LocaleNotCalledFirstError extends Error {
|
|
653
|
-
constructor(t:
|
|
453
|
+
constructor(t: TranslateFunction);
|
|
654
454
|
}
|
|
655
455
|
|
|
656
456
|
type MaybeArray<T> = T | T[];
|
|
@@ -660,14 +460,14 @@ declare const defineHandler: <C extends Clerc<{}, {}>, K extends keyof C["_comma
|
|
|
660
460
|
declare const defineInspector: <C extends Clerc<{}, {}>>(_cli: C, inspector: Inspector<C["_commands"]>) => Inspector<C["_commands"]>;
|
|
661
461
|
declare const defineCommand: <N extends string | typeof Root, O extends CommandOptions<[...P], MaybeArray<string | typeof Root>, Flags>, P extends string[]>(command: Command<N, O & CommandOptions<[...P], MaybeArray<string | typeof Root>, Flags>>, handler?: HandlerInCommand<HandlerContext<Record<N, Command<N, O>> & Record<never, never>, N, {}>> | undefined) => CommandWithHandler<N, O & CommandOptions<[...P], MaybeArray<string | typeof Root>, Flags>>;
|
|
662
462
|
|
|
663
|
-
declare function resolveFlattenCommands(commands: Commands, t:
|
|
664
|
-
declare function resolveCommand(commands: Commands, argv: string[], t:
|
|
463
|
+
declare function resolveFlattenCommands(commands: Commands, t: TranslateFunction): Map<string[] | typeof Root, CommandAlias>;
|
|
464
|
+
declare function resolveCommand(commands: Commands, argv: string[], t: TranslateFunction): [Command<string | RootType> | undefined, string[] | RootType | undefined];
|
|
665
465
|
declare const resolveArgv: () => string[];
|
|
666
|
-
declare function compose(inspectors: Inspector[]): (
|
|
466
|
+
declare function compose(inspectors: Inspector[]): (context: InspectorContext) => void;
|
|
667
467
|
declare const isValidName: (name: CommandType) => boolean;
|
|
668
468
|
declare const withBrackets: (s: string, isOptional?: boolean) => string;
|
|
669
469
|
declare const formatCommandName: (name: string | string[] | RootType) => string;
|
|
670
470
|
declare const detectLocale: () => string;
|
|
671
471
|
declare const stripFlags: (argv: string[]) => string[];
|
|
672
472
|
|
|
673
|
-
export { Clerc, Command, CommandAlias, CommandCustomProperties, CommandExistsError, CommandNameConflictError, CommandOptions, CommandType, CommandWithHandler, Commands, DescriptionNotSetError, FallbackType, Flag, FlagOptions, Flags, GlobalFlagOption, GlobalFlagOptions, Handler, HandlerContext, HandlerInCommand, I18N, Inspector, InspectorContext,
|
|
473
|
+
export { Clerc, Command, CommandAlias, CommandCustomProperties, CommandExistsError, CommandNameConflictError, CommandOptions, CommandType, CommandWithHandler, Commands, DescriptionNotSetError, FallbackType, Flag, FlagOptions, Flags, GlobalFlagOption, GlobalFlagOptions, Handler, HandlerContext, HandlerInCommand, I18N, Inspector, InspectorContext, InspectorFunction, InspectorObject, InvalidCommandNameError, LocaleNotCalledFirstError, Locales, MakeEventMap, NoCommandGivenError, NoSuchCommandError, ParseOptions, Plugin, Root, RootType, ScriptNameNotSetError, TranslateFunction, VersionNotSetError, compose, defineCommand, defineHandler, defineInspector, definePlugin, detectLocale, formatCommandName, isValidName, resolveArgv, resolveCommand, resolveFlattenCommands, stripFlags, withBrackets };
|