Coverage for src/devboard/cli.py: 90.32%

29 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-03-19 20:21 +0100

1"""Module that contains the command line application.""" 

2 

3# Why does this file exist, and why not put this in `__main__`? 

4# 

5# You might be tempted to import things from `__main__` later, 

6# but that will cause problems: the code will get executed twice: 

7# 

8# - When you run `python -m devboard` python will execute 

9# `__main__.py` as a script. That means there won't be any 

10# `devboard.__main__` in `sys.modules`. 

11# - When you import `__main__` it will get executed again (as a module) because 

12# there's no `devboard.__main__` in `sys.modules`. 

13 

14from __future__ import annotations 

15 

16import argparse 

17import sys 

18from typing import Any 

19 

20from appdirs import user_config_dir 

21 

22from devboard import debug 

23from devboard.app import Devboard 

24 

25 

26class _DebugInfo(argparse.Action): 

27 def __init__(self, nargs: int | str | None = 0, **kwargs: Any) -> None: 

28 super().__init__(nargs=nargs, **kwargs) 

29 

30 def __call__(self, *args: Any, **kwargs: Any) -> None: # noqa: ARG002 

31 debug.print_debug_info() 

32 sys.exit(0) 

33 

34 

35def get_parser() -> argparse.ArgumentParser: 

36 """Return the CLI argument parser. 

37 

38 Returns: 

39 An argparse parser. 

40 """ 

41 parser = argparse.ArgumentParser(prog="devboard") 

42 parser.add_argument("--show-config-dir", action="store_true", help="Show Devboard's configuration directory.") 

43 parser.add_argument("-V", "--version", action="version", version=f"%(prog)s {debug.get_version()}") 

44 parser.add_argument("--debug-info", action=_DebugInfo, help="Print debug information.") 

45 parser.add_argument("board", nargs="?", default=None, help="Board name or path.") 

46 return parser 

47 

48 

49def main(args: list[str] | None = None) -> int: 

50 """Run the main program. 

51 

52 This function is executed when you type `devboard` or `python -m devboard`. 

53 

54 Parameters: 

55 args: Arguments passed from the command line. 

56 

57 Returns: 

58 An exit code. 

59 """ 

60 parser = get_parser() 

61 opts = parser.parse_args(args=args) 

62 if opts.show_config_dir: 62 ↛ 63line 62 didn't jump to line 63, because the condition on line 62 was never true

63 print(user_config_dir(appname="devboard")) 

64 return 0 

65 app = Devboard(board=opts.board) 

66 app.run() 

67 return 0