// CLI argument parsing for cool-branch import * as path from 'node:path'; declare const __VERSION__: string; /** * Available commands */ export type Command = | 'help' | 'version' | 'list' | 'add' | 'rm' | 'rename' | 'mv' | 'where' | 'last' | 'dirname' | 'config' | 'init' | 'setup'; /** * Copy config modes for .cool-branch directory */ export type CopyConfigMode = 'all' | 'none' | 'local'; /** * Parsed CLI options */ export interface ParsedArgs { command: Command; positional: string | undefined; positional2: string | undefined; // second positional for config base: string; baseExplicit: boolean; // true if --base was explicitly provided force: boolean; setup: string | undefined; noSetup: boolean; copyConfig: CopyConfigMode | undefined; // undefined means use config file or default config: string | undefined; // path to custom config file or directory remote: string | undefined; // custom remote name (default: origin) local: boolean; // for config --local unset: boolean; // for config --unset edit: boolean; // for setup --edit and init --edit pathOnly: boolean; // for setup --path help: boolean; version: boolean; } /** * Default base directory for worktrees */ const DEFAULT_BASE = path.join(process.env['HOME'] ?? '~', '.worktrees'); /** * Parse command line arguments * @param argv Command line arguments (process.argv.slice(2)) * @returns Parsed arguments object */ export function parseArgs(argv: string[]): ParsedArgs { const result: ParsedArgs = { command: 'help', positional: undefined, positional2: undefined, base: DEFAULT_BASE, baseExplicit: false, force: false, setup: undefined, noSetup: false, copyConfig: undefined, config: undefined, remote: undefined, local: false, unset: false, edit: false, pathOnly: false, help: false, version: false, }; const args = [...argv]; const validCommands = [ 'version', 'list', 'add', 'rm', 'rename', 'mv', 'where', 'last', 'dirname', 'config', 'init', 'setup', ]; for (let i = 0; i < args.length; i++) { const arg = args[i] as string; // Handle flags if (arg === '-h' || arg === '--help') { result.help = true; } else if (arg === '-v' || arg === '--version') { result.version = true; } else if (arg === '-f' || arg === '--force') { result.force = true; } else if (arg === '--no-setup') { result.noSetup = true; } else if (arg === '--base') { i++; if (i < args.length) { result.base = args[i] as string; result.baseExplicit = true; } } else if (arg === '--setup') { i++; if (i < args.length) { result.setup = args[i] as string; } } else if (arg === '--copy-config') { i++; if (i < args.length) { const value = args[i] as string; if (value === 'all' || value === 'none' || value === 'local') { result.copyConfig = value; } } } else if (arg === '--config') { i++; if (i < args.length) { result.config = args[i] as string; } } else if (arg === '--remote') { i++; if (i < args.length) { result.remote = args[i] as string; } } else if (arg === '--local') { result.local = true; } else if (arg === '--unset') { result.unset = true; } else if (arg === '--edit') { result.edit = true; } else if (arg === '--path') { result.pathOnly = true; } else if (arg.startsWith('-')) { // Unknown flag - ignore for now } else if (validCommands.includes(arg) && result.command === 'help') { // First non-flag argument that's a valid command result.command = arg as Command; } else if (result.positional === undefined) { // Positional argument (branch-name or folder-name) result.positional = arg; } else if (result.positional2 === undefined) { // Second positional argument (for config ) result.positional2 = arg; } } return result; } /** * Get version (injected at build time) */ export function getVersion(): string { if (typeof __VERSION__ !== 'string') { return 'dev'; } return __VERSION__; } /** * Display help message for a specific command or general help */ export function showHelp(command?: Command): void { switch (command) { case 'list': console.log(`cool-branch list - List worktrees and branches Usage: cool-branch list [options] Options: --base Base directory for worktrees (default: ~/.worktrees) --config Path to config file or directory containing config.json -h, --help Show this help `); break; case 'add': console.log(`cool-branch add - Add a new worktree Usage: cool-branch add [options] [] If no branch name is provided, runs in interactive mode. Options: --base Base directory for worktrees (default: ~/.worktrees) --config Path to config file or directory containing config.json --remote Remote name to fetch from (default: origin) -f, --force Force creation even if branch exists --setup