| 1 | import { Command } from "commander"; |
| 2 | |
| 3 | // Function to add common options to any command |
| 4 | export function addCommonOptions(command: Command): Command { |
| 5 | return command |
| 6 | .option( |
| 7 | "--config <path>", |
| 8 | "Configuration file for the assistant (can be a file path or hub slug)", |
| 9 | ) |
| 10 | .option( |
| 11 | "--org <slug>", |
| 12 | "Organization slug to use for this session (supported only in headless mode)", |
| 13 | ) |
| 14 | .option("--readonly", "Start in plan mode (read-only tools)") |
| 15 | .option("--auto", "Start in auto mode (all tools allowed)") |
| 16 | .option("--verbose", "Enable verbose logging") |
| 17 | .option( |
| 18 | "--rule <rule>", |
| 19 | "Add a rule (can be a file path, hub slug, or string content). Can be specified multiple times.", |
| 20 | (value: string, previous: string[] | undefined) => { |
| 21 | const array = Array.isArray(previous) ? previous : []; |
| 22 | array.push(value); |
| 23 | return array; |
| 24 | }, |
| 25 | [] as string[], |
| 26 | ) |
| 27 | .option( |
| 28 | "--mcp <slug>", |
| 29 | "Add an MCP server from the hub (slug in format 'owner/package'). Can be specified multiple times.", |
| 30 | (value: string, previous: string[] | undefined) => { |
| 31 | const array = Array.isArray(previous) ? previous : []; |
| 32 | array.push(value); |
| 33 | return array; |
| 34 | }, |
| 35 | [] as string[], |
| 36 | ) |
| 37 | .option( |
| 38 | "--model <slug>", |
| 39 | "Add a model from the hub (slug in format 'owner/package'). Can be specified multiple times.", |
| 40 | (value: string, previous: string[] | undefined) => { |
| 41 | const array = Array.isArray(previous) ? previous : []; |
| 42 | array.push(value); |
| 43 | return array; |
| 44 | }, |
| 45 | [] as string[], |
| 46 | ) |
| 47 | .option( |
| 48 | "--prompt <prompt>", |
| 49 | "Add to the initial user message (can be a file path, hub slug, or string content). Can be specified multiple times.", |
| 50 | (value: string, previous: string[] | undefined) => { |
| 51 | const array = Array.isArray(previous) ? previous : []; |
| 52 | array.push(value); |
| 53 | return array; |
| 54 | }, |
| 55 | [] as string[], |
| 56 | ) |
| 57 | .option( |
| 58 | "--allow <tool>", |
| 59 | "Allow specified tool (overrides default policies). Can be specified multiple times.", |
| 60 | (value: string, previous: string[] | undefined) => { |
| 61 | const array = Array.isArray(previous) ? previous : []; |
| 62 | array.push(value); |
| 63 | return array; |
| 64 | }, |
| 65 | [] as string[], |
| 66 | ) |
| 67 | .option( |
| 68 | "--ask <tool>", |
| 69 | "Ask for permission before using specified tool (overrides default policies). Can be specified multiple times.", |
| 70 | (value: string, previous: string[] | undefined) => { |
| 71 | const array = Array.isArray(previous) ? previous : []; |
| 72 | array.push(value); |
| 73 | return array; |
| 74 | }, |
| 75 | [] as string[], |
| 76 | ) |
| 77 | .option( |
| 78 | "--exclude <tool>", |
| 79 | "Exclude specified tool from use (overrides default policies). Can be specified multiple times.", |
| 80 | (value: string, previous: string[] | undefined) => { |
| 81 | const array = Array.isArray(previous) ? previous : []; |
| 82 | array.push(value); |
| 83 | return array; |
| 84 | }, |
| 85 | [] as string[], |
| 86 | ); |
| 87 | } |
| 88 | |
| 89 | // Function to merge parent options into subcommand options |
| 90 | export function mergeParentOptions(parentCommand: Command, options: any): any { |
| 91 | const parentOpts = parentCommand.opts(); |
| 92 | const mergedOptions = { ...options }; |
| 93 | |
| 94 | // List of options to inherit from parent if not present in subcommand |
| 95 | const inheritableOptions = [ |
| 96 | "config", |
| 97 | "org", |
| 98 | "readonly", |
| 99 | "auto", |
| 100 | "tools", |
| 101 | "verbose", |
| 102 | "rule", |
| 103 | "mcp", |
| 104 | "model", |
| 105 | "prompt", |
| 106 | "allow", |
| 107 | "ask", |
| 108 | "exclude", |
| 109 | ]; |
| 110 | |
| 111 | for (const optName of inheritableOptions) { |
| 112 | if ( |
| 113 | parentOpts[optName] !== undefined && |
| 114 | mergedOptions[optName] === undefined |
| 115 | ) { |
| 116 | mergedOptions[optName] = parentOpts[optName]; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | return mergedOptions; |
| 121 | } |