Coverage for pyTooling/Attributes/ArgParse/KeyValueFlag.py: 48%
21 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-03 23:02 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-03 23:02 +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 2007-2016 Patrick Lehmann - Dresden, Germany #
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"""
33Attributes describing options carrying key-value-pairs, like ``-Dkey=value``.
35The pairs are collected into a dictionary and passed to the handler method as one parameter.
36"""
37from typing import Optional as Nullable
39from pyTooling.Decorators import export
40from pyTooling.Attributes.ArgParse.Argument import NamedAndValuedArgument
43@export
44class NamedKeyValuePairsArgument(NamedAndValuedArgument):
45 """
46 Defines a key-value argument like ``-Dkey=value``.
48 Some of the named parameters passed to :meth:`~ArgumentParser.add_argument` are predefined (or overwritten) to create
49 a boolean parameter passed to the registered handler method. The boolean parameter is ``True`` if the switch argument
50 is present in the commandline arguments, otherwise ``False``.
51 """
53 def __init__(self, short: Nullable[str] = None, long: Nullable[str] = None, dest: Nullable[str] = None, help: Nullable[str] = None) -> None:
54 """
55 Initializes a flag argument.
57 .. admonition:: ArgParse parameterization
59 :meth:`~argparse.ArgumentParser.add_argument` is called with the option strings from ``short`` and ``long``
60 as positional parameters, and with these named parameters:
62 * ``dest=dest``
63 * ``action="store_const"``
64 * ``const=True``
65 * ``default=False``
66 * ``help=help``
68 ``action="store_const"`` with ``const=True`` and ``default=False`` is what makes the handler method receive a
69 boolean.
71 .. attention::
73 This is exactly :class:`~pyTooling.Attributes.ArgParse.Flag.FlagArgument`'s parameterization, so no
74 key-value pair is parsed and the handler method receives a boolean. See the class doc-string.
76 :param short: Optional, short option name including the dash, e.g. ``"-v"``. Default: ``None``.
77 :param long: Optional, long option name including the dashes, e.g. ``"--verbose"``. Default: ``None``.
78 :param dest: Name the parsed value is stored under in the :class:`~argparse.Namespace`, and the name the handler
79 method reads it by.
80 :param help: Optional, help text shown for this argument in the help page. Default: ``None``.
81 """
82 args = []
83 if short is not None:
84 args.append(short)
86 if long is not None:
87 args.append(long)
89 kwargs = {
90 "dest": dest,
91 "action": "store_const",
92 "const": True,
93 "default": False,
94 "help": help,
95 }
96 super().__init__(*args, **kwargs)
99@export
100class ShortKeyValueFlag(NamedKeyValuePairsArgument):
101 """Defines a key-value argument in short form like ``-Dkey=value``."""
103 def __init__(self, short: Nullable[str] = None, dest: Nullable[str] = None, help: Nullable[str] = None) -> None:
104 """
105 Initialize a key-value argument in its short form.
107 :param short: Optional, short form of the option, including its dash, e.g. :pycode:`short="-v"`.
108 :param dest: Optional, name of the parameter the parsed value is passed to the handler method as.
109 :param help: Optional, help text of the option, displayed in the help page.
110 """
111 super().__init__(short=short, dest=dest, help=help)
114@export
115class LongKeyValueFlag(NamedKeyValuePairsArgument):
116 """Defines a key-value argument in long form like ``--define key=value``."""
118 def __init__(self, long: Nullable[str] = None, dest: Nullable[str] = None, help: Nullable[str] = None) -> None:
119 """
120 Initialize a key-value argument in its long form.
122 :param long: Optional, long form of the option, including its dashes, e.g. :pycode:`long="--verbose"`.
123 :param dest: Optional, name of the parameter the parsed value is passed to the handler method as.
124 :param help: Optional, help text of the option, displayed in the help page.
125 """
126 super().__init__(long=long, dest=dest, help=help)