cli_utils.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. """Tiny CLI helpers shared across the MiniMax PDF skill scripts.
  2. Each entry-point script in ``scripts/`` builds its argparse parser
  3. through :func:`make_parser` so the help output, default ``--quiet`` /
  4. ``--verbose`` flags, and exit-code conventions stay uniform. The
  5. ``emit`` / ``warn`` / ``fail`` helpers funnel all chatter through a
  6. single place so we can later switch to ``logging`` if needed.
  7. """
  8. from __future__ import annotations
  9. import argparse
  10. import sys
  11. def make_parser(prog: str, description: str) -> argparse.ArgumentParser:
  12. """Build a parser pre-loaded with the project-wide flags."""
  13. parser = argparse.ArgumentParser(prog=prog, description=description)
  14. parser.add_argument(
  15. "--quiet",
  16. action="store_true",
  17. help="Suppress informational output (warnings and errors still print).",
  18. )
  19. return parser
  20. def emit(msg: str, *, quiet: bool = False) -> None:
  21. """Print an informational message unless ``--quiet`` is in effect."""
  22. if not quiet:
  23. print(msg)
  24. def warn(msg: str) -> None:
  25. """Print a warning to stderr; never silenced by ``--quiet``."""
  26. print(f"WARN: {msg}", file=sys.stderr)
  27. def fail(msg: str, code: int = 1) -> None:
  28. """Print an error to stderr and exit with a non-zero status."""
  29. print(f"ERROR: {msg}", file=sys.stderr)
  30. sys.exit(code)