Coverage for pyTooling/CLIAbstraction/Flag.py: 100%

37 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 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""" 

33Flag arguments represent simple boolean values by being present or absent. 

34 

35.. seealso:: 

36 

37 * For flags with different pattern based on the boolean value itself. |br| 

38 |rarr| :mod:`~pyTooling.CLIAbstraction.BooleanFlag` 

39 * For flags with a value. |br| 

40 |rarr| :mod:`~pyTooling.CLIAbstraction.ValuedFlag` 

41 * For flags that have an optional value. |br| 

42 |rarr| :mod:`~pyTooling.CLIAbstraction.NamedOptionalValuedFlag` 

43""" 

44from typing import Any 

45 

46try: 

47 from pyTooling.Decorators import export 

48 from pyTooling.CLIAbstraction.Argument import NamedArgument 

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

50 print("[pyTooling.Versioning] Could not import from 'pyTooling.*'!") 

51 

52 try: 

53 from Decorators import export 

54 from CLIAbstraction.Argument import NamedArgument 

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

56 print("[pyTooling.Versioning] Could not import directly!") 

57 raise ex 

58 

59 

60@export 

61class FlagArgument(NamedArgument): 

62 """ 

63 Base-class for all Flag classes, which represents a simple flag argument like ``-v`` or ``--verbose``. 

64 

65 A simple flag is a single value (absent/present or off/on) with no additional data (value). 

66 """ 

67 

68 def __new__(cls, *args: Any, **kwargs: Any): 

69 if cls is FlagArgument: 

70 raise TypeError(f"Class '{cls.__name__}' is abstract.") 

71 return super().__new__(cls, *args, **kwargs) 

72 

73 

74@export 

75class ShortFlag(FlagArgument, pattern="-{0}"): 

76 """ 

77 Represents a :class:`~pyTooling.CLIAbstraction.Flag.Flag` argument with a single dash. 

78 

79 **Example:** 

80 

81 * ``-optimize`` 

82 """ 

83 

84 def __init_subclass__(cls, *args: Any, pattern: str = "-{0}", **kwargs: Any): 

85 kwargs["pattern"] = pattern 

86 super().__init_subclass__(*args, **kwargs) 

87 

88 def __new__(cls, *args: Any, **kwargs: Any): 

89 if cls is ShortFlag: 

90 raise TypeError(f"Class '{cls.__name__}' is abstract.") 

91 return super().__new__(cls, *args, **kwargs) 

92 

93 

94@export 

95class LongFlag(FlagArgument, pattern="--{0}"): 

96 """ 

97 Represents a :class:`~pyTooling.CLIAbstraction.Flag.Flag` argument with a double dash. 

98 

99 **Example:** 

100 

101 * ``--optimize`` 

102 """ 

103 

104 def __init_subclass__(cls, *args: Any, pattern: str = "--{0}", **kwargs: Any): 

105 kwargs["pattern"] = pattern 

106 super().__init_subclass__(*args, **kwargs) 

107 

108 def __new__(cls, *args: Any, **kwargs: Any): 

109 if cls is LongFlag: 

110 raise TypeError(f"Class '{cls.__name__}' is abstract.") 

111 return super().__new__(cls, *args, **kwargs) 

112 

113 

114@export 

115class WindowsFlag(FlagArgument, pattern="/{0}"): 

116 """ 

117 Represents a :class:`~pyTooling.CLIAbstraction.Flag.Flag` argument with a single slash. 

118 

119 **Example:** 

120 

121 * ``/optimize`` 

122 """ 

123 

124 def __init_subclass__(cls, *args: Any, pattern: str = "/{0}", **kwargs: Any): 

125 kwargs["pattern"] = pattern 

126 super().__init_subclass__(*args, **kwargs) 

127 

128 def __new__(cls, *args: Any, **kwargs: Any): 

129 if cls is WindowsFlag: 

130 raise TypeError(f"Class '{cls.__name__}' is abstract.") 

131 return super().__new__(cls, *args, **kwargs)