@@ -18,6 +18,7 @@ export interface IBooleanFlag<T> extends IFlagBase<T, boolean> {
18
18
19
19
export interface IOptionFlag < T > extends IFlagBase < T , string > {
20
20
type : 'option'
21
+ optionType : string
21
22
default ?: T | ( ( context : DefaultContext < T > ) => T | undefined )
22
23
multiple : boolean
23
24
input : string [ ]
@@ -29,6 +30,12 @@ export interface Definition<T> {
29
30
( options ?: Partial < IOptionFlag < T > > ) : IOptionFlag < T | undefined >
30
31
}
31
32
33
+ export interface EnumFlagOptions < T > extends Partial < IOptionFlag < T > > {
34
+ options : string [ ]
35
+ }
36
+
37
+ export type IFlag < T > = IBooleanFlag < T > | IOptionFlag < T >
38
+
32
39
export function build < T > ( defaults : { parse : IOptionFlag < T > [ 'parse' ] } & Partial < IOptionFlag < T > > ) : Definition < T >
33
40
export function build ( defaults : Partial < IOptionFlag < string > > ) : Definition < string >
34
41
export function build < T > ( defaults : Partial < IOptionFlag < T > > ) : Definition < T > {
@@ -44,8 +51,6 @@ export function build<T>(defaults: Partial<IOptionFlag<T>>): Definition<T> {
44
51
}
45
52
}
46
53
47
- export type IFlag < T > = IBooleanFlag < T > | IOptionFlag < T >
48
-
49
54
export function boolean < T = boolean > ( options : Partial < IBooleanFlag < T > > = { } ) : IBooleanFlag < T > {
50
55
return {
51
56
parse : b => b ,
@@ -56,30 +61,28 @@ export function boolean<T = boolean>(options: Partial<IBooleanFlag<T>> = {}): IB
56
61
}
57
62
58
63
export const integer = build ( {
64
+ optionType : 'integer' ,
59
65
parse : input => {
60
66
if ( ! / ^ [ 0 - 9 ] + $ / . test ( input ) ) throw new Error ( `Expected an integer but received: ${ input } ` )
61
67
return parseInt ( input , 10 )
62
68
} ,
63
69
} )
64
70
65
- export interface EnumFlagOptions < T > extends Partial < IOptionFlag < T > > {
66
- options : string [ ]
67
- }
68
-
69
71
const _enum = < T = string > ( opts : EnumFlagOptions < T > ) => build < T > ( {
70
72
parse ( input ) {
71
73
if ( ! opts . options . includes ( input ) ) throw new Error ( `Expected --${ this . name } =${ input } to be one of: ${ opts . options . join ( ', ' ) } ` )
72
74
return input
73
75
} ,
74
76
...opts as any ,
77
+ optionType : 'enum' ,
75
78
} )
76
79
export { _enum as enum }
77
80
78
81
export function option < T > ( options : { parse : IOptionFlag < T > [ 'parse' ] } & Partial < IOptionFlag < T > > ) {
79
- return build < T > ( options ) ( )
82
+ return build < T > ( { optionType : 'custom' , ... options } ) ( )
80
83
}
81
84
82
- const stringFlag = build ( { } )
85
+ const stringFlag = build ( { optionType : 'string' } )
83
86
export { stringFlag as string }
84
87
85
88
export const defaultFlags = {
0 commit comments