Source code for spacr.__main__

"""Command-line entry point for ``python -m spacr``.

The module builds and dispatches spaCR's command-line subcommands. Every
window command opens the PySide6 application; with no subcommand it opens on
the Home tab.

Copyright © 2025 olafsson lab
"""

from __future__ import annotations

import argparse


[docs] def build_parser() -> argparse.ArgumentParser: """Return the argparse parser exposing the ``spacr`` CLI subcommands. :returns: Parser accepting a single positional ``command`` argument. """ parser = argparse.ArgumentParser(prog="spacr") parser.add_argument( "command", nargs="?", default="gui", choices=[ "gui", "mask", "measure", "classify", "annotate", "sequencing", "umap", "make-masks", "version", ], help="Command to run.", ) return parser
#: The subcommand a user types -> the Qt tab it should open on. #: #: `gui` names no tab: it opens on Home, which is what it always did. _APP_KEYS = { "mask": "mask", "measure": "measure", "classify": "classify_merged", "annotate": "annotate", "sequencing": "map_barcodes", "umap": "umap", "make-masks": "make_masks", }
[docs] def main(argv: list[str] | None = None) -> int: """CLI entry point; dispatch to the requested spacr subcommand. :param argv: Argument list to parse. When None, ``sys.argv[1:]`` is used. :returns: ``0`` for the version command; window commands return the Qt launcher's integer exit status. :raises SystemExit: with code ``2`` when argparse rejects the arguments or a command accepted by the parser has no dispatcher. ``parser.error`` never returns, so there is no exit code to hand back in either case. """ parser = build_parser() args = parser.parse_args(argv) if args.command == "version": from .version import version_str print(version_str) return 0 if args.command in ("gui", "mask", "measure", "classify", "annotate", "sequencing", "umap", "make-masks"): from .qt import run key = _APP_KEYS.get(args.command) return int(run([key] if key else []) or 0) parser.error(f"Unknown command: {args.command}")
if __name__ == "__main__": raise SystemExit(main())