Coverage for kiirastuli/cli.py: 54.76%

31 statements  

« prev     ^ index     » next       coverage.py v7.4.1, created at 2024-02-04 18:51:34 +00:00

1import argparse 

2import pathlib 

3import sys 

4from typing import List, Union 

5 

6import kiirastuli.kiirastuli as api 

7from kiirastuli import APP_ALIAS, APP_NAME, log, parse_csl 

8 

9 

10def parse_request(argv: List[str]) -> Union[int, argparse.Namespace]: 

11 """DRY.""" 

12 parser = argparse.ArgumentParser( 

13 prog=APP_ALIAS, description=APP_NAME, formatter_class=argparse.RawTextHelpFormatter 

14 ) 

15 parser.add_argument( 

16 '--purge', 

17 '-p', 

18 dest='folders', 

19 default='', 

20 help='Folders to visit and purge files from. Optional\n(default: positional purge value)', 

21 required=False, 

22 ) 

23 parser.add_argument( 

24 'folders_pos', 

25 nargs='?', 

26 default='', 

27 help='Folders to visit and purge files from. Optional\n(default: value of purge option)', 

28 ) 

29 parser.add_argument( 

30 '--human', 

31 '-H', 

32 dest='human', 

33 default=False, 

34 action='store_true', 

35 help='choose units to represent values optimized for humans (default: False)', 

36 ) 

37 parser.add_argument( 

38 '--verbose', 

39 '-v', 

40 dest='verbose', 

41 default=False, 

42 action='store_true', 

43 help='work logging more information along the way (default: False)', 

44 ) 

45 parser.add_argument( 

46 '--debug', 

47 '-d', 

48 dest='debug', 

49 default=False, 

50 action='store_true', 

51 help='be even more vrbose to support debugging (default: False)', 

52 ) 

53 if not argv: 53 ↛ 57line 53 didn't jump to line 57, because the condition on line 53 was never false

54 parser.print_help() 

55 return 0 

56 

57 options = parser.parse_args(argv) 

58 

59 if not options.folders: 

60 if options.folders_pos: 

61 options.folders = options.folders_pos 

62 

63 options.folders = parse_csl(options.folders) 

64 

65 if all(pathlib.Path(folder).is_dir() for folder in options.folders): 

66 return options 

67 

68 log.error(f'some in {options.folders} do not exist or are no folders') 

69 return 1 

70 

71 

72def main(argv: Union[List[str], None] = None) -> int: 

73 """Delegate processing to functional module.""" 

74 argv = sys.argv[1:] if argv is None else argv 

75 options = parse_request(argv) 

76 if isinstance(options, int): 76 ↛ 78line 76 didn't jump to line 78, because the condition on line 76 was never false

77 return 0 

78 return api.main(options) # type: ignore