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