Coverage for src/hypra/cli/application.py: 91.67%

24 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-26 22:12 +0000

1"""Assemble the Typer application and handle process exit semantics.""" 

2 

3from collections.abc import Sequence 

4 

5import typer 

6from click import Abort, ClickException 

7 

8from ..api import backend_info, expand_epsilon 

9from .commands import Commands 

10 

11 

12class CliApplication: 

13 """Register Hypra's commands with injectable expansion and status services.""" 

14 

15 def __init__(self, *, get_backend_info=backend_info, expand=expand_epsilon): 

16 self.app = typer.Typer( 

17 name="hypra", 

18 help="Native exact hypergeometric epsilon expansions from structured JSON AST input.", 

19 add_completion=False, 

20 no_args_is_help=False, 

21 pretty_exceptions_enable=False, 

22 ) 

23 commands = Commands(get_backend_info, expand) 

24 self.app.command("backend-info")(commands.backend_info) 

25 self.app.command("expand")(commands.expand) 

26 

27 def run(self, argv: Sequence[str] | None = None) -> int: 

28 """Return the command status; usage errors and aborts raise SystemExit.""" 

29 try: 

30 result = self.app(args=argv, prog_name="hypra", standalone_mode=False) 

31 return result if isinstance(result, int) else 0 

32 except ClickException as exc: 

33 exc.show() 

34 raise SystemExit(exc.exit_code) from exc 

35 except Abort as exc: 

36 raise SystemExit(1) from exc 

37 

38 

39_cli = CliApplication() 

40app = _cli.app 

41 

42 

43def main(argv: Sequence[str] | None = None) -> int: 

44 """Invoke the same application used by the console script and documentation.""" 

45 return _cli.run(argv)