( parsed: RawParsedArgs, options?: FinalizeArgsOptions, )
| 138 | } |
| 139 | |
| 140 | export function finalizeParsedArgs( |
| 141 | parsed: RawParsedArgs, |
| 142 | options?: FinalizeArgsOptions, |
| 143 | ): ParsedArgs { |
| 144 | const strictFlags = options?.strictFlags ?? true; |
| 145 | const warnings = [...parsed.warnings]; |
| 146 | const flags = mergeDefinedFlags( |
| 147 | { json: false, help: false, version: false } as CliFlags, |
| 148 | options?.defaultFlags ?? {}, |
| 149 | ); |
| 150 | mergeDefinedFlags(flags, parsed.flags); |
| 151 | const disallowed = parsed.providedFlags.filter( |
| 152 | (entry) => !isFlagSupportedForCommand(entry.key, parsed.command), |
| 153 | ); |
| 154 | if (disallowed.length > 0) { |
| 155 | const unsupported = disallowed.map((entry) => entry.token); |
| 156 | const message = formatUnsupportedFlagMessage(parsed.command, unsupported); |
| 157 | if (strictFlags) { |
| 158 | throw new AppError('INVALID_ARGS', message); |
| 159 | } |
| 160 | warnings.push(message); |
| 161 | for (const entry of disallowed) { |
| 162 | delete (flags as Record<string, unknown>)[entry.key]; |
| 163 | } |
| 164 | } |
| 165 | for (const key of Object.keys(flags) as FlagKey[]) { |
| 166 | if (flags[key] === undefined) continue; |
| 167 | if (!isFlagSupportedForCommand(key, parsed.command)) { |
| 168 | delete (flags as Record<string, unknown>)[key]; |
| 169 | } |
| 170 | } |
| 171 | assertNoConflictingBackModeFlags(parsed); |
| 172 | applyCommandDefaults(parsed.command, flags); |
| 173 | if (parsed.command === 'batch') { |
| 174 | const stepSourceCount = (flags.steps ? 1 : 0) + (flags.stepsFile ? 1 : 0); |
| 175 | if (stepSourceCount !== 1) { |
| 176 | throw new AppError( |
| 177 | 'INVALID_ARGS', |
| 178 | 'batch requires exactly one step source: --steps or --steps-file.', |
| 179 | ); |
| 180 | } |
| 181 | } |
| 182 | return normalizeParsedCommandAliases({ |
| 183 | command: parsed.command, |
| 184 | positionals: parsed.positionals, |
| 185 | flags, |
| 186 | warnings, |
| 187 | }); |
| 188 | } |
| 189 | |
| 190 | function assertNoConflictingBackModeFlags(parsed: RawParsedArgs): void { |
| 191 | if (parsed.command !== 'back') return; |
no test coverage detected