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

30 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-07 17:18 +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 

44from pyTooling.Decorators import export 

45from pyTooling.CLIAbstraction.Argument import NamedTupledArgument 

46 

47 

48@export 

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

50 """ 

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

52 

53 **Example:** 

54 

55 * ``-file file1.txt`` 

56 """ 

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

58 """ 

59 This method is called when a class is derived. 

60 

61 :param args: Any positional arguments. 

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

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

64 :param kwargs: Any keyword argument. 

65 """ 

66 kwargs["pattern"] = pattern 

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

68 

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

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

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

72 """ 

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

74 

75 :param args: Any positional arguments. 

76 :param kwargs: Any keyword arguments. 

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

78 """ 

79 if cls is ShortTupleFlag: 

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

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

82 

83 

84@export 

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

86 """ 

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

88 

89 **Example:** 

90 

91 * ``--file file1.txt`` 

92 """ 

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

94 """ 

95 This method is called when a class is derived. 

96 

97 :param args: Any positional arguments. 

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

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

100 :param kwargs: Any keyword argument. 

101 """ 

102 kwargs["pattern"] = pattern 

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

104 

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

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

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

108 """ 

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

110 

111 :param args: Any positional arguments. 

112 :param kwargs: Any keyword arguments. 

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

114 """ 

115 if cls is LongTupleFlag: 

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

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

118 

119 

120@export 

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

122 """ 

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

124 

125 **Example:** 

126 

127 * ``/file file1.txt`` 

128 """ 

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

130 """ 

131 This method is called when a class is derived. 

132 

133 :param args: Any positional arguments. 

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

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

136 :param kwargs: Any keyword argument. 

137 """ 

138 kwargs["pattern"] = pattern 

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

140 

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

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

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

144 """ 

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

146 

147 :param args: Any positional arguments. 

148 :param kwargs: Any keyword arguments. 

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

150 """ 

151 if cls is WindowsTupleFlag: 

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

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