Coverage for pyTooling/Attributes/ArgParse/Argument.py: 97%

59 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 2007-2016 Patrick Lehmann - Dresden, Germany # 

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# 

32from pathlib import Path 

33from typing import Type 

34 

35try: 

36 from pyTooling.Decorators import export 

37 from pyTooling.Attributes.ArgParse import CommandLineArgument 

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

39 print("[pyTooling.Attributes.ArgParse.Argument] Could not import from 'pyTooling.*'!") 

40 

41 try: 

42 from Decorators import export 

43 from Attributes.ArgParse import CommandLineArgument 

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

45 print("[pyTooling.Attributes.ArgParse.Argument] Could not import directly!") 

46 raise ex 

47 

48 

49@export 

50class DelimiterArgument(CommandLineArgument): 

51 """ 

52 Represents a delimiter symbol like ``--``. 

53 """ 

54 

55 

56@export 

57class NamedArgument(CommandLineArgument): 

58 """ 

59 Base-class for all command line arguments with a name. 

60 """ 

61 

62 

63@export 

64class ValuedArgument(CommandLineArgument): 

65 """ 

66 Base-class for all command line arguments with a value. 

67 """ 

68 

69 

70class NamedAndValuedArgument(NamedArgument, ValuedArgument): 

71 """ 

72 Base-class for all command line arguments with a name and a value. 

73 """ 

74 

75 

76class NamedTupledArgument(NamedArgument, ValuedArgument): 

77 """ 

78 Class and base-class for all TupleFlag classes, which represents an argument with separate value. 

79 

80 A tuple argument is a command line argument followed by a separate value. Name and value are passed as two arguments 

81 to the executable. 

82 

83 **Example: ** 

84 

85 * `width 100`` 

86 """ 

87 

88 

89@export 

90class PositionalArgument(ValuedArgument): 

91 """ 

92 Represents a simple string argument containing any information encoded in a string. 

93 

94 TODO 

95 

96 A list of strings is available as :class:`~pyTooling.Attribute.ArgParse.Argument.StringListArgument`. 

97 """ 

98 

99 def __init__(self, dest: str, metaName: str, type: Type = str, optional: bool = False, help: str = "") -> None: 

100 """ 

101 The constructor expects positional (``*args``) and/or named parameters (``**kwargs``) which are passed without 

102 modification to :meth:`~ArgumentParser.add_argument`. 

103 """ 

104 args = [] 

105 kwargs = { 

106 "dest": dest, 

107 "metavar": metaName, 

108 "type": type, 

109 "help": help 

110 } 

111 if optional: 111 ↛ 112line 111 didn't jump to line 112 because the condition on line 111 was never true

112 kwargs["nargs"] = "?" 

113 

114 super().__init__(*args, **kwargs) 

115 

116 

117@export 

118class StringArgument(PositionalArgument): 

119 """ 

120 Represents a simple string argument. 

121 

122 A list of strings is available as :class:`~pyTooling.Attribute.ArgParse.Argument.StringListArgument`. 

123 """ 

124 

125 def __init__(self, dest: str, metaName: str, optional: bool = False, help: str = "") -> None: 

126 """ 

127 The constructor expects positional (``*args``) and/or named parameters (``**kwargs``) which are passed without 

128 modification to :meth:`~ArgumentParser.add_argument`. 

129 """ 

130 super().__init__(dest, metaName, str, optional, help) 

131 

132 

133@export 

134class IntegerArgument(PositionalArgument): 

135 """ 

136 Represents an integer argument. 

137 

138 A list of strings is available as :class:`~pyTooling.Attribute.ArgParse.Argument.StringListArgument`. 

139 """ 

140 

141 def __init__(self, dest: str, metaName: str, optional: bool = False, help: str = "") -> None: 

142 """ 

143 The constructor expects positional (``*args``) and/or named parameters (``**kwargs``) which are passed without 

144 modification to :meth:`~ArgumentParser.add_argument`. 

145 """ 

146 super().__init__(dest, metaName, int, optional, help) 

147 

148 

149@export 

150class FloatArgument(PositionalArgument): 

151 """ 

152 Represents a floating point number argument. 

153 

154 A list of strings is available as :class:`~pyTooling.Attribute.ArgParse.Argument.StringListArgument`. 

155 """ 

156 

157 def __init__(self, dest: str, metaName: str, optional: bool = False, help: str = "") -> None: 

158 """ 

159 The constructor expects positional (``*args``) and/or named parameters (``**kwargs``) which are passed without 

160 modification to :meth:`~ArgumentParser.add_argument`. 

161 """ 

162 super().__init__(dest, metaName, float, optional, help) 

163 

164 

165# TODO: Add option to class if path should be checked for existence 

166@export 

167class PathArgument(PositionalArgument): 

168 """ 

169 Represents a single path argument. 

170 

171 A list of paths is available as :class:`~pyTooling.Attribute.ArgParse.Argument.PathListArgument`. 

172 """ 

173 

174 def __init__(self, dest: str, metaName: str, optional: bool = False, help: str = "") -> None: 

175 """ 

176 The constructor expects positional (``*args``) and/or named parameters (``**kwargs``) which are passed without 

177 modification to :meth:`~ArgumentParser.add_argument`. 

178 """ 

179 super().__init__(dest, metaName, Path, optional, help) 

180 

181 

182@export 

183class ListArgument(ValuedArgument): 

184 """ 

185 Represents a list of string argument (:class:`~pyTooling.Attribute.ArgParse.Argument.StringArgument`). 

186 """ 

187 

188 def __init__(self, dest: str, metaName: str, type: Type = str, optional: bool = False, help: str = "") -> None: 

189 """ 

190 The constructor expects positional (``*args``) and/or named parameters (``**kwargs``) which are passed without 

191 modification to :meth:`~ArgumentParser.add_argument`. 

192 """ 

193 args = [] 

194 kwargs = { 

195 "dest": dest, 

196 "metavar": metaName, 

197 "nargs": "*" if optional else "+", 

198 "type": type, 

199 "help": help 

200 } 

201 super().__init__(*args, **kwargs) 

202 

203 

204@export 

205class StringListArgument(ListArgument): 

206 """ 

207 Represents a list of string argument (:class:`~pyTooling.Attribute.ArgParse.Argument.StringArgument`). 

208 """ 

209 

210 def __init__(self, dest: str, metaName: str, optional: bool = False, help: str = "") -> None: 

211 """ 

212 The constructor expects positional (``*args``) and/or named parameters (``**kwargs``) which are passed without 

213 modification to :meth:`~ArgumentParser.add_argument`. 

214 """ 

215 super().__init__(dest, metaName, str, optional, help) 

216 

217 

218@export 

219class IntegerListArgument(ListArgument): 

220 """ 

221 Represents a list of string argument (:class:`~pyTooling.Attribute.ArgParse.Argument.StringArgument`). 

222 """ 

223 

224 def __init__(self, dest: str, metaName: str, optional: bool = False, help: str = "") -> None: 

225 """ 

226 The constructor expects positional (``*args``) and/or named parameters (``**kwargs``) which are passed without 

227 modification to :meth:`~ArgumentParser.add_argument`. 

228 """ 

229 super().__init__(dest, metaName, int, optional, help) 

230 

231 

232@export 

233class FloatListArgument(ListArgument): 

234 """ 

235 Represents a list of string argument (:class:`~pyTooling.Attribute.ArgParse.Argument.StringArgument`). 

236 """ 

237 

238 def __init__(self, dest: str, metaName: str, optional: bool = False, help: str = "") -> None: 

239 """ 

240 The constructor expects positional (``*args``) and/or named parameters (``**kwargs``) which are passed without 

241 modification to :meth:`~ArgumentParser.add_argument`. 

242 """ 

243 super().__init__(dest, metaName, float, optional, help) 

244 

245 

246@export 

247class PathListArgument(ListArgument): 

248 """ 

249 Represents a list of path arguments (:class:`~pyTooling.Attribute.ArgParse.Argument.PathArgument`). 

250 """ 

251 

252 def __init__(self, dest: str, metaName: str, optional: bool = False, help: str = "") -> None: 

253 """ 

254 The constructor expects positional (``*args``) and/or named parameters (``**kwargs``) which are passed without 

255 modification to :meth:`~ArgumentParser.add_argument`. 

256 """ 

257 super().__init__(dest, metaName, Path, optional, help)