main.py
1"""Stable CLI entry point: parse, dispatch, and report expected errors."""23from __future__ import annotations45import argparse6from pathlib import Path7import sys8from typing import Optional, Sequence9from sqlalchemy.exc import SQLAlchemyError1011from sim_platform import __version__12from sim_platform.diagnostics import LEVELS, configure_logging13from sim_platform.domain.models import ContractError14from .context import CliContext15from .commands import register_commands161718def parser() -> argparse.ArgumentParser:19 root = argparse.ArgumentParser(20 description="Local simulation accounts, market playback and simulated execution",21 )22 root.add_argument("--version", action="version", version="Market Lab " + __version__)23 root.add_argument(24 "--data-dir", type=Path, help="absolute override; default is OS App data directory"25 )26 root.add_argument(27 "--log-level",28 choices=LEVELS,29 help="diagnostic detail on standard error; default MARKET_LAB_LOG_LEVEL or warning",30 )31 root.add_argument(32 "--log-json", action="store_true", help="write diagnostics as one JSON object per line"33 )34 commands = root.add_subparsers(dest="command", required=True)35 register_commands(commands)36 return root373839def main(argv: Optional[Sequence[str]] = None) -> int:40 args = parser().parse_args(argv)41 try:42 configure_logging(args.log_level, json_format=True if args.log_json else None)43 with CliContext(data_dir=args.data_dir) as context:44 return args.handler(args, context)45 except (ContractError, OSError, ValueError, SQLAlchemyError) as exc:46 print(f"Platform error: {exc}", file=sys.stderr)47 return 1