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

57 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-08-22 21:29 +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 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# 

32""" 

33Attributes describing positional command line arguments. 

34 

35A positional argument has no option name: it is recognized by its position, and its value is converted to the type the 

36attribute declares before the handler method is called: 

37 

38* :class:`~pyTooling.Attributes.ArgParse.Argument.StringArgument` 

39* :class:`~pyTooling.Attributes.ArgParse.Argument.IntegerArgument` 

40* :class:`~pyTooling.Attributes.ArgParse.Argument.FloatArgument` 

41* :class:`~pyTooling.Attributes.ArgParse.Argument.PathArgument` 

42* :class:`~pyTooling.Attributes.ArgParse.Argument.ListArgument` and its typed variants 

43 (:class:`~pyTooling.Attributes.ArgParse.Argument.StringListArgument`, 

44 :class:`~pyTooling.Attributes.ArgParse.Argument.IntegerListArgument`, 

45 :class:`~pyTooling.Attributes.ArgParse.Argument.FloatListArgument`, 

46 :class:`~pyTooling.Attributes.ArgParse.Argument.PathListArgument`) 

47""" 

48from pathlib import Path 

49 

50 

51from pyTooling.Decorators import export 

52from pyTooling.Attributes.ArgParse import CommandLineArgument 

53 

54 

55@export 

56class DelimiterArgument(CommandLineArgument): 

57 """ 

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

59 """ 

60 

61 

62@export 

63class NamedArgument(CommandLineArgument): 

64 """ 

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

66 """ 

67 

68 

69@export 

70class ValuedArgument(CommandLineArgument): 

71 """ 

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

73 """ 

74 

75 

76class NamedAndValuedArgument(NamedArgument, ValuedArgument): 

77 """ 

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

79 """ 

80 

81 

82class NamedTupledArgument(NamedArgument, ValuedArgument): 

83 """ 

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

85 

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

87 to the executable. 

88 

89 **Example: ** 

90 

91 * `width 100`` 

92 """ 

93 

94 

95@export 

96class PositionalArgument(ValuedArgument): 

97 """ 

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

99 

100 TODO 

101 

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

103 """ 

104 

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

106 """ 

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

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

109 """ 

110 args: list[str] = [] 

111 kwargs = { 

112 "dest": dest, 

113 "metavar": metaName, 

114 "type": type, 

115 "help": help 

116 } 

117 if optional: 

118 kwargs["nargs"] = "?" 

119 

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

121 

122 

123@export 

124class StringArgument(PositionalArgument): 

125 """ 

126 Represents a simple string argument. 

127 

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

129 """ 

130 

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

132 """ 

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

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

135 """ 

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

137 

138 

139@export 

140class IntegerArgument(PositionalArgument): 

141 """ 

142 Represents an integer argument. 

143 

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

145 """ 

146 

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

148 """ 

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

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

151 """ 

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

153 

154 

155@export 

156class FloatArgument(PositionalArgument): 

157 """ 

158 Represents a floating point number argument. 

159 

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

161 """ 

162 

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

164 """ 

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

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

167 """ 

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

169 

170 

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

172@export 

173class PathArgument(PositionalArgument): 

174 """ 

175 Represents a single path argument. 

176 

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

178 """ 

179 

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

181 """ 

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

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

184 """ 

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

186 

187 

188@export 

189class ListArgument(ValuedArgument): 

190 """ 

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

192 """ 

193 

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

195 """ 

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

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

198 """ 

199 args: list[str] = [] 

200 kwargs = { 

201 "dest": dest, 

202 "metavar": metaName, 

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

204 "type": type, 

205 "help": help 

206 } 

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

208 

209 

210@export 

211class StringListArgument(ListArgument): 

212 """ 

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

214 """ 

215 

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

217 """ 

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

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

220 """ 

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

222 

223 

224@export 

225class IntegerListArgument(ListArgument): 

226 """ 

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

228 """ 

229 

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

231 """ 

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

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

234 """ 

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

236 

237 

238@export 

239class FloatListArgument(ListArgument): 

240 """ 

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

242 """ 

243 

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

245 """ 

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

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

248 """ 

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

250 

251 

252@export 

253class PathListArgument(ListArgument): 

254 """ 

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

256 """ 

257 

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

259 """ 

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

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

262 """ 

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