diff --git a/features/main_error_paths.feature b/features/main_error_paths.feature index d262341bb..e7e87960f 100644 --- a/features/main_error_paths.feature +++ b/features/main_error_paths.feature @@ -35,3 +35,9 @@ Feature: CLI main() error handling paths Scenario: _print_basic_help prints without error When I call the main _print_basic_help Then the main _print_basic_help completes ok + + @coverage + Scenario: unknown option triggers UsageError handler with exit code 2 + When I call main with arguments ["plan", "use", "--no-such-flag", "x"] + Then the main cli exit code should be 2 + And the main cli output contains "No such option" diff --git a/features/steps/main_error_paths_steps.py b/features/steps/main_error_paths_steps.py index be461151d..4bb2e70ec 100644 --- a/features/steps/main_error_paths_steps.py +++ b/features/steps/main_error_paths_steps.py @@ -85,6 +85,23 @@ def step_print_basic_help(context: Context) -> None: context.help_output = buf.getvalue() +@when("I call main with arguments {args}") +def step_main_in_process(context: Context, args: str) -> None: + """Call ``main(args)`` in-process so coverage tracks the exception paths.""" + from cleveragents.cli.main import get_err_console, main + + cmd_args = ast.literal_eval(args) if isinstance(args, str) else [] + console = get_err_console() + buf = StringIO() + original_file = console.file + console.file = buf + try: + context.exit_code = main(cmd_args) + finally: + console.file = original_file + context.output = buf.getvalue() + + # --------------------------------------------------------------------------- # Then steps — unique step texts to avoid collisions with other suites # --------------------------------------------------------------------------- diff --git a/src/cleveragents/cli/main.py b/src/cleveragents/cli/main.py index 326ce5526..1045dd754 100644 --- a/src/cleveragents/cli/main.py +++ b/src/cleveragents/cli/main.py @@ -813,10 +813,6 @@ def main(args: list[str] | None = None) -> int: err_console = get_err_console() err_console.print("\n[yellow]Interrupted by user[/yellow]") return 130 - except typer.BadParameter as e: - err_console = get_err_console() - err_console.print(f"Error: {e.format_message()}") - return 2 except Exception as e: if any(c.__name__ == "UsageError" for c in type(e).__mro__) and hasattr( e, "format_message"