Coverage for pyTooling/Attributes/ArgParse/Argument.py: 100%
59 statements
« prev ^ index » next coverage.py v7.16.1, created at 2026-09-19 11:45 +0000
« prev ^ index » next coverage.py v7.16.1, created at 2026-09-19 11:45 +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.
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:
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
51from pyTooling.Decorators import export
52from pyTooling.Attributes.ArgParse import CommandLineArgument
55@export
56class DelimiterArgument(CommandLineArgument):
57 """
58 Represents a delimiter symbol like ``--``.
59 """
62@export
63class NamedArgument(CommandLineArgument):
64 """
65 Base-class for all command line arguments with a name.
66 """
69@export
70class ValuedArgument(CommandLineArgument):
71 """
72 Base-class for all command line arguments with a value.
73 """
76@export
77class NamedAndValuedArgument(NamedArgument, ValuedArgument):
78 """
79 Base-class for all command line arguments with a name and a value.
80 """
83@export
84class NamedTupledArgument(NamedArgument, ValuedArgument):
85 """
86 Class and base-class for all TupleFlag classes, which represents an argument with separate value.
88 A tuple argument is a command line argument followed by a separate value. Name and value are passed as two arguments
89 to the executable.
91 **Example:**
93 * ``width 100``
94 """
97@export
98class PositionalArgument(ValuedArgument):
99 """
100 Represents a simple string argument containing any information encoded in a string.
102 TODO
104 A list of strings is available as :class:`~pyTooling.Attributes.ArgParse.Argument.StringListArgument`.
105 """
107 def __init__(self, dest: str, metaName: str, type: type = str, optional: bool = False, help: str = "") -> None:
108 """
109 Initializes a positional argument.
111 .. admonition:: ArgParse parameterization
113 :meth:`~argparse.ArgumentParser.add_argument` is called without positional parameters, and with these named
114 parameters:
116 * ``dest=dest``
117 * ``metavar=metaName``
118 * ``type=type``
119 * ``help=help``
120 * ``nargs="?"`` - only if ``optional`` is ``True``
122 :param dest: Name the parsed value is stored under in the :class:`~argparse.Namespace`, and the name the
123 handler method reads it by.
124 :param metaName: Name shown for the value in the usage line and the help page (argparse's ``metavar``).
125 :param type: Optional, callable converting the argument's string to the target type. Default: :class:`str`.
126 :param optional: Optional, if ``True``, the argument may be omitted (``nargs="?"``). Default: ``False``.
127 :param help: Optional, help text shown for this argument in the help page. Default: ``""``.
128 """
129 args: list[str] = []
130 kwargs = {
131 "dest": dest,
132 "metavar": metaName,
133 "type": type,
134 "help": help
135 }
136 if optional:
137 kwargs["nargs"] = "?"
139 super().__init__(*args, **kwargs)
142@export
143class StringArgument(PositionalArgument):
144 """
145 Represents a simple string argument.
147 A list of strings is available as :class:`~pyTooling.Attributes.ArgParse.Argument.StringListArgument`.
148 """
150 def __init__(self, dest: str, metaName: str, optional: bool = False, help: str = "") -> None:
151 """
152 Initializes a positional string argument.
154 Parameterizes :meth:`~argparse.ArgumentParser.add_argument` like
155 :class:`~pyTooling.Attributes.ArgParse.Argument.PositionalArgument` does, with ``type`` set to :class:`str`.
157 :param dest: Name the parsed value is stored under in the :class:`~argparse.Namespace`, and the name the
158 handler method reads it by.
159 :param metaName: Name shown for the value in the usage line and the help page (argparse's ``metavar``).
160 :param optional: Optional, if ``True``, the argument may be omitted (``nargs="?"``). Default: ``False``.
161 :param help: Optional, help text shown for this argument in the help page. Default: ``""``.
162 """
163 super().__init__(dest, metaName, str, optional, help)
166@export
167class IntegerArgument(PositionalArgument):
168 """
169 Represents an integer argument.
171 A list of integer numbers is available as :class:`~pyTooling.Attributes.ArgParse.Argument.IntegerListArgument`.
172 """
174 def __init__(self, dest: str, metaName: str, optional: bool = False, help: str = "") -> None:
175 """
176 Initializes a positional integer argument.
178 Parameterizes :meth:`~argparse.ArgumentParser.add_argument` like
179 :class:`~pyTooling.Attributes.ArgParse.Argument.PositionalArgument` does, with ``type`` set to :class:`int`.
181 :param dest: Name the parsed value is stored under in the :class:`~argparse.Namespace`, and the name the
182 handler method reads it by.
183 :param metaName: Name shown for the value in the usage line and the help page (argparse's ``metavar``).
184 :param optional: Optional, if ``True``, the argument may be omitted (``nargs="?"``). Default: ``False``.
185 :param help: Optional, help text shown for this argument in the help page. Default: ``""``.
186 """
187 super().__init__(dest, metaName, int, optional, help)
190@export
191class FloatArgument(PositionalArgument):
192 """
193 Represents a floating point number argument.
195 A list of floating point numbers is available as :class:`~pyTooling.Attributes.ArgParse.Argument.FloatListArgument`.
196 """
198 def __init__(self, dest: str, metaName: str, optional: bool = False, help: str = "") -> None:
199 """
200 Initializes a positional floating point number argument.
202 Parameterizes :meth:`~argparse.ArgumentParser.add_argument` like
203 :class:`~pyTooling.Attributes.ArgParse.Argument.PositionalArgument` does, with ``type`` set to :class:`float`.
205 :param dest: Name the parsed value is stored under in the :class:`~argparse.Namespace`, and the name the
206 handler method reads it by.
207 :param metaName: Name shown for the value in the usage line and the help page (argparse's ``metavar``).
208 :param optional: Optional, if ``True``, the argument may be omitted (``nargs="?"``). Default: ``False``.
209 :param help: Optional, help text shown for this argument in the help page. Default: ``""``.
210 """
211 super().__init__(dest, metaName, float, optional, help)
214# TODO: Add option to class if path should be checked for existence
215@export
216class PathArgument(PositionalArgument):
217 """
218 Represents a single path argument.
220 A list of paths is available as :class:`~pyTooling.Attributes.ArgParse.Argument.PathListArgument`.
221 """
223 def __init__(self, dest: str, metaName: str, optional: bool = False, help: str = "") -> None:
224 """
225 Initializes a positional path argument.
227 Parameterizes :meth:`~argparse.ArgumentParser.add_argument` like
228 :class:`~pyTooling.Attributes.ArgParse.Argument.PositionalArgument` does, with ``type`` set to
229 :class:`~pathlib.Path`.
231 :param dest: Name the parsed value is stored under in the :class:`~argparse.Namespace`, and the name the
232 handler method reads it by.
233 :param metaName: Name shown for the value in the usage line and the help page (argparse's ``metavar``).
234 :param optional: Optional, if ``True``, the argument may be omitted (``nargs="?"``). Default: ``False``.
235 :param help: Optional, help text shown for this argument in the help page. Default: ``""``.
236 """
237 super().__init__(dest, metaName, Path, optional, help)
240@export
241class ListArgument(ValuedArgument):
242 """
243 Represents a list of values (:class:`~pyTooling.Attributes.ArgParse.Argument.StringArgument`).
244 """
246 def __init__(self, dest: str, metaName: str, type: type = str, optional: bool = False, help: str = "") -> None:
247 """
248 Initializes a positional argument accepting a list of values.
250 .. admonition:: ArgParse parameterization
252 :meth:`~argparse.ArgumentParser.add_argument` is called without positional parameters, and with these named
253 parameters:
255 * ``dest=dest``
256 * ``metavar=metaName``
257 * ``nargs="*"`` if ``optional`` is ``True``, otherwise ``"+"``
258 * ``type=type``
259 * ``help=help``
261 :param dest: Name the parsed value is stored under in the :class:`~argparse.Namespace`, and the name the
262 handler method reads it by.
263 :param metaName: Name shown for the value in the usage line and the help page (argparse's ``metavar``).
264 :param type: Optional, callable converting each value's string to the target type. Default: :class:`str`.
265 :param optional: Optional, if ``True``, an empty list is accepted (``nargs="*"``); otherwise at least one value is
266 required (``nargs="+"``). Default: ``False``.
267 :param help: Optional, help text shown for this argument in the help page. Default: ``""``.
268 """
269 args: list[str] = []
270 kwargs = {
271 "dest": dest,
272 "metavar": metaName,
273 "nargs": "*" if optional else "+",
274 "type": type,
275 "help": help
276 }
277 super().__init__(*args, **kwargs)
280@export
281class StringListArgument(ListArgument):
282 """
283 Represents a list of string arguments (:class:`~pyTooling.Attributes.ArgParse.Argument.StringArgument`).
284 """
286 def __init__(self, dest: str, metaName: str, optional: bool = False, help: str = "") -> None:
287 """
288 Initializes a positional argument accepting a list of string values.
290 Parameterizes :meth:`~argparse.ArgumentParser.add_argument` like
291 :class:`~pyTooling.Attributes.ArgParse.Argument.ListArgument` does, with ``type`` set to :class:`str`.
293 :param dest: Name the parsed value is stored under in the :class:`~argparse.Namespace`, and the name the
294 handler method reads it by.
295 :param metaName: Name shown for the value in the usage line and the help page (argparse's ``metavar``).
296 :param optional: Optional, if ``True``, an empty list is accepted (``nargs="*"``); otherwise at least one value is
297 required (``nargs="+"``). Default: ``False``.
298 :param help: Optional, help text shown for this argument in the help page. Default: ``""``.
299 """
300 super().__init__(dest, metaName, str, optional, help)
303@export
304class IntegerListArgument(ListArgument):
305 """
306 Represents a list of integer number arguments (:class:`~pyTooling.Attributes.ArgParse.Argument.IntegerArgument`).
307 """
309 def __init__(self, dest: str, metaName: str, optional: bool = False, help: str = "") -> None:
310 """
311 Initializes a positional argument accepting a list of integer numbers.
313 Parameterizes :meth:`~argparse.ArgumentParser.add_argument` like
314 :class:`~pyTooling.Attributes.ArgParse.Argument.ListArgument` does, with ``type`` set to :class:`int`.
316 :param dest: Name the parsed value is stored under in the :class:`~argparse.Namespace`, and the name the
317 handler method reads it by.
318 :param metaName: Name shown for the value in the usage line and the help page (argparse's ``metavar``).
319 :param optional: Optional, if ``True``, an empty list is accepted (``nargs="*"``); otherwise at least one value is
320 required (``nargs="+"``). Default: ``False``.
321 :param help: Optional, help text shown for this argument in the help page. Default: ``""``.
322 """
323 super().__init__(dest, metaName, int, optional, help)
326@export
327class FloatListArgument(ListArgument):
328 """
329 Represents a list of floating point number arguments (:class:`~pyTooling.Attributes.ArgParse.Argument.FloatArgument`).
330 """
332 def __init__(self, dest: str, metaName: str, optional: bool = False, help: str = "") -> None:
333 """
334 Initializes a positional argument accepting a list of floating point numbers.
336 Parameterizes :meth:`~argparse.ArgumentParser.add_argument` like
337 :class:`~pyTooling.Attributes.ArgParse.Argument.ListArgument` does, with ``type`` set to :class:`float`.
339 :param dest: Name the parsed value is stored under in the :class:`~argparse.Namespace`, and the name the
340 handler method reads it by.
341 :param metaName: Name shown for the value in the usage line and the help page (argparse's ``metavar``).
342 :param optional: Optional, if ``True``, an empty list is accepted (``nargs="*"``); otherwise at least one value is
343 required (``nargs="+"``). Default: ``False``.
344 :param help: Optional, help text shown for this argument in the help page. Default: ``""``.
345 """
346 super().__init__(dest, metaName, float, optional, help)
349@export
350class PathListArgument(ListArgument):
351 """
352 Represents a list of path arguments (:class:`~pyTooling.Attributes.ArgParse.Argument.PathArgument`).
353 """
355 def __init__(self, dest: str, metaName: str, optional: bool = False, help: str = "") -> None:
356 """
357 Initializes a positional argument accepting a list of path arguments.
359 Parameterizes :meth:`~argparse.ArgumentParser.add_argument` like
360 :class:`~pyTooling.Attributes.ArgParse.Argument.ListArgument` does, with ``type`` set to
361 :class:`~pathlib.Path`.
363 :param dest: Name the parsed value is stored under in the :class:`~argparse.Namespace`, and the name the
364 handler method reads it by.
365 :param metaName: Name shown for the value in the usage line and the help page (argparse's ``metavar``).
366 :param optional: Optional, if ``True``, an empty list is accepted (``nargs="*"``); otherwise at least one value is
367 required (``nargs="+"``). Default: ``False``.
368 :param help: Optional, help text shown for this argument in the help page. Default: ``""``.
369 """
370 super().__init__(dest, metaName, Path, optional, help)