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

31 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-01-08 23:46 +0000

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

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

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

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

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

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

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

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

9# Authors: # 

10# Patrick Lehmann # 

11# # 

12# License: # 

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

14# Copyright 2017-2026 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 tuple-flag arguments represent a name and a value as a 2-tuple. 

34 

35.. seealso:: 

36 

37 * For flags with a value. |br| 

38 |rarr| :mod:`~pyTooling.CLIAbstraction.ValuedFlag` 

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

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

41""" 

42from typing import Any, Self 

43 

44try: 

45 from pyTooling.Decorators import export 

46 from pyTooling.CLIAbstraction.Argument import NamedTupledArgument 

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

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

49 

50 try: 

51 from Decorators import export 

52 from CLIAbstraction.Argument import NamedTupledArgument 

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

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

55 raise ex 

56 

57 

58@export 

59class ShortTupleFlag(NamedTupledArgument, pattern="-{0}"): 

60 """ 

61 Represents a :class:`ValuedTupleArgument` with a single dash in front of the switch name. 

62 

63 **Example:** 

64 

65 * ``-file file1.txt`` 

66 """ 

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

68 """ 

69 This method is called when a class is derived. 

70 

71 :param args: Any positional arguments. 

72 :param pattern: This pattern is used to format an argument. |br| 

73 Default: ``"-{0}"``. 

74 :param kwargs: Any keyword argument. 

75 """ 

76 kwargs["pattern"] = pattern 

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

78 

79 # TODO: the whole class should be marked as abstract 

80 # TODO: a decorator should solve the issue and overwrite the __new__ method with that code 

81 def __new__(cls, *args: Any, **kwargs: Any) -> Self: 

82 """ 

83 Check if this class was directly instantiated without being derived to a subclass. If so, raise an error. 

84 

85 :param args: Any positional arguments. 

86 :param kwargs: Any keyword arguments. 

87 :raises TypeError: When this class gets directly instantiated without being derived to a subclass. 

88 """ 

89 if cls is ShortTupleFlag: 

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

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

92 

93 

94@export 

95class LongTupleFlag(NamedTupledArgument, pattern="--{0}"): 

96 """ 

97 Represents a :class:`ValuedTupleArgument` with a double dash in front of the switch name. 

98 

99 **Example:** 

100 

101 * ``--file file1.txt`` 

102 """ 

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

104 """ 

105 This method is called when a class is derived. 

106 

107 :param args: Any positional arguments. 

108 :param pattern: This pattern is used to format an argument. |br| 

109 Default: ``"--{0}"``. 

110 :param kwargs: Any keyword argument. 

111 """ 

112 kwargs["pattern"] = pattern 

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

114 

115 # TODO: the whole class should be marked as abstract 

116 # TODO: a decorator should solve the issue and overwrite the __new__ method with that code 

117 def __new__(cls, *args: Any, **kwargs: Any) -> Self: 

118 """ 

119 Check if this class was directly instantiated without being derived to a subclass. If so, raise an error. 

120 

121 :param args: Any positional arguments. 

122 :param kwargs: Any keyword arguments. 

123 :raises TypeError: When this class gets directly instantiated without being derived to a subclass. 

124 """ 

125 if cls is LongTupleFlag: 

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

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

128 

129 

130@export 

131class WindowsTupleFlag(NamedTupledArgument, pattern="/{0}"): 

132 """ 

133 Represents a :class:`ValuedTupleArgument` with a single slash in front of the switch name. 

134 

135 **Example:** 

136 

137 * ``/file file1.txt`` 

138 """ 

139 def __init_subclass__(cls, *args: Any, pattern: str = "/{0}", **kwargs: Any) -> None: 

140 """ 

141 This method is called when a class is derived. 

142 

143 :param args: Any positional arguments. 

144 :param pattern: This pattern is used to format an argument. |br| 

145 Default: ``"/{0}"``. 

146 :param kwargs: Any keyword argument. 

147 """ 

148 kwargs["pattern"] = pattern 

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

150 

151 # TODO: the whole class should be marked as abstract 

152 # TODO: a decorator should solve the issue and overwrite the __new__ method with that code 

153 def __new__(cls, *args: Any, **kwargs: Any) -> Self: 

154 """ 

155 Check if this class was directly instantiated without being derived to a subclass. If so, raise an error. 

156 

157 :param args: Any positional arguments. 

158 :param kwargs: Any keyword arguments. 

159 :raises TypeError: When this class gets directly instantiated without being derived to a subclass. 

160 """ 

161 if cls is WindowsTupleFlag: 

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

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