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