Coverage for pyTooling/CLIAbstraction/Command.py: 100%

37 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-04-25 22:22 +0000

1# ==================================================================================================================== # 

2# _____ _ _ ____ _ ___ _ _ _ _ _ # 

3# _ __ _ |_ _|__ ___ | (_)_ __ __ _ / ___| | |_ _| / \ | |__ ___| |_ _ __ __ _ ___| |_(_) ___ _ __ # 

4# | '_ \| | | || |/ _ \ / _ \| | | '_ \ / _` || | | | | | / _ \ | '_ \/ __| __| '__/ _` |/ __| __| |/ _ \| '_ \ # 

5# | |_) | |_| || | (_) | (_) | | | | | | (_| || |___| |___ | | / ___ \| |_) \__ \ |_| | | (_| | (__| |_| | (_) | | | | # 

6# | .__/ \__, ||_|\___/ \___/|_|_|_| |_|\__, (_)____|_____|___/_/ \_\_.__/|___/\__|_| \__,_|\___|\__|_|\___/|_| |_| # 

7# |_| |___/ |___/ # 

8# ==================================================================================================================== # 

9# Authors: # 

10# Patrick Lehmann # 

11# # 

12# License: # 

13# ==================================================================================================================== # 

14# Copyright 2017-2025 Patrick Lehmann - Bötzingen, Germany # 

15# Copyright 2014-2016 Technische Universität Dresden - Germany, Chair of VLSI-Design, Diagnostics and Architecture # 

16# # 

17# Licensed under the Apache License, Version 2.0 (the "License"); # 

18# you may not use this file except in compliance with the License. # 

19# You may obtain a copy of the License at # 

20# # 

21# http://www.apache.org/licenses/LICENSE-2.0 # 

22# # 

23# Unless required by applicable law or agreed to in writing, software # 

24# distributed under the License is distributed on an "AS IS" BASIS, # 

25# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # 

26# See the License for the specific language governing permissions and # 

27# limitations under the License. # 

28# # 

29# SPDX-License-Identifier: Apache-2.0 # 

30# ==================================================================================================================== # 

31# 

32""" 

33This module implements command arguments. Usually, commands are mutually exclusive and the first argument in a list of 

34arguments to a program. 

35 

36While commands can or cannot have prefix characters, they shouldn't be confused with flag arguments or string arguments. 

37 

38**Example:** 

39 

40* ``prog command -arg1 --argument2`` 

41 

42.. seealso:: 

43 

44 * For simple flags (various formats). |br| 

45 |rarr| :mod:`~pyTooling.CLIAbstraction.Flag` 

46 * For string arguments. |br| 

47 |rarr| :class:`~pyTooling.CLIAbstraction.Argument.StringArgument` 

48""" 

49from typing import Any 

50 

51try: 

52 from pyTooling.Decorators import export 

53 from pyTooling.CLIAbstraction.Argument import NamedArgument 

54except (ImportError, ModuleNotFoundError): # pragma: no cover 

55 print("[pyTooling.Versioning] Could not import from 'pyTooling.*'!") 

56 

57 try: 

58 from Decorators import export 

59 from CLIAbstraction.Argument import NamedArgument 

60 except (ImportError, ModuleNotFoundError) as ex: # pragma: no cover 

61 print("[pyTooling.Versioning] Could not import directly!") 

62 raise ex 

63 

64 

65# TODO: make this class abstract 

66@export 

67class CommandArgument(NamedArgument): 

68 """ 

69 Represents a command argument. 

70 

71 It is usually used to select a sub parser in a CLI argument parser or to hand over all following parameters to a 

72 separate tool. An example for a command is 'checkout' in ``git.exe checkout``, which calls ``git-checkout.exe``. 

73 

74 **Example:** 

75 

76 * ``command`` 

77 """ 

78 

79 def __new__(cls, *args: Any, **kwargs: Any): 

80 if cls is CommandArgument: 

81 raise TypeError(f"Class '{cls.__name__}' is abstract.") 

82 return super().__new__(cls, *args, **kwargs) 

83 

84 

85@export 

86class ShortCommand(CommandArgument, pattern="-{0}"): 

87 """ 

88 Represents a command name with a single dash. 

89 

90 **Example:** 

91 

92 * ``-command`` 

93 """ 

94 

95 def __init_subclass__(cls, *args: Any, pattern: str = "-{0}", **kwargs: Any): 

96 """ 

97 This method is called when a class is derived. 

98 

99 :param args: Any positional arguments. 

100 :param pattern: This pattern is used to format an argument. 

101 :param kwargs: Any keyword argument. 

102 """ 

103 kwargs["pattern"] = pattern 

104 super().__init_subclass__(*args, **kwargs) 

105 

106 def __new__(cls, *args: Any, **kwargs: Any): 

107 if cls is ShortCommand: 

108 raise TypeError(f"Class '{cls.__name__}' is abstract.") 

109 return super().__new__(cls, *args, **kwargs) 

110 

111 

112@export 

113class LongCommand(CommandArgument, pattern="--{0}"): 

114 """ 

115 Represents a command name with a double dash. 

116 

117 **Example:** 

118 

119 * ``--command`` 

120 """ 

121 

122 def __init_subclass__(cls, *args: Any, pattern: str = "--{0}", **kwargs: Any): 

123 """ 

124 This method is called when a class is derived. 

125 

126 :param args: Any positional arguments. 

127 :param pattern: This pattern is used to format an argument. 

128 :param kwargs: Any keyword argument. 

129 """ 

130 kwargs["pattern"] = pattern 

131 super().__init_subclass__(*args, **kwargs) 

132 

133 def __new__(cls, *args: Any, **kwargs: Any): 

134 if cls is LongCommand: 

135 raise TypeError(f"Class '{cls.__name__}' is abstract.") 

136 return super().__new__(cls, *args, **kwargs) 

137 

138 

139@export 

140class WindowsCommand(CommandArgument, pattern="/{0}"): 

141 """ 

142 Represents a command name with a single slash. 

143 

144 **Example:** 

145 

146 * ``/command`` 

147 """ 

148 

149 def __init_subclass__(cls, *args: Any, pattern: str = "/{0}", **kwargs: Any): 

150 """ 

151 This method is called when a class is derived. 

152 

153 :param args: Any positional arguments. 

154 :param pattern: This pattern is used to format an argument. 

155 :param kwargs: Any keyword argument. 

156 """ 

157 kwargs["pattern"] = pattern 

158 super().__init_subclass__(*args, **kwargs) 

159 

160 def __new__(cls, *args: Any, **kwargs: Any): 

161 if cls is WindowsCommand: 

162 raise TypeError(f"Class '{cls.__name__}' is abstract.") 

163 return super().__new__(cls, *args, **kwargs)