import process from 'node:process' interface ParsedArgv { args: string[] options: { [k: string]: string | boolean | number } } function isLongOption(arg?: string): boolean { if (!arg) return false return arg.startsWith('--') } function isShortOption(arg: string): boolean { return arg.startsWith('-') && !isLongOption(arg) } function parseValue(value: string, coerceValues = true): string | boolean | number { if (!coerceValues) return value if (value === 'true') return true if (value === 'false') return false const numberValue = Number.parseFloat(value) if (!Number.isNaN(numberValue)) return numberValue return value.replace(/"/g, '') } function parseLongOption( arg: string, argv: string[], index: number, options: { [k: string]: string | boolean | number }, coerceValues: boolean, ): number { const [key, ...parts] = arg.slice(2).split('=') const value = parts.length ? parts.join('=') : undefined if (value !== undefined) { options[key as string] = parseValue(value, coerceValues) } else if (index + 1 < argv.length && !argv[index + 1]?.startsWith('-')) { options[key as string] = argv[index + 1] as string index++ } else { options[key as string] = true } return index } function parseShortOption( arg: string, argv: string[], index: number, options: { [k: string]: string | boolean | number }, coerceValues: boolean, ): number { const [key, ...parts] = arg.slice(1).split('=') const value = parts.length ? parts.join('=') : undefined // Check if key is undefined and handle it if (key === undefined) return index if (value !== undefined && key !== undefined) { for (let j = 0; j < key.length; j++) options[key[j] as string] = parseValue(value, coerceValues) } else { for (let j = 0; j < key.length; j++) { if (index + 1 < argv.length && j === key.length - 1 && !argv[index + 1]?.startsWith('-')) { options[key[j] as string] = parseValue(argv[index + 1] as string, coerceValues) index++ } else { options[key[j] as string] = true } } } return index } /** Set coerceValues to false when option values are identities rather than scalars. */ export function parseArgv(argv?: string[], config: { coerceValues?: boolean } = {}): ParsedArgv { if (argv === undefined) argv = process.argv.slice(2) const args: string[] = [] const options: { [k: string]: string | boolean | number } = {} for (let i = 0; i < argv.length; i++) { const arg = argv[i] if (!arg) continue if (isLongOption(arg)) i = parseLongOption(arg, argv, i, options, config.coerceValues !== false) else if (isShortOption(arg)) i = parseShortOption(arg, argv, i, options, config.coerceValues !== false) else args.push(arg) } return { args, options } } export function parseArgs(argv?: string[]): string[] { if (argv === undefined) argv = process.argv.slice(2) return parseArgv(argv).args } interface CliOptions { dryRun?: boolean quiet?: boolean verbose?: boolean [k: string]: string | boolean | number | undefined } export function parseOptions(options?: CliOptions): CliOptions { options = options || {} const defaults = { dryRun: false, quiet: false, verbose: false } const args = process.argv.slice(2) for (let i = 0; i < args.length; i++) { const arg = args[i] if (arg?.startsWith('--')) { const key = arg.substring(2) // remove the -- const camelCaseKey = key.replace( /-([a-z])/gi, g => (g[1] ? g[1].toUpperCase() : ''), // convert kebab-case to camelCase ) if (i + 1 < args.length && !args?.[i + 1]?.startsWith('--')) { // if the next arg exists and is not an option if (args?.[i + 1] === 'true' || args?.[i + 1] === 'false') { // if the next arg is a boolean options[camelCaseKey] = args[i + 1] === 'true' // set the value to the boolean i++ } else { options[camelCaseKey] = args[i + 1] i++ } } else { options[camelCaseKey] = true } } } if (Object.keys(options).length === 0) // if options has no keys, return an empty object return {} return { ...defaults, ...options } } // interface BuddyOptions { // dryRun?: boolean // verbose?: boolean // } /** * Options as argv, one entry per token, for a child process spawned without a * shell. * * `runAction` used to join these into one string, and `runCommand` splits a * string on whitespace with no shell to honour quotes. So an option value * with a space - `--address-line1 12 St James Sq` - reached the action as * `--address-line1 12` plus three stray arguments, and an action path inside a * directory with a space in its name was cut in two. As an array, every value * stays one argument whatever it contains. * * `false`, `undefined` and `null` are omitted, as they always were: several * actions test for a flag with `argv.includes('--fresh')`, which would read a * passed `--fresh false` as set. Values that are not primitives - an `env` * object, a stream - are runner settings, not flags, and are omitted too * rather than sent as `[object Object]`. */ export function buddyOptionArgs(options: Record): string[] { const args: string[] = [] for (const [key, value] of Object.entries(options)) { // cac reserves `--` for arguments after the option separator and some // callers use `_` for positional arguments. Neither is a CLI option. if (key === '--' || key === '_' || key === 'forwardSignals' || value === false || value === undefined || value === null) continue if (typeof value === 'object' && !Array.isArray(value)) continue if (typeof value === 'function' || typeof value === 'symbol') continue if (value === true) args.push(`--${key}`) else args.push(`--${key}`, String(value)) } return args } export function buddyOptions(options?: string[] | Record): string { if (Array.isArray(options)) { options = Array.from(new Set(options)) as string[] if (Array.isArray(options) && options[0] && !(options[0] as string).startsWith('-')) options.shift() return options.join(' ') } if (typeof options === 'object' && options !== null) return buddyOptionArgs(options).join(' ') return buddyOptions(process.argv.slice(2)) }