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

40 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""" 

33Valued flags are arguments with a name and an always present value. 

34 

35The usual delimiter sign between name and value is an equal sign (``=``). 

36 

37.. seealso:: 

38 

39 * For simple flags. |br| 

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

41 * For flags with different pattern based on the boolean value itself. |br| 

42 |rarr| :mod:`~pyTooling.CLIAbstraction.BooleanFlag` 

43 * For flags that have an optional value. |br| 

44 |rarr| :mod:`~pyTooling.CLIAbstraction.NamedOptionalValuedFlag` 

45 * For list of valued flags. |br| 

46 |rarr| :mod:`~pyTooling.CLIAbstraction.ValuedFlagList` 

47""" 

48from typing import Any 

49 

50try: 

51 from pyTooling.Decorators import export 

52 from pyTooling.CLIAbstraction.Argument import NamedAndValuedArgument 

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

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

55 

56 try: 

57 from Decorators import export 

58 from CLIAbstraction.Argument import NamedAndValuedArgument 

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

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

61 raise ex 

62 

63 

64@export 

65class ValuedFlag(NamedAndValuedArgument, pattern="{0}={1}"): 

66 """ 

67 Class and base-class for all ValuedFlag classes, which represents a flag argument with value. 

68 

69 A valued flag is a flag name followed by a value. The default delimiter sign is equal (``=``). Name and value are 

70 passed as one argument to the executable even if the delimiter sign is a whitespace character. 

71 

72 **Example:** 

73 

74 * ``width=100`` 

75 """ 

76 

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

78 """ 

79 This method is called when a class is derived. 

80 

81 :param args: Any positional arguments. 

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

83 :param kwargs: Any keyword argument. 

84 """ 

85 kwargs["pattern"] = pattern 

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

87 

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

89 if cls is ValuedFlag: 

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

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

92 

93 

94@export 

95class ShortValuedFlag(ValuedFlag, pattern="-{0}={1}"): 

96 """ 

97 Represents a :py:class:`ValuedFlagArgument` with a single dash. 

98 

99 **Example:** 

100 

101 * ``-optimizer=on`` 

102 """ 

103 

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

105 """ 

106 This method is called when a class is derived. 

107 

108 :param args: Any positional arguments. 

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

110 :param kwargs: Any keyword argument. 

111 """ 

112 kwargs["pattern"] = pattern 

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

114 

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

116 if cls is ShortValuedFlag: 

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

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

119 

120 

121@export 

122class LongValuedFlag(ValuedFlag, pattern="--{0}={1}"): 

123 """ 

124 Represents a :py:class:`ValuedFlagArgument` with a double dash. 

125 

126 **Example:** 

127 

128 * ``--optimizer=on`` 

129 """ 

130 

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

132 """ 

133 This method is called when a class is derived. 

134 

135 :param args: Any positional arguments. 

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

137 :param kwargs: Any keyword argument. 

138 """ 

139 kwargs["pattern"] = pattern 

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

141 

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

143 if cls is LongValuedFlag: 

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

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

146 

147 

148@export 

149class WindowsValuedFlag(ValuedFlag, pattern="/{0}:{1}"): 

150 """ 

151 Represents a :py:class:`ValuedFlagArgument` with a single slash. 

152 

153 **Example:** 

154 

155 * ``/optimizer:on`` 

156 """ 

157 

158 # TODO: Is it possible to copy the doc-string from super? 

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

160 """ 

161 This method is called when a class is derived. 

162 

163 :param args: Any positional arguments. 

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

165 :param kwargs: Any keyword argument. 

166 """ 

167 kwargs["pattern"] = pattern 

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

169 

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

171 if cls is WindowsValuedFlag: 

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

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