Coverage for src/geosdhydro/_internal/cli.py: 100.00%
21 statements
« prev ^ index » next coverage.py v7.10.0, created at 2025-09-10 18:56 +1000
« prev ^ index » next coverage.py v7.10.0, created at 2025-09-10 18:56 +1000
1# Why does this file exist, and why not put this in `__main__`?
2#
3# You might be tempted to import things from `__main__` later,
4# but that will cause problems: the code will get executed twice:
5#
6# - When you run `python -m geosdhydro` python will execute
7# `__main__.py` as a script. That means there won't be any
8# `geosdhydro.__main__` in `sys.modules`.
9# - When you import `__main__` it will get executed again (as a module) because
10# there's no `geosdhydro.__main__` in `sys.modules`.
12from __future__ import annotations
14import argparse
15import sys
16from typing import Any
18from geosdhydro._internal import debug
21class _DebugInfo(argparse.Action):
22 def __init__(self, nargs: int | str | None = 0, **kwargs: Any) -> None:
23 super().__init__(nargs=nargs, **kwargs)
25 def __call__(self, *args: Any, **kwargs: Any) -> None: # noqa: ARG002
26 debug._print_debug_info()
27 sys.exit(0)
30def get_parser() -> argparse.ArgumentParser:
31 """Return the CLI argument parser.
33 Returns:
34 An argparse parser.
35 """
36 parser = argparse.ArgumentParser(prog="geosdhydro")
37 parser.add_argument("-V", "--version", action="version", version=f"%(prog)s {debug._get_version()}")
38 parser.add_argument("--debug-info", action=_DebugInfo, help="Print debug information.")
39 return parser
42def main(args: list[str] | None = None) -> int:
43 """Run the main program.
45 This function is executed when you type `geosdhydro` or `python -m geosdhydro`.
47 Parameters:
48 args: Arguments passed from the command line.
50 Returns:
51 An exit code.
52 """
53 parser = get_parser()
54 opts = parser.parse_args(args=args)
55 print(opts)
56 return 0