Coverage for pyTooling / CLIAbstraction / Command.py: 100%
36 statements
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-06 22:35 +0000
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-06 22:35 +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
51from pyTooling.Decorators import export
52from pyTooling.CLIAbstraction.Argument import NamedArgument
55# TODO: make this class abstract
56@export
57class CommandArgument(NamedArgument):
58 """
59 Represents a command argument.
61 It is usually used to select a sub parser in a CLI argument parser or to hand over all following parameters to a
62 separate tool. An example for a command is 'checkout' in ``git.exe checkout``, which calls ``git-checkout.exe``.
64 **Example:**
66 * ``command``
67 """
69 # TODO: the whole class should be marked as abstract
70 # TODO: a decorator should solve the issue and overwrite the __new__ method with that code
71 def __new__(cls, *args: Any, **kwargs: Any) -> Self:
72 """
73 Check if this class was directly instantiated without being derived to a subclass. If so, raise an error.
75 :param args: Any positional arguments.
76 :param kwargs: Any keyword arguments.
77 :raises TypeError: When this class gets directly instantiated without being derived to a subclass.
78 """
79 if cls is CommandArgument:
80 raise TypeError(f"Class '{cls.__name__}' is abstract.")
81 return super().__new__(cls, *args, **kwargs)
84@export
85class ShortCommand(CommandArgument, pattern="-{0}"):
86 """
87 Represents a command name with a single dash.
89 **Example:**
91 * ``-command``
92 """
94 def __init_subclass__(cls, *args: Any, pattern: str = "-{0}", **kwargs: Any) -> None:
95 """
96 This method is called when a class is derived.
98 :param args: Any positional arguments.
99 :param pattern: This pattern is used to format an argument. |br|
100 Default: ``"-{0}"``.
101 :param kwargs: Any keyword argument.
102 """
103 kwargs["pattern"] = pattern
104 super().__init_subclass__(*args, **kwargs)
106 # TODO: the whole class should be marked as abstract
107 # TODO: a decorator should solve the issue and overwrite the __new__ method with that code
108 def __new__(cls, *args: Any, **kwargs: Any) -> Self:
109 """
110 Check if this class was directly instantiated without being derived to a subclass. If so, raise an error.
112 :param args: Any positional arguments.
113 :param kwargs: Any keyword arguments.
114 :raises TypeError: When this class gets directly instantiated without being derived to a subclass.
115 """
116 if cls is ShortCommand:
117 raise TypeError(f"Class '{cls.__name__}' is abstract.")
118 return super().__new__(cls, *args, **kwargs)
121@export
122class LongCommand(CommandArgument, pattern="--{0}"):
123 """
124 Represents a command name with a double dash.
126 **Example:**
128 * ``--command``
129 """
131 def __init_subclass__(cls, *args: Any, pattern: str = "--{0}", **kwargs: Any) -> None:
132 """
133 This method is called when a class is derived.
135 :param args: Any positional arguments.
136 :param pattern: This pattern is used to format an argument. |br|
137 Default: ``"--{0}"``.
138 :param kwargs: Any keyword argument.
139 """
140 kwargs["pattern"] = pattern
141 super().__init_subclass__(*args, **kwargs)
143 # TODO: the whole class should be marked as abstract
144 # TODO: a decorator should solve the issue and overwrite the __new__ method with that code
145 def __new__(cls, *args: Any, **kwargs: Any) -> Self:
146 """
147 Check if this class was directly instantiated without being derived to a subclass. If so, raise an error.
149 :param args: Any positional arguments.
150 :param kwargs: Any keyword arguments.
151 :raises TypeError: When this class gets directly instantiated without being derived to a subclass.
152 """
153 if cls is LongCommand:
154 raise TypeError(f"Class '{cls.__name__}' is abstract.")
155 return super().__new__(cls, *args, **kwargs)
158@export
159class WindowsCommand(CommandArgument, pattern="/{0}"):
160 """
161 Represents a command name with a single slash.
163 **Example:**
165 * ``/command``
166 """
168 def __init_subclass__(cls, *args: Any, pattern: str = "/{0}", **kwargs: Any) -> None:
169 """
170 This method is called when a class is derived.
172 :param args: Any positional arguments.
173 :param pattern: This pattern is used to format an argument. |br|
174 Default: ``"/{0}"``.
175 :param kwargs: Any keyword argument.
176 """
177 kwargs["pattern"] = pattern
178 super().__init_subclass__(*args, **kwargs)
180 # TODO: the whole class should be marked as abstract
181 # TODO: a decorator should solve the issue and overwrite the __new__ method with that code
182 def __new__(cls, *args: Any, **kwargs: Any) -> Self:
183 """
184 Check if this class was directly instantiated without being derived to a subclass. If so, raise an error.
186 :param args: Any positional arguments.
187 :param kwargs: Any keyword arguments.
188 :raises TypeError: When this class gets directly instantiated without being derived to a subclass.
189 """
190 if cls is WindowsCommand:
191 raise TypeError(f"Class '{cls.__name__}' is abstract.")
192 return super().__new__(cls, *args, **kwargs)