Coverage for src/griffe2md/cli.py: 100.00%

24 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-03-20 11:44 +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 griffe2md` python will execute 

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

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

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

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

13 

14from __future__ import annotations 

15 

16import argparse 

17import sys 

18from typing import Any 

19 

20from griffe2md import debug 

21from griffe2md.main import write_package_docs 

22 

23 

24class _DebugInfo(argparse.Action): 

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

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

27 

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

29 debug.print_debug_info() 

30 sys.exit(0) 

31 

32 

33def get_parser() -> argparse.ArgumentParser: 

34 """Return the CLI argument parser. 

35 

36 Returns: 

37 An argparse parser. 

38 """ 

39 parser = argparse.ArgumentParser(prog="griffe2md") 

40 parser.add_argument("package", help="The package to output Markdown docs for.") 

41 parser.add_argument("-o", "--output", default=None, help="File to write to. Default: stdout.") 

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

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

44 return parser 

45 

46 

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

48 """Run the main program. 

49 

50 This function is executed when you type `griffe2md` or `python -m griffe2md`. 

51 

52 Parameters: 

53 args: Arguments passed from the command line. 

54 

55 Returns: 

56 An exit code. 

57 """ 

58 parser = get_parser() 

59 opts = parser.parse_args(args=args) 

60 write_package_docs(opts.package, output=opts.output) 

61 return 0