Skip to content

Refactor: cli.main if/elif chain → command dispatch table #7

@JRS1986

Description

@JRS1986

Context

cli.py:90-196 is a 100-line if/elif cascade dispatching on args.command. Each branch is short on its own, but the function mixes argument access, business logic, and printing. Adding a new command requires editing two unrelated places (parser definition + dispatch chain), and there's no isolation between handlers.

Recommendation

Move each command into its own function and use a dispatch dict:

COMMANDS: dict[str, Callable[[argparse.Namespace], int]] = {
    \"probe\": _cmd_probe,
    \"doctor\": _cmd_doctor,
    \"credentials\": _cmd_credentials,
    \"skill\": _cmd_skill,
    \"knowledge\": _cmd_knowledge,
    \"orchestrate\": _cmd_orchestrate,
    \"adapt\": _cmd_adapt,
    \"route\": _cmd_route,
    \"select-model\": _cmd_select_model,
    \"workflow\": _cmd_workflow,
    \"init\": _cmd_init_or_wizard,
    \"wizard\": _cmd_init_or_wizard,
}

def main(argv: list[str] | None = None) -> int:
    args = build_parser().parse_args(argv)
    handler = COMMANDS.get(args.command)
    return handler(args) if handler else 2

Each _cmd_* is independently testable.

Verification

  • main() shrinks to <10 lines.
  • Each command handler is callable directly from a test (some already are via the existing tests/test_cli.py).
  • All 51 tests still pass.

Carry-over from prior code review (F11).

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions