import {URL} from 'node:url' import {CLIError} from './errors' import {loadHelpClass} from './help' import {BooleanFlag, CustomOptions, FlagDefinition, OptionFlag} from './interfaces' import {dirExists, fileExists} from './util/fs' type NotArray = T extends Array ? never : T /** * Create a custom flag. * * @example * type Id = string * type IdOpts = { startsWith: string; length: number } * * export const myFlag = custom({ * parse: async (input, opts) => { * if (input.startsWith(opts.startsWith) && input.length === opts.length) { * return input * } * * throw new Error('Invalid id') * }, * }) */ export function custom( defaults: Partial> & { multiple: true } & ({default: OptionFlag['default']} | {required: true}), ): FlagDefinition export function custom( defaults: Partial, P>> & { multiple?: false | undefined } & ({default: OptionFlag, P>['default']} | {required: true}), ): FlagDefinition export function custom( defaults: Partial, P>> & { default?: OptionFlag, P>['default'] | undefined multiple?: false | undefined required?: false | undefined }, ): FlagDefinition export function custom( defaults: Partial> & { default?: OptionFlag['default'] | undefined multiple: true required?: false | undefined }, ): FlagDefinition export function custom(): FlagDefinition< T, P, {multiple: false; requiredOrDefaulted: false} > export function custom( defaults?: Partial>, ): FlagDefinition { return (options: any = {}) => ({ parse: async (input, _ctx, _opts) => input, ...defaults, ...options, input: [] as string[], multiple: Boolean(options.multiple === undefined ? (defaults?.multiple ?? false) : options.multiple), type: 'option', }) } /** * A boolean flag. Defaults to `false` unless default is set to `true`. * * - `allowNo` option allows `--no-` prefix to negate boolean flag. */ export function boolean(options: Partial> = {}): BooleanFlag { return { parse: async (b, _) => b, ...options, allowNo: Boolean(options.allowNo), type: 'boolean', } as BooleanFlag } /** * An integer flag. Throws an error if the provided value is not a valid integer. * * - `min` option allows to set a minimum value. * - `max` option allows to set a maximum value. */ export const integer = custom({ async parse(input, _, opts) { if (!/^-?\d+$/.test(input)) throw new CLIError(`Expected an integer but received: ${input}`) const num = Number.parseInt(input, 10) if (opts.min !== undefined && num < opts.min) throw new CLIError(`Expected an integer greater than or equal to ${opts.min} but received: ${input}`) if (opts.max !== undefined && num > opts.max) throw new CLIError(`Expected an integer less than or equal to ${opts.max} but received: ${input}`) return num }, }) /** * A directory flag. * * - `exists` option allows you to throw an error if the directory does not exist. */ export const directory = custom({ async parse(input, _, opts) { if (opts.exists) return dirExists(input) return input }, }) /** * A file flag. * * - `exists` option allows you to throw an error if the file does not exist. */ export const file = custom({ async parse(input, _, opts) { if (opts.exists) return fileExists(input) return input }, }) /** * A URL flag that converts the provided value is a string. * * Throws an error if the string is not a valid URL. */ export const url = custom({ async parse(input) { try { return new URL(input) } catch { throw new CLIError(`Expected a valid url but received: ${input}`) } }, }) /** * A string flag. */ export const string = custom() /** * Version flag that will print the CLI version and exit. */ export const version = (opts: Partial> = {}): BooleanFlag => boolean({ description: 'Show CLI version.', ...opts, async parse(_, ctx) { ctx.log(ctx.config.userAgent) ctx.exit(0) }, }) /** * A help flag that will print the CLI help and exit. */ export const help = (opts: Partial> = {}): BooleanFlag => boolean({ description: 'Show CLI help.', ...opts, async parse(_, cmd) { const Help = await loadHelpClass(cmd.config) await new Help(cmd.config, cmd.config.pjson.oclif.helpOptions ?? cmd.config.pjson.helpOptions).showHelp( cmd.id ? [cmd.id, ...cmd.argv] : cmd.argv, ) cmd.exit(0) }, }) type ReadonlyElementOf> = T[number] /** * Create a custom flag that infers the flag type from the provided options. * * The provided `options` must be a readonly array in order for type inference to work. * * @example * export default class MyCommand extends Command { * static flags = { * name: Flags.option({ * options: ['foo', 'bar'] as const, * })(), * } * } */ export function option( defaults: Partial[], P>> & { multiple: true options: T } & ( | { default: OptionFlag[], P>['default'] | undefined } | {required: true} ), ): FlagDefinition<(typeof defaults.options)[number], P, {multiple: true; requiredOrDefaulted: true}> export function option( defaults: Partial, P>> & { multiple?: false | undefined options: T } & ({default: OptionFlag, P>['default']} | {required: true}), ): FlagDefinition<(typeof defaults.options)[number], P, {multiple: false; requiredOrDefaulted: true}> export function option( defaults: Partial, P>> & { default?: OptionFlag, P>['default'] | undefined multiple?: false | undefined options: T required?: false | undefined }, ): FlagDefinition<(typeof defaults.options)[number], P, {multiple: false; requiredOrDefaulted: false}> export function option( defaults: Partial[], P>> & { default?: OptionFlag[], P>['default'] | undefined multiple: true options: T required?: false | undefined }, ): FlagDefinition<(typeof defaults.options)[number], P, {multiple: true; requiredOrDefaulted: false}> export function option( defaults: Partial, P>> & {options: T}, ): FlagDefinition<(typeof defaults.options)[number], P, {multiple: boolean; requiredOrDefaulted: boolean}> { return (options: any = {}) => ({ parse: async (input, _ctx, _opts) => input, ...defaults, ...options, input: [] as string[], multiple: Boolean(options.multiple === undefined ? defaults.multiple : options.multiple), type: 'option', }) }