| 1 | export interface CommandLineArgs { |
| 2 | configPath?: string; |
| 3 | organizationSlug?: string; // Organization slug to use for this session |
| 4 | prompt?: string; // Optional prompt argument |
| 5 | resume?: boolean; // Resume from last session |
| 6 | fork?: string; // Fork from an existing session ID |
| 7 | readonly?: boolean; // Start in plan mode (backward compatibility) |
| 8 | rules?: string[]; // Array of rule specifications |
| 9 | mcps?: string[]; // Array of MCP server slugs from the hub |
| 10 | models?: string[]; // Array of model slugs from the hub |
| 11 | prompts?: string[]; // Array of prompt slugs from the hub |
| 12 | format?: "json"; // Output format for headless mode |
| 13 | } |
| 14 | |
| 15 | // Re-export hub loader functions for backward compatibility |
| 16 | export { loadMcpFromHub, processRule } from "./hubLoader.js"; |
| 17 | // Alias processRule as processPromptOrRule for backward compatibility |
| 18 | export { processRule as processPromptOrRule } from "./hubLoader.js"; |
| 19 | |
| 20 | /** |
| 21 | * Extract values for a specific flag that can appear multiple times |
| 22 | */ |
| 23 | function extractMultipleFlags(args: string[], flagName: string): string[] { |
| 24 | const indices: number[] = []; |
| 25 | for (let i = 0; i < args.length; i++) { |
| 26 | if (args[i] === flagName) { |
| 27 | indices.push(i); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | const values: string[] = []; |
| 32 | for (const index of indices) { |
| 33 | if (index + 1 < args.length && !args[index + 1].startsWith("--")) { |
| 34 | values.push(args[index + 1]); |
| 35 | } |
| 36 | } |
| 37 | return values; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Extract value for a single-value flag |
| 42 | */ |
| 43 | function extractSingleFlag( |
| 44 | args: string[], |
| 45 | flagName: string, |
| 46 | ): string | undefined { |
| 47 | const index = args.indexOf(flagName); |
| 48 | if ( |
| 49 | index !== -1 && |
| 50 | index + 1 < args.length && |
| 51 | !args[index + 1].startsWith("--") |
| 52 | ) { |
| 53 | return args[index + 1]; |
| 54 | } |
| 55 | return undefined; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Parse command line arguments for non-permission related flags |
| 60 | * @deprecated Most functionality has been moved to services. This is only kept for backward compatibility. |
| 61 | * @returns Parsed command line arguments |
| 62 | */ |
| 63 | export function parseArgs(): CommandLineArgs { |
| 64 | const args = process.argv.slice(2); |
| 65 | const result: CommandLineArgs = {}; |
| 66 | |
| 67 | // Parse boolean flags |
| 68 | if (args.includes("--resume")) { |
| 69 | result.resume = true; |
| 70 | } |
| 71 | if (args.includes("--readonly")) { |
| 72 | result.readonly = true; |
| 73 | } |
| 74 | |
| 75 | // Parse fork flag |
| 76 | const forkValue = extractSingleFlag(args, "--fork"); |
| 77 | if (forkValue) { |
| 78 | result.fork = forkValue; |
| 79 | } |
| 80 | |
| 81 | // Parse single-value flags |
| 82 | const formatValue = extractSingleFlag(args, "--format"); |
| 83 | if (formatValue === "json") { |
| 84 | result.format = "json"; |
| 85 | } |
| 86 | |
| 87 | result.configPath = extractSingleFlag(args, "--config"); |
| 88 | |
| 89 | const orgValue = extractSingleFlag(args, "--org"); |
| 90 | if (orgValue) { |
| 91 | result.organizationSlug = |
| 92 | orgValue.toLowerCase() === "personal" ? undefined : orgValue; |
| 93 | } |
| 94 | |
| 95 | // Parse multi-value flags |
| 96 | const rules = extractMultipleFlags(args, "--rule"); |
| 97 | if (rules.length > 0) { |
| 98 | result.rules = rules; |
| 99 | } else if (args.includes("--rule")) { |
| 100 | result.rules = []; |
| 101 | } |
| 102 | |
| 103 | const mcps = extractMultipleFlags(args, "--mcp"); |
| 104 | if (mcps.length > 0) { |
| 105 | result.mcps = mcps; |
| 106 | } else if (args.includes("--mcp")) { |
| 107 | // Ensure we have an empty array if --mcp flag is present but has no values |
| 108 | result.mcps = []; |
| 109 | } |
| 110 | |
| 111 | const models = extractMultipleFlags(args, "--model"); |
| 112 | if (models.length > 0) { |
| 113 | result.models = models; |
| 114 | } else if (args.includes("--model")) { |
| 115 | result.models = []; |
| 116 | } |
| 117 | |
| 118 | const prompts = extractMultipleFlags(args, "--prompt"); |
| 119 | if (prompts.length > 0) { |
| 120 | result.prompts = prompts; |
| 121 | } else if (args.includes("--prompt")) { |
| 122 | result.prompts = []; |
| 123 | } |
| 124 | |
| 125 | // Extract prompt from non-flag arguments |
| 126 | const flagsWithValues = [ |
| 127 | "--config", |
| 128 | "--org", |
| 129 | "--rule", |
| 130 | "--mcp", |
| 131 | "--model", |
| 132 | "--prompt", |
| 133 | "--format", |
| 134 | "--fork", |
| 135 | ]; |
| 136 | const nonFlagArgs = args.filter((arg, index) => { |
| 137 | if (arg.startsWith("--") || arg === "-p") return false; |
| 138 | const prevArg = index > 0 ? args[index - 1] : ""; |
| 139 | return !flagsWithValues.includes(prevArg); |
| 140 | }); |
| 141 | |
| 142 | if (nonFlagArgs.length > 0) { |
| 143 | result.prompt = nonFlagArgs[nonFlagArgs.length - 1]; |
| 144 | } |
| 145 | |
| 146 | return result; |
| 147 | } |