Coverage for src/cli/main.py: 100.00%

80 statements  

« prev     ^ index     » next       coverage.py v7.17.2, created at 2026-11-07 23:52 +0000

1#!/usr/bin/env python3 

2# PYTHON_ARGCOMPLETE_OK 

3 

4# Standard libraries 

5from argparse import ( 

6 _ArgumentGroup, 

7 _MutuallyExclusiveGroup, 

8 ArgumentParser, 

9 Namespace, 

10 RawTextHelpFormatter, 

11) 

12from os import environ 

13from shutil import get_terminal_size 

14from sys import exit as sys_exit 

15 

16# Modules libraries 

17from argcomplete import autocomplete 

18 

19# Components 

20from ..package.bundle import Bundle 

21from ..package.settings import Settings 

22from ..package.updates import Updates 

23from ..package.version import Version 

24from ..prints.colors import Colors 

25from ..system.platform import Platform 

26from .entrypoint import Entrypoint 

27 

28# Constants 

29HELP_POSITION: int = 23 

30 

31# Main, pylint: disable=too-many-branches,too-many-statements 

32def main() -> None: 

33 

34 # Variables 

35 group: _ArgumentGroup 

36 result: Entrypoint.Result = Entrypoint.Result.ERROR 

37 subgroup: _MutuallyExclusiveGroup 

38 

39 # Arguments creation 

40 parser: ArgumentParser = ArgumentParser( 

41 prog=Bundle.NAME, 

42 description=f'{Bundle.NAME}: {Bundle.DESCRIPTION}', 

43 add_help=False, 

44 formatter_class=lambda prog: RawTextHelpFormatter( 

45 prog, 

46 max_help_position=HELP_POSITION, 

47 width=min( 

48 120, 

49 get_terminal_size().columns - 2, 

50 ), 

51 ), 

52 ) 

53 

54 # Arguments internal definitions 

55 group = parser.add_argument_group('internal arguments') 

56 group.add_argument( 

57 '-h', 

58 '--help', 

59 dest='help', 

60 action='store_true', 

61 help='Show this help message', 

62 ) 

63 group.add_argument( 

64 '--version', 

65 dest='version', 

66 action='store_true', 

67 help='Show the current version', 

68 ) 

69 group.add_argument( 

70 '--no-color', 

71 dest='no_color', 

72 action='store_true', 

73 help=f'Disable colors outputs with \'{Bundle.ENV_NO_COLOR}=1\'\n' 

74 '(or default settings: [themes] > no_color)', 

75 ) 

76 group.add_argument( 

77 '--update-check', 

78 dest='update_check', 

79 action='store_true', 

80 help='Check for newer package updates', 

81 ) 

82 group.add_argument( 

83 '--settings', 

84 dest='settings', 

85 action='store_true', 

86 help='Show the current settings path and contents', 

87 ) 

88 group.add_argument( 

89 '--set', 

90 dest='set', 

91 action='store', 

92 metavar=('GROUP', 'KEY', 'VAL'), 

93 nargs=3, 

94 help='Set settings specific \'VAL\' value to [GROUP] > KEY\n' \ 

95 'or unset by using \'UNSET\' as \'VAL\'', 

96 ) 

97 

98 # Arguments modes definitions 

99 group = parser.add_argument_group('modes arguments') 

100 subgroup = group.add_mutually_exclusive_group() 

101 subgroup.add_argument( 

102 '-e', 

103 '--enable', 

104 dest='enable', 

105 action='store_true', 

106 help='Enable profiles configurations', 

107 ) 

108 subgroup.add_argument( 

109 '-d', 

110 '--disable', 

111 dest='disable', 

112 action='store_true', 

113 help='Disable profiles configurations', 

114 ) 

115 

116 # Arguments positional definitions 

117 group = parser.add_argument_group('positional arguments') 

118 group.add_argument( 

119 '--', 

120 dest='double_dash', 

121 action='store_true', 

122 help='Positional arguments separator (recommended)', 

123 ) 

124 

125 # Arguments autocompletion 

126 autocomplete(parser) 

127 

128 # Arguments parser 

129 options: Namespace = parser.parse_args() 

130 

131 # Help informations 

132 if options.help: 

133 print(' ') 

134 parser.print_help() 

135 print(' ') 

136 Platform.flush() 

137 sys_exit(0) 

138 

139 # Instantiate settings 

140 settings: Settings = Settings(name=Bundle.NAME) 

141 

142 # Prepare no_color 

143 if not options.no_color: 

144 if settings.has('themes', 'no_color'): 

145 options.no_color = settings.get_bool('themes', 'no_color') 

146 else: 

147 options.no_color = False 

148 settings.set_bool('themes', 'no_color', options.no_color) 

149 

150 # Configure no_color 

151 if options.no_color: 

152 environ[Bundle.ENV_FORCE_COLOR] = '0' 

153 environ[Bundle.ENV_NO_COLOR] = '1' 

154 

155 # Prepare colors 

156 Colors.prepare() 

157 

158 # Settings setter 

159 if options.set: 

160 settings.set(options.set[0], options.set[1], options.set[2]) 

161 settings.show() 

162 sys_exit(0) 

163 

164 # Settings informations 

165 if options.settings: 

166 settings.show() 

167 sys_exit(0) 

168 

169 # Instantiate updates 

170 updates: Updates = Updates( 

171 name=Bundle.PACKAGE, 

172 settings=settings, 

173 ) 

174 

175 # Version informations 

176 if options.version: 

177 print( 

178 f'{Bundle.NAME} {Version.get()} from {Version.path()} (python {Version.python()})' 

179 ) 

180 Platform.flush() 

181 sys_exit(0) 

182 

183 # Check for current updates 

184 if options.update_check: 

185 if not updates.check(): 

186 updates.check(older=True) 

187 sys_exit(0) 

188 

189 # Arguments modes 

190 if not options.enable and not options.disable: 

191 result = Entrypoint.Result.CRITICAL 

192 

193 # Header 

194 print(' ') 

195 Platform.flush() 

196 

197 # Tool identifier 

198 if result != Entrypoint.Result.CRITICAL: 

199 print(f'{Colors.BOLD} {Bundle.NAME}' 

200 f'{Colors.YELLOW_LIGHT} ({Version.get()})' 

201 f'{Colors.RESET}') 

202 Platform.flush() 

203 

204 # CLI entrypoint 

205 if result != Entrypoint.Result.CRITICAL: 

206 result = Entrypoint.cli(options) 

207 

208 # CLI helper 

209 else: 

210 parser.print_help() 

211 

212 # Footer 

213 print(' ') 

214 Platform.flush() 

215 

216 # Check for daily updates 

217 if updates.enabled and updates.daily: 

218 updates.check() 

219 

220 # Result 

221 if result in [ 

222 Entrypoint.Result.SUCCESS, 

223 Entrypoint.Result.FINALIZE, 

224 ]: 

225 sys_exit(0) 

226 else: 

227 sys_exit(1) 

228 

229# Entrypoint 

230if __name__ == '__main__': # pragma: no cover 

231 main()