File size: 1,600 Bytes
e1ced61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"""Shared helper for command-line entry points that are not yet implemented.

A phase-P0 build wires every declared console entry point so that ``--help``
documents the intended interface, but any actual dispatch fails loudly with a
non-zero exit. This is deliberately *not* a silent success: the runbook forbids
pretending a command works before it is implemented.
"""

from __future__ import annotations

import sys
from typing import Any


def not_implemented(prog: str, subcommands: dict[str, str]) -> int:
    """Print a not-implemented message and return a non-zero exit code.

    ``subcommands`` maps subcommand name to a one-line description so the
    planned interface remains visible in the error output.
    """
    print(f"{prog}: no subcommand is implemented in this build.", file=sys.stderr)
    print("Planned subcommands:", file=sys.stderr)
    for name, description in subcommands.items():
        print(f"  {name:<22} {description}", file=sys.stderr)
    print(
        "Implementing these is tracked in docs/07_IMPLEMENTATION_TASKS.md.",
        file=sys.stderr,
    )
    return 2


def dispatch(
    prog: str,
    argv: list[str] | None,
    subcommands: dict[str, str],
    implementations: dict[str, Any] | None = None,
) -> int:
    """Dispatch to an implemented subcommand or report not-implemented."""
    implementations = implementations or {}
    if not argv:
        return not_implemented(prog, subcommands)
    command = argv[0]
    if command in implementations:
        return int(implementations[command](argv[1:]))
    return not_implemented(prog, subcommands)