Coverage for pyTooling / CLIAbstraction / Command.py: 100%
37 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-08 23:46 +0000
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-08 23:46 +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"""
33This module implements command arguments. Usually, commands are mutually exclusive and the first argument in a list of
34arguments to a program.
36While commands can or cannot have prefix characters, they shouldn't be confused with flag arguments or string arguments.
38**Example:**
40* ``prog command -arg1 --argument2``
42.. seealso::
44 * For simple flags (various formats). |br|
45 |rarr| :mod:`~pyTooling.CLIAbstraction.Flag`
46 * For string arguments. |br|
47 |rarr| :class:`~pyTooling.CLIAbstraction.Argument.StringArgument`
48"""
49from typing import Any, Self
51try:
52 from pyTooling.Decorators import export
53 from pyTooling.CLIAbstraction.Argument import NamedArgument
54except (ImportError, ModuleNotFoundError): # pragma: no cover
55 print("[pyTooling.Versioning] Could not import from 'pyTooling.*'!")
57 try:
58 from Decorators import export
59 from CLIAbstraction.Argument import NamedArgument
60 except (ImportError, ModuleNotFoundError) as ex: # pragma: no cover
61 print("[pyTooling.Versioning] Could not import directly!")
62 raise ex
65# TODO: make this class abstract
66@export
67class CommandArgument(NamedArgument):
68 """
69 Represents a command argument.
71 It is usually used to select a sub parser in a CLI argument parser or to hand over all following parameters to a
72 separate tool. An example for a command is 'checkout' in ``git.exe checkout``, which calls ``git-checkout.exe``.
74 **Example:**
76 * ``command``
77 """
79 # TODO: the whole class should be marked as abstract
80 # TODO: a decorator should solve the issue and overwrite the __new__ method with that code
81 def __new__(cls, *args: Any, **kwargs: Any) -> Self:
82 """
83 Check if this class was directly instantiated without being derived to a subclass. If so, raise an error.
85 :param args: Any positional arguments.
86 :param kwargs: Any keyword arguments.
87 :raises TypeError: When this class gets directly instantiated without being derived to a subclass.
88 """
89 if cls is CommandArgument:
90 raise TypeError(f"Class '{cls.__name__}' is abstract.")
91 return super().__new__(cls, *args, **kwargs)
94@export
95class ShortCommand(CommandArgument, pattern="-{0}"):
96 """
97 Represents a command name with a single dash.
99 **Example:**
101 * ``-command``
102 """
104 def __init_subclass__(cls, *args: Any, pattern: str = "-{0}", **kwargs: Any) -> None:
105 """
106 This method is called when a class is derived.
108 :param args: Any positional arguments.
109 :param pattern: This pattern is used to format an argument. |br|
110 Default: ``"-{0}"``.
111 :param kwargs: Any keyword argument.
112 """
113 kwargs["pattern"] = pattern
114 super().__init_subclass__(*args, **kwargs)
116 # TODO: the whole class should be marked as abstract
117 # TODO: a decorator should solve the issue and overwrite the __new__ method with that code
118 def __new__(cls, *args: Any, **kwargs: Any) -> Self:
119 """
120 Check if this class was directly instantiated without being derived to a subclass. If so, raise an error.
122 :param args: Any positional arguments.
123 :param kwargs: Any keyword arguments.
124 :raises TypeError: When this class gets directly instantiated without being derived to a subclass.
125 """
126 if cls is ShortCommand:
127 raise TypeError(f"Class '{cls.__name__}' is abstract.")
128 return super().__new__(cls, *args, **kwargs)
131@export
132class LongCommand(CommandArgument, pattern="--{0}"):
133 """
134 Represents a command name with a double dash.
136 **Example:**
138 * ``--command``
139 """
141 def __init_subclass__(cls, *args: Any, pattern: str = "--{0}", **kwargs: Any) -> None:
142 """
143 This method is called when a class is derived.
145 :param args: Any positional arguments.
146 :param pattern: This pattern is used to format an argument. |br|
147 Default: ``"--{0}"``.
148 :param kwargs: Any keyword argument.
149 """
150 kwargs["pattern"] = pattern
151 super().__init_subclass__(*args, **kwargs)
153 # TODO: the whole class should be marked as abstract
154 # TODO: a decorator should solve the issue and overwrite the __new__ method with that code
155 def __new__(cls, *args: Any, **kwargs: Any) -> Self:
156 """
157 Check if this class was directly instantiated without being derived to a subclass. If so, raise an error.
159 :param args: Any positional arguments.
160 :param kwargs: Any keyword arguments.
161 :raises TypeError: When this class gets directly instantiated without being derived to a subclass.
162 """
163 if cls is LongCommand:
164 raise TypeError(f"Class '{cls.__name__}' is abstract.")
165 return super().__new__(cls, *args, **kwargs)
168@export
169class WindowsCommand(CommandArgument, pattern="/{0}"):
170 """
171 Represents a command name with a single slash.
173 **Example:**
175 * ``/command``
176 """
178 def __init_subclass__(cls, *args: Any, pattern: str = "/{0}", **kwargs: Any) -> None:
179 """
180 This method is called when a class is derived.
182 :param args: Any positional arguments.
183 :param pattern: This pattern is used to format an argument. |br|
184 Default: ``"/{0}"``.
185 :param kwargs: Any keyword argument.
186 """
187 kwargs["pattern"] = pattern
188 super().__init_subclass__(*args, **kwargs)
190 # TODO: the whole class should be marked as abstract
191 # TODO: a decorator should solve the issue and overwrite the __new__ method with that code
192 def __new__(cls, *args: Any, **kwargs: Any) -> Self:
193 """
194 Check if this class was directly instantiated without being derived to a subclass. If so, raise an error.
196 :param args: Any positional arguments.
197 :param kwargs: Any keyword arguments.
198 :raises TypeError: When this class gets directly instantiated without being derived to a subclass.
199 """
200 if cls is WindowsCommand:
201 raise TypeError(f"Class '{cls.__name__}' is abstract.")
202 return super().__new__(cls, *args, **kwargs)