Coverage for pyTooling / CLIAbstraction / ValuedFlag.py: 100%
40 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-09 22:24 +0000
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-09 22:24 +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 flags are arguments with a name and an always present value.
35The usual delimiter sign between name and value is an equal sign (``=``).
37.. seealso::
39 * For simple flags. |br|
40 |rarr| :mod:`~pyTooling.CLIAbstraction.Flag`
41 * For flags with different pattern based on the boolean value itself. |br|
42 |rarr| :mod:`~pyTooling.CLIAbstraction.BooleanFlag`
43 * For flags that have an optional value. |br|
44 |rarr| :mod:`~pyTooling.CLIAbstraction.NamedOptionalValuedFlag`
45 * For list of valued flags. |br|
46 |rarr| :mod:`~pyTooling.CLIAbstraction.ValuedFlagList`
47"""
48from typing import Any, Self
50try:
51 from pyTooling.Decorators import export
52 from pyTooling.CLIAbstraction.Argument import NamedAndValuedArgument
53except (ImportError, ModuleNotFoundError): # pragma: no cover
54 print("[pyTooling.Versioning] Could not import from 'pyTooling.*'!")
56 try:
57 from Decorators import export
58 from CLIAbstraction.Argument import NamedAndValuedArgument
59 except (ImportError, ModuleNotFoundError) as ex: # pragma: no cover
60 print("[pyTooling.Versioning] Could not import directly!")
61 raise ex
64@export
65class ValuedFlag(NamedAndValuedArgument, pattern="{0}={1}"):
66 """
67 Class and base-class for all ValuedFlag classes, which represents a flag argument with value.
69 A valued flag is a flag name followed by a value. The default delimiter sign is equal (``=``). Name and value are
70 passed as one argument to the executable even if the delimiter sign is a whitespace character.
72 **Example:**
74 * ``width=100``
75 """
77 def __init_subclass__(cls, *args: Any, pattern: str = "{0}={1}", **kwargs: Any) -> None:
78 """
79 This method is called when a class is derived.
81 :param args: Any positional arguments.
82 :param pattern: This pattern is used to format an argument. |br|
83 Default: ``"{0}={1}"``.
84 :param kwargs: Any keyword argument.
85 """
86 kwargs["pattern"] = pattern
87 super().__init_subclass__(*args, **kwargs)
89 # TODO: the whole class should be marked as abstract
90 # TODO: a decorator should solve the issue and overwrite the __new__ method with that code
91 def __new__(cls, *args: Any, **kwargs: Any) -> Self:
92 """
93 Check if this class was directly instantiated without being derived to a subclass. If so, raise an error.
95 :param args: Any positional arguments.
96 :param kwargs: Any keyword arguments.
97 :raises TypeError: When this class gets directly instantiated without being derived to a subclass.
98 """
99 if cls is ValuedFlag:
100 raise TypeError(f"Class '{cls.__name__}' is abstract.")
101 return super().__new__(cls, *args, **kwargs)
104@export
105class ShortValuedFlag(ValuedFlag, pattern="-{0}={1}"):
106 """
107 Represents a :py:class:`ValuedFlagArgument` with a single dash.
109 **Example:**
111 * ``-optimizer=on``
112 """
114 def __init_subclass__(cls, *args: Any, pattern: str = "-{0}={1}", **kwargs: Any) -> None:
115 """
116 This method is called when a class is derived.
118 :param args: Any positional arguments.
119 :param pattern: This pattern is used to format an argument. |br|
120 Default: ``"-{0}={1}"``.
121 :param kwargs: Any keyword argument.
122 """
123 kwargs["pattern"] = pattern
124 super().__init_subclass__(*args, **kwargs)
126 # TODO: the whole class should be marked as abstract
127 # TODO: a decorator should solve the issue and overwrite the __new__ method with that code
128 def __new__(cls, *args: Any, **kwargs: Any) -> Self:
129 """
130 Check if this class was directly instantiated without being derived to a subclass. If so, raise an error.
132 :param args: Any positional arguments.
133 :param kwargs: Any keyword arguments.
134 :raises TypeError: When this class gets directly instantiated without being derived to a subclass.
135 """
136 if cls is ShortValuedFlag:
137 raise TypeError(f"Class '{cls.__name__}' is abstract.")
138 return super().__new__(cls, *args, **kwargs)
141@export
142class LongValuedFlag(ValuedFlag, pattern="--{0}={1}"):
143 """
144 Represents a :py:class:`ValuedFlagArgument` with a double dash.
146 **Example:**
148 * ``--optimizer=on``
149 """
151 def __init_subclass__(cls, *args: Any, pattern: str = "--{0}={1}", **kwargs: Any) -> None:
152 """
153 This method is called when a class is derived.
155 :param args: Any positional arguments.
156 :param pattern: This pattern is used to format an argument. |br|
157 Default: ``"--{0}={1}"``.
158 :param kwargs: Any keyword argument.
159 """
160 kwargs["pattern"] = pattern
161 super().__init_subclass__(*args, **kwargs)
163 # TODO: the whole class should be marked as abstract
164 # TODO: a decorator should solve the issue and overwrite the __new__ method with that code
165 def __new__(cls, *args: Any, **kwargs: Any) -> Self:
166 """
167 Check if this class was directly instantiated without being derived to a subclass. If so, raise an error.
169 :param args: Any positional arguments.
170 :param kwargs: Any keyword arguments.
171 :raises TypeError: When this class gets directly instantiated without being derived to a subclass.
172 """
173 if cls is LongValuedFlag:
174 raise TypeError(f"Class '{cls.__name__}' is abstract.")
175 return super().__new__(cls, *args, **kwargs)
178@export
179class WindowsValuedFlag(ValuedFlag, pattern="/{0}:{1}"):
180 """
181 Represents a :py:class:`ValuedFlagArgument` with a single slash.
183 **Example:**
185 * ``/optimizer:on``
186 """
188 # TODO: Is it possible to copy the doc-string from super?
189 def __init_subclass__(cls, *args: Any, pattern: str = "/{0}:{1}", **kwargs: Any) -> None:
190 """
191 This method is called when a class is derived.
193 :param args: Any positional arguments.
194 :param pattern: This pattern is used to format an argument. |br|
195 Default: ``"/{0}:{1}"``.
196 :param kwargs: Any keyword argument.
197 """
198 kwargs["pattern"] = pattern
199 super().__init_subclass__(*args, **kwargs)
201 # TODO: the whole class should be marked as abstract
202 # TODO: a decorator should solve the issue and overwrite the __new__ method with that code
203 def __new__(cls, *args: Any, **kwargs: Any) -> Self:
204 """
205 Check if this class was directly instantiated without being derived to a subclass. If so, raise an error.
207 :param args: Any positional arguments.
208 :param kwargs: Any keyword arguments.
209 :raises TypeError: When this class gets directly instantiated without being derived to a subclass.
210 """
211 if cls is WindowsValuedFlag:
212 raise TypeError(f"Class '{cls.__name__}' is abstract.")
213 return super().__new__(cls, *args, **kwargs)