-
Notifications
You must be signed in to change notification settings - Fork 69
/
flags.ts
42 lines (38 loc) · 1.63 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
import {OptionFlag, BooleanFlag, EnumFlagOptions, Default} from './interfaces'
import {custom, boolean} from './parser'
import Command from './command'
export {boolean, integer, url, directory, file, string, build, option, custom} from './parser'
export function _enum<T = string>(opts: EnumFlagOptions<T> & {multiple: true} & ({required: true} | { default: Default<T[]> })): OptionFlag<T[]>
export function _enum<T = string>(opts: EnumFlagOptions<T> & {multiple: true}): OptionFlag<T[] | undefined>
export function _enum<T = string>(opts: EnumFlagOptions<T> & ({required: true} | { default: Default<T> })): OptionFlag<T>
export function _enum<T = string>(opts: EnumFlagOptions<T>): OptionFlag<T | undefined>
export function _enum<T = string>(opts: EnumFlagOptions<T>): OptionFlag<T> | OptionFlag<T[]> | OptionFlag<T | undefined> | OptionFlag<T[] | undefined> {
return custom<T, EnumFlagOptions<T>>({
async 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,
})()
}
export {_enum as enum}
export const version = (opts: Partial<BooleanFlag<boolean>> = {}) => {
return boolean({
description: 'Show CLI version.',
...opts,
parse: async (_: any, cmd: Command) => {
cmd.log(cmd.config.userAgent)
cmd.exit(0)
},
})
}
export const help = (opts: Partial<BooleanFlag<boolean>> = {}) => {
return boolean({
description: 'Show CLI help.',
...opts,
parse: async (_: any, cmd: Command) => {
(cmd as any)._help()
},
})
}