/**
 * common-flags.mjs — shared argument-parsing helpers for new subcommand
 * modules (bootstrap, personas, create-docs). Extracted to eliminate the
 * three byte-identical copies that previously lived in each command file.
 *
 * These helpers are intentionally minimal — no option parsing library
 * required for a well-scoped CLI surface.
 */

import { resolve } from 'node:path'

/**
 * Resolve the first positional argument as a project directory, defaulting to
 * `process.cwd()`. Flag arguments (prefixed `--`) are skipped.
 *
 * @param {string[]} args
 * @returns {string}
 */
export function resolveProjectDir(args) {
	const positional = args.find((a) => !a.startsWith('--'))
	return resolve(positional && positional.length > 0 ? positional : '.')
}

/**
 * Pull the string value that follows a named flag, e.g. `--mode extend` → `'extend'`.
 * Returns `null` when the flag is absent or has no following argument.
 *
 * @param {string[]} args
 * @param {string} flag
 * @returns {string | null}
 */
export function getFlagValue(args, flag) {
	for (let i = 0; i < args.length; i++) {
		if (args[i] === flag && args[i + 1] !== undefined) return args[i + 1]
	}
	return null
}
