This repository has been archived by the owner on Aug 22, 2023. It is now read-only.
/
flags.ts
83 lines (69 loc) · 2.48 KB
/
flags.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import {IConfig} from '@oclif/config'
import * as Parser from '@oclif/parser'
import {Command} from '.'
export type ICompletionContext = {
args?: { [name: string]: string };
flags?: { [name: string]: string };
argv?: string[];
config: IConfig;
}
export type ICompletion = {
skipCache?: boolean;
cacheDuration?: number;
cacheKey?(ctx: ICompletionContext): Promise<string>;
options(ctx: ICompletionContext): Promise<string[]>;
}
export type IOptionFlag<T> = Parser.flags.IOptionFlag<T> & {
completion?: ICompletion;
}
export type IFlag<T> = Parser.flags.IBooleanFlag<T> | IOptionFlag<T>
export type Output = Parser.flags.Output
export type Input<T extends Output> = { [P in keyof T]: IFlag<T[P]> }
export type Definition<T> = {
(options: {multiple: true} & Partial<IOptionFlag<T[]>>): IOptionFlag<T[]>;
(options: ({required: true} | {default: Parser.flags.Default<T>}) & Partial<IOptionFlag<T>>): IOptionFlag<T>;
(options?: Partial<IOptionFlag<T>>): IOptionFlag<T | undefined>;
}
export function build<T>(defaults: {parse: IOptionFlag<T>['parse']} & Partial<IOptionFlag<T>>): Definition<T>
export function build(defaults: Partial<IOptionFlag<string>>): Definition<string>
export function build<T>(defaults: Partial<IOptionFlag<T>>): Definition<T> {
return Parser.flags.build<T>(defaults as any)
}
export function option<T>(options: {parse: IOptionFlag<T>['parse']} & Partial<IOptionFlag<T>>) {
return build<T>(options)()
}
const _enum = <T = string>(opts: Parser.flags.EnumFlagOptions<T>): IOptionFlag<T> => {
return build<T>({
parse(input) {
if (!opts.options.includes(input)) throw new Error(`Expected --${this.name}=${input} to be one of: ${opts.options.join(', ')}`)
return input as unknown as T
},
helpValue: `(${opts.options.join('|')})`,
...opts,
})() as IOptionFlag<T>
}
export {_enum as enum}
const stringFlag = build({})
export {stringFlag as string}
export {boolean, integer} from '@oclif/parser/lib/flags'
export const version = (opts: Partial<Parser.flags.IBooleanFlag<boolean>> = {}) => {
return Parser.flags.boolean({
// char: 'v',
description: 'show CLI version',
...opts,
parse: (_: any, cmd: Command) => {
cmd.log(cmd.config.userAgent)
cmd.exit(0)
},
})
}
export const help = (opts: Partial<Parser.flags.IBooleanFlag<boolean>> = {}) => {
return Parser.flags.boolean({
// char: 'h',
description: 'show CLI help',
...opts,
parse: (_: any, cmd: Command) => {
(cmd as any)._help()
},
})
}