Coverage for pyTooling / CLIAbstraction / Flag.py: 100%
37 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-23 22:21 +0000
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-23 22:21 +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"""
33Flag arguments represent simple boolean values by being present or absent.
35.. seealso::
37 * For flags with different pattern based on the boolean value itself. |br|
38 |rarr| :mod:`~pyTooling.CLIAbstraction.BooleanFlag`
39 * For flags with a value. |br|
40 |rarr| :mod:`~pyTooling.CLIAbstraction.ValuedFlag`
41 * For flags that have an optional value. |br|
42 |rarr| :mod:`~pyTooling.CLIAbstraction.NamedOptionalValuedFlag`
43"""
44from typing import Any, Self
46try:
47 from pyTooling.Decorators import export
48 from pyTooling.CLIAbstraction.Argument import NamedArgument
49except (ImportError, ModuleNotFoundError): # pragma: no cover
50 print("[pyTooling.Versioning] Could not import from 'pyTooling.*'!")
52 try:
53 from Decorators import export
54 from CLIAbstraction.Argument import NamedArgument
55 except (ImportError, ModuleNotFoundError) as ex: # pragma: no cover
56 print("[pyTooling.Versioning] Could not import directly!")
57 raise ex
60@export
61class FlagArgument(NamedArgument):
62 """
63 Base-class for all Flag classes, which represents a simple flag argument like ``-v`` or ``--verbose``.
65 A simple flag is a single value (absent/present or off/on) with no additional data (value).
66 """
68 # TODO: the whole class should be marked as abstract
69 # TODO: a decorator should solve the issue and overwrite the __new__ method with that code
70 def __new__(cls, *args: Any, **kwargs: Any) -> Self:
71 """
72 Check if this class was directly instantiated without being derived to a subclass. If so, raise an error.
74 :param args: Any positional arguments.
75 :param kwargs: Any keyword arguments.
76 :raises TypeError: When this class gets directly instantiated without being derived to a subclass.
77 """
78 if cls is FlagArgument:
79 raise TypeError(f"Class '{cls.__name__}' is abstract.")
80 return super().__new__(cls, *args, **kwargs)
83@export
84class ShortFlag(FlagArgument, pattern="-{0}"):
85 """
86 Represents a :class:`~pyTooling.CLIAbstraction.Flag.Flag` argument with a single dash.
88 **Example:**
90 * ``-optimize``
91 """
93 def __init_subclass__(cls, *args: Any, pattern: str = "-{0}", **kwargs: Any) -> None:
94 """
95 This method is called when a class is derived.
97 :param args: Any positional arguments.
98 :param pattern: This pattern is used to format an argument. |br|
99 Default: ``"-{0}"``.
100 :param kwargs: Any keyword argument.
101 """
102 kwargs["pattern"] = pattern
103 super().__init_subclass__(*args, **kwargs)
105 # TODO: the whole class should be marked as abstract
106 # TODO: a decorator should solve the issue and overwrite the __new__ method with that code
107 def __new__(cls, *args: Any, **kwargs: Any) -> Self:
108 """
109 Check if this class was directly instantiated without being derived to a subclass. If so, raise an error.
111 :param args: Any positional arguments.
112 :param kwargs: Any keyword arguments.
113 :raises TypeError: When this class gets directly instantiated without being derived to a subclass.
114 """
115 if cls is ShortFlag:
116 raise TypeError(f"Class '{cls.__name__}' is abstract.")
117 return super().__new__(cls, *args, **kwargs)
120@export
121class LongFlag(FlagArgument, pattern="--{0}"):
122 """
123 Represents a :class:`~pyTooling.CLIAbstraction.Flag.Flag` argument with a double dash.
125 **Example:**
127 * ``--optimize``
128 """
130 def __init_subclass__(cls, *args: Any, pattern: str = "--{0}", **kwargs: Any) -> None:
131 """
132 This method is called when a class is derived.
134 :param args: Any positional arguments.
135 :param pattern: This pattern is used to format an argument. |br|
136 Default: ``"--{0}"``.
137 :param kwargs: Any keyword argument.
138 """
139 kwargs["pattern"] = pattern
140 super().__init_subclass__(*args, **kwargs)
142 # TODO: the whole class should be marked as abstract
143 # TODO: a decorator should solve the issue and overwrite the __new__ method with that code
144 def __new__(cls, *args: Any, **kwargs: Any) -> Self:
145 """
146 Check if this class was directly instantiated without being derived to a subclass. If so, raise an error.
148 :param args: Any positional arguments.
149 :param kwargs: Any keyword arguments.
150 :raises TypeError: When this class gets directly instantiated without being derived to a subclass.
151 """
152 if cls is LongFlag:
153 raise TypeError(f"Class '{cls.__name__}' is abstract.")
154 return super().__new__(cls, *args, **kwargs)
157@export
158class WindowsFlag(FlagArgument, pattern="/{0}"):
159 """
160 Represents a :class:`~pyTooling.CLIAbstraction.Flag.Flag` argument with a single slash.
162 **Example:**
164 * ``/optimize``
165 """
167 def __init_subclass__(cls, *args: Any, pattern: str = "/{0}", **kwargs: Any) -> None:
168 """
169 This method is called when a class is derived.
171 :param args: Any positional arguments.
172 :param pattern: This pattern is used to format an argument. |br|
173 Default: ``"/{0}"``.
174 :param kwargs: Any keyword argument.
175 """
176 kwargs["pattern"] = pattern
177 super().__init_subclass__(*args, **kwargs)
179 # TODO: the whole class should be marked as abstract
180 # TODO: a decorator should solve the issue and overwrite the __new__ method with that code
181 def __new__(cls, *args: Any, **kwargs: Any) -> Self:
182 """
183 Check if this class was directly instantiated without being derived to a subclass. If so, raise an error.
185 :param args: Any positional arguments.
186 :param kwargs: Any keyword arguments.
187 :raises TypeError: When this class gets directly instantiated without being derived to a subclass.
188 """
189 if cls is WindowsFlag:
190 raise TypeError(f"Class '{cls.__name__}' is abstract.")
191 return super().__new__(cls, *args, **kwargs)