Coverage for lobster/meta_data_tool_base.py: 87%

15 statements  

« prev     ^ index     » next       coverage.py v7.10.2, created at 2025-08-06 09:51 +0000

1from abc import abstractmethod, ABCMeta 

2from argparse import ArgumentParser, Namespace, RawTextHelpFormatter 

3from lobster.version import FULL_NAME 

4 

5 

6BUG_URL = "https://github.com/bmw-software-engineering/lobster/issues" 

7 

8 

9class MetaDataToolBase(metaclass=ABCMeta): 

10 def __init__( 

11 self, 

12 name: str, 

13 description: str, 

14 official: bool, 

15 ) -> None: 

16 """Base class for LOBSTER tools. 

17 

18 It provides an ArgumentParser and implements the --version and --help 

19 features. 

20 

21 params: 

22 name (str): The name of the tool, without the 'lobster-' prefix. 

23 description (str): A brief description of the tool. 

24 It will be used in the help message. 

25 official (bool): Whether the tool is an official LOBSTER tool. 

26 This flag determines the URL for the bug ticker.""" 

27 

28 self._name = f"lobster-{name}" 

29 self._argument_parser = ArgumentParser( 

30 prog = self._name, 

31 description = description, 

32 epilog = (f"Part of {FULL_NAME}, licensed under the AGPLv3." 

33 f" Please report bugs to {BUG_URL}." 

34 if official else None), 

35 allow_abbrev = False, 

36 formatter_class=RawTextHelpFormatter, 

37 ) 

38 self._argument_parser.add_argument( 

39 "-v", 

40 "--version", 

41 action="version", 

42 default=None, 

43 help="print version and exit", 

44 version=FULL_NAME, 

45 ) 

46 

47 @property 

48 def name(self) -> str: 

49 """The name of the tool, prefixed with 'lobster-'.""" 

50 return self._name 

51 

52 def run(self) -> int: 

53 """ 

54 Parse the command line arguments and return the parsed namespace. 

55 

56 If the --version or --help flag is set, it prints those messages. 

57 Otherwise it calls the _run_impl method with the parsed arguments. 

58 """ 

59 args = self._argument_parser.parse_args() 

60 return self._run_impl(args) 

61 

62 @abstractmethod 

63 def _run_impl(self, options: Namespace) -> int: 

64 """This method should be implemented by subclasses to run the tool. 

65 

66 The return value shall be an exit code. 

67 """