Coverage for pyTooling / CLIAbstraction / Flag.py: 100%
36 statements
« prev ^ index » next coverage.py v7.13.4, created at 2026-02-13 22:36 +0000
« 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 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
46from pyTooling.Decorators import export
47from pyTooling.CLIAbstraction.Argument import NamedArgument
50@export
51class FlagArgument(NamedArgument):
52 """
53 Base-class for all Flag classes, which represents a simple flag argument like ``-v`` or ``--verbose``.
55 A simple flag is a single value (absent/present or off/on) with no additional data (value).
56 """
58 # TODO: the whole class should be marked as abstract
59 # TODO: a decorator should solve the issue and overwrite the __new__ method with that code
60 def __new__(cls, *args: Any, **kwargs: Any) -> Self:
61 """
62 Check if this class was directly instantiated without being derived to a subclass. If so, raise an error.
64 :param args: Any positional arguments.
65 :param kwargs: Any keyword arguments.
66 :raises TypeError: When this class gets directly instantiated without being derived to a subclass.
67 """
68 if cls is FlagArgument:
69 raise TypeError(f"Class '{cls.__name__}' is abstract.")
70 return super().__new__(cls, *args, **kwargs)
73@export
74class ShortFlag(FlagArgument, pattern="-{0}"):
75 """
76 Represents a :class:`~pyTooling.CLIAbstraction.Flag.Flag` argument with a single dash.
78 **Example:**
80 * ``-optimize``
81 """
83 def __init_subclass__(cls, *args: Any, pattern: str = "-{0}", **kwargs: Any) -> None:
84 """
85 This method is called when a class is derived.
87 :param args: Any positional arguments.
88 :param pattern: This pattern is used to format an argument. |br|
89 Default: ``"-{0}"``.
90 :param kwargs: Any keyword argument.
91 """
92 kwargs["pattern"] = pattern
93 super().__init_subclass__(*args, **kwargs)
95 # TODO: the whole class should be marked as abstract
96 # TODO: a decorator should solve the issue and overwrite the __new__ method with that code
97 def __new__(cls, *args: Any, **kwargs: Any) -> Self:
98 """
99 Check if this class was directly instantiated without being derived to a subclass. If so, raise an error.
101 :param args: Any positional arguments.
102 :param kwargs: Any keyword arguments.
103 :raises TypeError: When this class gets directly instantiated without being derived to a subclass.
104 """
105 if cls is ShortFlag:
106 raise TypeError(f"Class '{cls.__name__}' is abstract.")
107 return super().__new__(cls, *args, **kwargs)
110@export
111class LongFlag(FlagArgument, pattern="--{0}"):
112 """
113 Represents a :class:`~pyTooling.CLIAbstraction.Flag.Flag` argument with a double dash.
115 **Example:**
117 * ``--optimize``
118 """
120 def __init_subclass__(cls, *args: Any, pattern: str = "--{0}", **kwargs: Any) -> None:
121 """
122 This method is called when a class is derived.
124 :param args: Any positional arguments.
125 :param pattern: This pattern is used to format an argument. |br|
126 Default: ``"--{0}"``.
127 :param kwargs: Any keyword argument.
128 """
129 kwargs["pattern"] = pattern
130 super().__init_subclass__(*args, **kwargs)
132 # TODO: the whole class should be marked as abstract
133 # TODO: a decorator should solve the issue and overwrite the __new__ method with that code
134 def __new__(cls, *args: Any, **kwargs: Any) -> Self:
135 """
136 Check if this class was directly instantiated without being derived to a subclass. If so, raise an error.
138 :param args: Any positional arguments.
139 :param kwargs: Any keyword arguments.
140 :raises TypeError: When this class gets directly instantiated without being derived to a subclass.
141 """
142 if cls is LongFlag:
143 raise TypeError(f"Class '{cls.__name__}' is abstract.")
144 return super().__new__(cls, *args, **kwargs)
147@export
148class WindowsFlag(FlagArgument, pattern="/{0}"):
149 """
150 Represents a :class:`~pyTooling.CLIAbstraction.Flag.Flag` argument with a single slash.
152 **Example:**
154 * ``/optimize``
155 """
157 def __init_subclass__(cls, *args: Any, pattern: str = "/{0}", **kwargs: Any) -> None:
158 """
159 This method is called when a class is derived.
161 :param args: Any positional arguments.
162 :param pattern: This pattern is used to format an argument. |br|
163 Default: ``"/{0}"``.
164 :param kwargs: Any keyword argument.
165 """
166 kwargs["pattern"] = pattern
167 super().__init_subclass__(*args, **kwargs)
169 # TODO: the whole class should be marked as abstract
170 # TODO: a decorator should solve the issue and overwrite the __new__ method with that code
171 def __new__(cls, *args: Any, **kwargs: Any) -> Self:
172 """
173 Check if this class was directly instantiated without being derived to a subclass. If so, raise an error.
175 :param args: Any positional arguments.
176 :param kwargs: Any keyword arguments.
177 :raises TypeError: When this class gets directly instantiated without being derived to a subclass.
178 """
179 if cls is WindowsFlag:
180 raise TypeError(f"Class '{cls.__name__}' is abstract.")
181 return super().__new__(cls, *args, **kwargs)