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

58 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-02-13 22:36 +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# 

32from pathlib import Path 

33from typing import Type 

34 

35from pyTooling.Decorators import export 

36from pyTooling.Attributes.ArgParse import CommandLineArgument 

37 

38 

39@export 

40class DelimiterArgument(CommandLineArgument): 

41 """ 

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

43 """ 

44 

45 

46@export 

47class NamedArgument(CommandLineArgument): 

48 """ 

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

50 """ 

51 

52 

53@export 

54class ValuedArgument(CommandLineArgument): 

55 """ 

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

57 """ 

58 

59 

60class NamedAndValuedArgument(NamedArgument, ValuedArgument): 

61 """ 

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

63 """ 

64 

65 

66class NamedTupledArgument(NamedArgument, ValuedArgument): 

67 """ 

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

69 

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

71 to the executable. 

72 

73 **Example: ** 

74 

75 * `width 100`` 

76 """ 

77 

78 

79@export 

80class PositionalArgument(ValuedArgument): 

81 """ 

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

83 

84 TODO 

85 

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

87 """ 

88 

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

90 """ 

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

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

93 """ 

94 args = [] 

95 kwargs = { 

96 "dest": dest, 

97 "metavar": metaName, 

98 "type": type, 

99 "help": help 

100 } 

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

102 kwargs["nargs"] = "?" 

103 

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

105 

106 

107@export 

108class StringArgument(PositionalArgument): 

109 """ 

110 Represents a simple string argument. 

111 

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

113 """ 

114 

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

116 """ 

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

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

119 """ 

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

121 

122 

123@export 

124class IntegerArgument(PositionalArgument): 

125 """ 

126 Represents an integer argument. 

127 

128 A list of strings is available as :class:`~pyTooling.Attribute.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, int, optional, help) 

137 

138 

139@export 

140class FloatArgument(PositionalArgument): 

141 """ 

142 Represents a floating point number argument. 

143 

144 A list of strings is available as :class:`~pyTooling.Attribute.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, float, optional, help) 

153 

154 

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

156@export 

157class PathArgument(PositionalArgument): 

158 """ 

159 Represents a single path argument. 

160 

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

162 """ 

163 

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

165 """ 

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

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

168 """ 

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

170 

171 

172@export 

173class ListArgument(ValuedArgument): 

174 """ 

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

176 """ 

177 

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

179 """ 

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

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

182 """ 

183 args = [] 

184 kwargs = { 

185 "dest": dest, 

186 "metavar": metaName, 

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

188 "type": type, 

189 "help": help 

190 } 

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

192 

193 

194@export 

195class StringListArgument(ListArgument): 

196 """ 

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

198 """ 

199 

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

201 """ 

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

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

204 """ 

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

206 

207 

208@export 

209class IntegerListArgument(ListArgument): 

210 """ 

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

212 """ 

213 

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

215 """ 

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

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

218 """ 

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

220 

221 

222@export 

223class FloatListArgument(ListArgument): 

224 """ 

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

226 """ 

227 

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

229 """ 

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

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

232 """ 

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

234 

235 

236@export 

237class PathListArgument(ListArgument): 

238 """ 

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

240 """ 

241 

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

243 """ 

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

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

246 """ 

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