/** * Command Execution Contract * * Standardized lifecycle: parse -> validate -> execute -> render * for CLI command handlers. */ export interface CommandExecutionContract { parse(rawArgs: string[]): TParsedArgs; validate(parsedArgs: TParsedArgs): void | Promise; execute(parsedArgs: TParsedArgs): Promise | TExecutionResult; render( result: TExecutionResult, context: { rawArgs: string[]; parsedArgs: TParsedArgs } ): Promise | void; } /** * Run a command through the standard lifecycle. */ export async function runCommandWithContract( rawArgs: string[], contract: CommandExecutionContract ): Promise<{ parsedArgs: TParsedArgs; result: TExecutionResult }> { const parsedArgs = contract.parse(rawArgs); await contract.validate(parsedArgs); const result = await contract.execute(parsedArgs); await contract.render(result, { rawArgs, parsedArgs }); return { parsedArgs, result }; }