| 1 | /** |
| 2 | * Centralized command line flag validation |
| 3 | * Provides consistent validation across all commands and proper error reporting |
| 4 | */ |
| 5 | |
| 6 | import { safeStderr } from "../init.js"; |
| 7 | |
| 8 | export interface ValidationOptions { |
| 9 | // Root command specific flags |
| 10 | print?: boolean; |
| 11 | format?: string; |
| 12 | silent?: boolean; |
| 13 | |
| 14 | // Common flags |
| 15 | readonly?: boolean; |
| 16 | auto?: boolean; |
| 17 | config?: string; |
| 18 | |
| 19 | // Permission flags |
| 20 | allow?: string[]; |
| 21 | ask?: string[]; |
| 22 | exclude?: string[]; |
| 23 | |
| 24 | // Command context |
| 25 | isRootCommand?: boolean; |
| 26 | commandName?: string; |
| 27 | } |
| 28 | |
| 29 | export interface ValidationError { |
| 30 | message: string; |
| 31 | code: string; |
| 32 | } |
| 33 | |
| 34 | export interface ValidationResult { |
| 35 | isValid: boolean; |
| 36 | errors: ValidationError[]; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Validate that --format flag only works with -p/--print |
| 41 | */ |
| 42 | function validateFormatFlag(options: ValidationOptions): ValidationError[] { |
| 43 | const errors: ValidationError[] = []; |
| 44 | |
| 45 | if (options.format && !options.print) { |
| 46 | errors.push({ |
| 47 | code: "FORMAT_REQUIRES_PRINT", |
| 48 | message: "Error: --format flag can only be used with -p/--print flag", |
| 49 | }); |
| 50 | } |
| 51 | |
| 52 | return errors; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Validate that --silent flag only works with -p/--print |
| 57 | */ |
| 58 | function validateSilentFlag(options: ValidationOptions): ValidationError[] { |
| 59 | const errors: ValidationError[] = []; |
| 60 | |
| 61 | if (options.silent && !options.print) { |
| 62 | errors.push({ |
| 63 | code: "SILENT_REQUIRES_PRINT", |
| 64 | message: "Error: --silent flag can only be used with -p/--print flag", |
| 65 | }); |
| 66 | } |
| 67 | |
| 68 | return errors; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Validate format value is supported |
| 73 | */ |
| 74 | function validateFormatValue(options: ValidationOptions): ValidationError[] { |
| 75 | const errors: ValidationError[] = []; |
| 76 | |
| 77 | if (options.format && options.format !== "json") { |
| 78 | errors.push({ |
| 79 | code: "INVALID_FORMAT_VALUE", |
| 80 | message: "Error: --format currently only supports 'json'", |
| 81 | }); |
| 82 | } |
| 83 | |
| 84 | return errors; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Validate that --readonly and --auto are mutually exclusive |
| 89 | * This validation is now handled in flagProcessor.ts, but we keep it here for completeness |
| 90 | */ |
| 91 | function validateModeFlags(options: ValidationOptions): ValidationError[] { |
| 92 | const errors: ValidationError[] = []; |
| 93 | |
| 94 | if (options.readonly && options.auto) { |
| 95 | errors.push({ |
| 96 | code: "CONFLICTING_MODE_FLAGS", |
| 97 | message: "Error: Cannot use both --readonly and --auto flags together", |
| 98 | }); |
| 99 | } |
| 100 | |
| 101 | return errors; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Validate config path exists (basic check) |
| 106 | */ |
| 107 | function validateConfigPath(options: ValidationOptions): ValidationError[] { |
| 108 | const errors: ValidationError[] = []; |
| 109 | |
| 110 | // Note: We don't check file existence here since the config loader handles that |
| 111 | // and provides better error messages. This is just for basic validation. |
| 112 | if (options.config && typeof options.config !== "string") { |
| 113 | errors.push({ |
| 114 | code: "INVALID_CONFIG_PATH", |
| 115 | message: "Error: --config must be a valid path", |
| 116 | }); |
| 117 | } |
| 118 | |
| 119 | return errors; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Validate permission flag combinations |
| 124 | */ |
| 125 | function validatePermissionFlags( |
| 126 | options: ValidationOptions, |
| 127 | ): ValidationError[] { |
| 128 | const errors: ValidationError[] = []; |
| 129 | |
| 130 | // Check for empty permission arrays (likely user error) |
| 131 | if (options.allow?.some((tool) => !tool.trim())) { |
| 132 | errors.push({ |
| 133 | code: "EMPTY_ALLOW_TOOL", |
| 134 | message: "Error: --allow requires a tool name (e.g., --allow readFile)", |
| 135 | }); |
| 136 | } |
| 137 | |
| 138 | if (options.ask?.some((tool) => !tool.trim())) { |
| 139 | errors.push({ |
| 140 | code: "EMPTY_ASK_TOOL", |
| 141 | message: "Error: --ask requires a tool name (e.g., --ask writeFile)", |
| 142 | }); |
| 143 | } |
| 144 | |
| 145 | if (options.exclude?.some((tool) => !tool.trim())) { |
| 146 | errors.push({ |
| 147 | code: "EMPTY_EXCLUDE_TOOL", |
| 148 | message: "Error: --exclude requires a tool name (e.g., --exclude Write)", |
| 149 | }); |
| 150 | } |
| 151 | |
| 152 | return errors; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Run all validation checks on the provided options |
| 157 | */ |
| 158 | export function validateFlags(options: ValidationOptions): ValidationResult { |
| 159 | const allErrors: ValidationError[] = []; |
| 160 | |
| 161 | // Run all validation functions |
| 162 | const validators = [ |
| 163 | validateFormatFlag, |
| 164 | validateSilentFlag, |
| 165 | validateFormatValue, |
| 166 | validateModeFlags, |
| 167 | validateConfigPath, |
| 168 | validatePermissionFlags, |
| 169 | ]; |
| 170 | |
| 171 | for (const validator of validators) { |
| 172 | allErrors.push(...validator(options)); |
| 173 | } |
| 174 | |
| 175 | return { |
| 176 | isValid: allErrors.length === 0, |
| 177 | errors: allErrors, |
| 178 | }; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Display validation errors and exit process |
| 183 | * Provides consistent error formatting across all commands |
| 184 | */ |
| 185 | export function handleValidationErrors(errors: ValidationError[]): never { |
| 186 | for (const error of errors) { |
| 187 | safeStderr(error.message + "\n"); |
| 188 | } |
| 189 | |
| 190 | if (errors.length > 1) { |
| 191 | safeStderr(`\nFound ${errors.length} validation errors.\n`); |
| 192 | } |
| 193 | |
| 194 | process.exit(1); |
| 195 | } |