Coverage for pyTooling/CLIAbstraction/Command.py: 100%
37 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-25 22:22 +0000
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-25 22:22 +0000
1# ==================================================================================================================== #
2# _____ _ _ ____ _ ___ _ _ _ _ _ #
3# _ __ _ |_ _|__ ___ | (_)_ __ __ _ / ___| | |_ _| / \ | |__ ___| |_ _ __ __ _ ___| |_(_) ___ _ __ #
4# | '_ \| | | || |/ _ \ / _ \| | | '_ \ / _` || | | | | | / _ \ | '_ \/ __| __| '__/ _` |/ __| __| |/ _ \| '_ \ #
5# | |_) | |_| || | (_) | (_) | | | | | | (_| || |___| |___ | | / ___ \| |_) \__ \ |_| | | (_| | (__| |_| | (_) | | | | #
6# | .__/ \__, ||_|\___/ \___/|_|_|_| |_|\__, (_)____|_____|___/_/ \_\_.__/|___/\__|_| \__,_|\___|\__|_|\___/|_| |_| #
7# |_| |___/ |___/ #
8# ==================================================================================================================== #
9# Authors: #
10# Patrick Lehmann #
11# #
12# License: #
13# ==================================================================================================================== #
14# Copyright 2017-2025 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
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 def __new__(cls, *args: Any, **kwargs: Any):
80 if cls is CommandArgument:
81 raise TypeError(f"Class '{cls.__name__}' is abstract.")
82 return super().__new__(cls, *args, **kwargs)
85@export
86class ShortCommand(CommandArgument, pattern="-{0}"):
87 """
88 Represents a command name with a single dash.
90 **Example:**
92 * ``-command``
93 """
95 def __init_subclass__(cls, *args: Any, pattern: str = "-{0}", **kwargs: Any):
96 """
97 This method is called when a class is derived.
99 :param args: Any positional arguments.
100 :param pattern: This pattern is used to format an argument.
101 :param kwargs: Any keyword argument.
102 """
103 kwargs["pattern"] = pattern
104 super().__init_subclass__(*args, **kwargs)
106 def __new__(cls, *args: Any, **kwargs: Any):
107 if cls is ShortCommand:
108 raise TypeError(f"Class '{cls.__name__}' is abstract.")
109 return super().__new__(cls, *args, **kwargs)
112@export
113class LongCommand(CommandArgument, pattern="--{0}"):
114 """
115 Represents a command name with a double dash.
117 **Example:**
119 * ``--command``
120 """
122 def __init_subclass__(cls, *args: Any, pattern: str = "--{0}", **kwargs: Any):
123 """
124 This method is called when a class is derived.
126 :param args: Any positional arguments.
127 :param pattern: This pattern is used to format an argument.
128 :param kwargs: Any keyword argument.
129 """
130 kwargs["pattern"] = pattern
131 super().__init_subclass__(*args, **kwargs)
133 def __new__(cls, *args: Any, **kwargs: Any):
134 if cls is LongCommand:
135 raise TypeError(f"Class '{cls.__name__}' is abstract.")
136 return super().__new__(cls, *args, **kwargs)
139@export
140class WindowsCommand(CommandArgument, pattern="/{0}"):
141 """
142 Represents a command name with a single slash.
144 **Example:**
146 * ``/command``
147 """
149 def __init_subclass__(cls, *args: Any, pattern: str = "/{0}", **kwargs: Any):
150 """
151 This method is called when a class is derived.
153 :param args: Any positional arguments.
154 :param pattern: This pattern is used to format an argument.
155 :param kwargs: Any keyword argument.
156 """
157 kwargs["pattern"] = pattern
158 super().__init_subclass__(*args, **kwargs)
160 def __new__(cls, *args: Any, **kwargs: Any):
161 if cls is WindowsCommand:
162 raise TypeError(f"Class '{cls.__name__}' is abstract.")
163 return super().__new__(cls, *args, **kwargs)