Coverage for src/somesy/main.py: 75%

24 statements  

« prev     ^ index     » next       coverage.py v7.6.0, created at 2024-07-29 07:42 +0000

1"""Main entry point for the somesy CLI.""" 

2 

3import logging 

4import sys 

5 

6import typer 

7 

8from somesy import __version__ 

9from somesy.cli import fill, init, sync 

10from somesy.core.log import SomesyLogLevel, init_log, set_log_level 

11 

12app = typer.Typer() 

13 

14logger = logging.getLogger("somesy") 

15 

16 

17@app.callback() 

18def version(value: bool): 

19 """Show somesy version and exit.""" 

20 if value: 

21 typer.echo(f"somesy version: {__version__}") 

22 raise typer.Exit() 

23 

24 

25@app.callback() 

26def common( 

27 ctx: typer.Context, 

28 version: bool = typer.Option( 

29 None, "--version", help=version.__doc__, callback=version 

30 ), 

31 show_info: bool = typer.Option( 

32 None, 

33 "--info", 

34 "-v", 

35 help="Enable basic logging.", 

36 ), 

37 verbose: bool = typer.Option( 

38 None, 

39 "--verbose", 

40 "-vv", 

41 help="Enable verbose logging.", 

42 ), 

43 debug: bool = typer.Option( 

44 None, 

45 "--debug", 

46 "-vvv", 

47 help="Enable debug logging.", 

48 ), 

49): 

50 """General flags and arguments for somesy.""" 

51 init_log() 

52 

53 if sum(map(int, map(bool, [show_info, verbose, debug]))) > 1: 

54 typer.echo( 

55 "Only one of --info, --verbose or --debug may be set!", file=sys.stderr 

56 ) 

57 raise typer.Exit(1) 

58 

59 if show_info or verbose or debug: 

60 # NOTE: only explicitly setting log level if a flag is passed, 

61 # in order to distinguish from using the "default log level" 

62 # (needed to check if the user did override the log level as a CLI flag) 

63 set_log_level( 

64 SomesyLogLevel.from_flags(info=show_info, verbose=verbose, debug=debug) 

65 ) 

66 

67 

68# add subcommands 

69app.add_typer(sync.app, name="sync") 

70app.add_typer(init.app, name="init") 

71app.add_typer(fill.app, name="fill")