Coverage for pyTooling/Attributes/ArgParse/KeyValueFlag.py: 50%

22 statements  

« 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 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# 

32from typing import Optional as Nullable 

33 

34try: 

35 from pyTooling.Decorators import export 

36 from pyTooling.Attributes.ArgParse.Argument import NamedAndValuedArgument 

37except (ImportError, ModuleNotFoundError): # pragma: no cover 

38 print("[pyTooling.Attributes.ArgParse.KeyValueFlag] Could not import from 'pyTooling.*'!") 

39 

40 try: 

41 from Decorators import export 

42 from Attributes.ArgParse.Argument import NamedAndValuedArgument 

43 except (ImportError, ModuleNotFoundError) as ex: # pragma: no cover 

44 print("[pyTooling.Attributes.ArgParse.KeyValueFlag] Could not import directly!") 

45 raise ex 

46 

47 

48@export 

49class NamedKeyValuePairsArgument(NamedAndValuedArgument): 

50 """ 

51 Defines a switch argument like ``--help``. 

52 

53 Some of the named parameters passed to :meth:`~ArgumentParser.add_argument` are predefined (or overwritten) to create 

54 a boolean parameter passed to the registered handler method. The boolean parameter is ``True`` if the switch argument 

55 is present in the commandline arguments, otherwise ``False``. 

56 """ 

57 

58 def __init__(self, short: Nullable[str] = None, long: Nullable[str] = None, dest: Nullable[str] = None, help: Nullable[str] = None): 

59 """ 

60 The constructor expects positional (``*args``), the destination parameter name ``dest`` and/or named parameters 

61 (``**kwargs``) which are passed to :meth:`~ArgumentParser.add_argument`. 

62 

63 To implement a switch argument, the following named parameters are predefined: 

64 

65 * ``action="store_const"`` 

66 * ``const=True`` 

67 * ``default=False`` 

68 

69 This implements a boolean parameter passed to the handler method. 

70 """ 

71 args = [] 

72 if short is not None: 

73 args.append(short) 

74 if long is not None: 

75 args.append(long) 

76 

77 kwargs = { 

78 "dest": dest, 

79 "action": "store_const", 

80 "const": True, 

81 "default": False, 

82 "help": help, 

83 } 

84 super().__init__(*args, **kwargs) 

85 

86 

87@export 

88class ShortKeyValueFlag(NamedKeyValuePairsArgument): 

89 def __init__(self, short: Nullable[str] = None, dest: Nullable[str] = None, help: Nullable[str] = None): 

90 super().__init__(short=short, dest=dest, help=help) 

91 

92 

93@export 

94class LongKeyValueFlag(NamedKeyValuePairsArgument): 

95 def __init__(self, long: Nullable[str] = None, dest: Nullable[str] = None, help: Nullable[str] = None): 

96 super().__init__(long=long, dest=dest, help=help)