-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmileArgs.py
More file actions
409 lines (343 loc) · 9.91 KB
/
SmileArgs.py
File metadata and controls
409 lines (343 loc) · 9.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
"""
Author: masakokh
Year: 2023
Package: library
Note:
Version: 1.1.2
"""
# built-in
import re
import sys
from typing import Any, Union
class CommandBase:
def __init__(self):
"""
"""
self.cmdLong = None
self.cmdShort = None
self.id = 0
self.priority = 0
self.value = None
class CommandAdd(CommandBase):
def __init__(self):
"""
"""
super().__init__()
self.description = ''
class CommandArgs(CommandBase):
def __init__(self):
"""
"""
super().__init__()
self.isLong = False
class SmileArgs:
"""
"""
class OptionPrefixSymbol:
"""
"""
LONG = '--'
SHORT = '-'
class OptionDelimiterSymbol:
"""
"""
COLON = ':'
EQUAL = '='
class Priority:
"""
"""
IGNORE = 0
LOW = 1
MEDIUM = 2
HIGH = 3
def __init__(self, isAllowedNoValue: bool= False, isDuplicated: bool= False, console: bool= True, debug: bool= False):
"""
:param isAllowedNoValue:
:param isDuplicated:
:param console:
:param debug:
"""
# private
self.__commandAssignedSymbol= self.OptionDelimiterSymbol.EQUAL
self.__commandList = []
## command
self.__acceptedDuplicate = isDuplicated
self.__args = []
self.__isAllowedNoValue = isAllowedNoValue
self.__isCmdLong = False
self.__isConsole = console
self.__isDebug = debug
## value
self.__id = 1
self.__numIncrease = 2
def __appendPrefixLong(self, command: str = None) -> Union[str | None]:
"""
:param command:
:return:
"""
return f'{self.OptionPrefixSymbol.LONG}{command}' if command else None
def __appendPrefixShort(self, command: str= None) -> Union[str | None]:
"""
:param command:
:return:
"""
return f'{self.OptionPrefixSymbol.SHORT}{command}' if command else None
def __console(self, func: str, content: str= None) -> None:
"""
:param func:
:param content:
:return:
"""
if self.__isConsole:
self.__print(f'{self.__class__.__name__}.{func}: {content}')
def __debug(self, func: str, content: str) -> None:
"""
:param func:
:param content:
:return:
"""
if self.__isDebug:
self.__print(f'{self.__class__.__name__}.{func} Debug: {content}')
def __hatchArg(self, arg: str, length: int= 1) -> None:
"""
:param arg:
:param length:
:return:
"""
# pattern
## equal --mom=miss or -m=miss or
## colom --mom:miss or -m:miss
_pW = '([(\-\-)(\-)]+\w+)(' + self.__commandAssignedSymbol + '?(\w+)?)'
_pS = '(^\-\w{' + str(length) + '}$)' # -m len(m) == 1
_pL = '(^\-\-\w.{' + str(length) + ',}$)' # --mom len(mom) > 1
#
cmd = ''
command = ''
isLong = False
value = ''
# round 1
try:
round1 = re.match(_pW, arg)
#
command = round1.groups()[0]
value = round1.groups()[2]
# round 2
try:
# set long command
isLong = True
round2 = re.match(_pL, command)
cmd = round2.groups()[0]
except Exception as e:
# round 3
try:
# set short command
isLong = False
round3 = re.match(_pS, command)
cmd = round3.groups()[0]
except Exception as e:
self.__debug(func= f'__hatchArg round 3 {command=}', content= f'Exception: {str(e)}')
cmd = ''
# valid command, and continue to find in the exist command
if cmd and not self.__isDuplicatedOnHatch(cmd):
# loop to find
for i in range(self.__commandList.__len__()):
# long condition
if isLong and self.__commandList[i].cmdLong == cmd:
item = CommandArgs()
# detail
item.cmdLong = cmd
item.id = self.__commandList[i].id
item.isLong = isLong
item.priority = self.__commandList[i].priority
item.value = value
# add to list
self.__args.append(item)
# short condition
elif isLong is False and self.__commandList[i].cmdShort == cmd:
item = CommandArgs()
# detail
item.cmdShort = cmd
item.id = self.__commandList[i].id
item.isLong = isLong
item.priority = self.__commandList[i].priority
item.value = value
# add to list
self.__args.append(item)
except Exception as e:
self.__debug(func= '__hatchArg round 1', content= f'Exception: {str(e)}')
def __isDuplicatedOnAdd(self, shortCommand: str= None, longCommand: str= None, appendPrefix: bool= True) -> bool:
"""
:param shortCommand:
:param longCommand:
:param appendPrefix:
:return:
"""
#
for i in range(self.__commandList.__len__()):
# with prefix - or --
if appendPrefix:
# self.__debug(f'__isDuplicatedOnAdd append prefix= {self.__appendPrefixShort(shortCommand)=}, {self.__commandList[i].cmdShort=}, {self.__commandList[i].cmdLong=} | {self.__appendPrefixLong(longCommand)=}, {self.__commandList[i].cmdLong=}')
return (self.__appendPrefixShort(shortCommand) == self.__commandList[i].cmdShort) or (self.__appendPrefixLong(longCommand) == self.__commandList[i].cmdLong)
else:
# self.__debug(f'__isDuplicatedOnAdd= {shortCommand=}, {self.__commandList[i].cmdShort=} | {longCommand=}, {self.__commandList[i].cmdLong=}')
return shortCommand == self.__commandList[i].cmdShort or longCommand == self.__commandList[i].cmdLong
#
return False
def __isDuplicatedOnHatch(self, command: str) -> bool:
"""
:param command:
:return:
"""
#
for i in range(self.__args.__len__()):
return command in [self.__args[i].cmdShort or self.__args[i].cmdLong]
#
return False
def __print(self, message: str) -> None:
"""
:param message:
:return:
"""
print(message)
def addCommand(self, shortCommand: str= None, longCommand: str= None, description: str= '', priority: int= Priority.IGNORE, overrideNum: list= []) -> None:
"""
:param shortCommand:
:param longCommand:
:param description:
:param priority:
:param overrideNum:
:return:
"""
# valid that
if longCommand or shortCommand:
# continue
_continue = False
# find any existed
if self.__isDuplicatedOnAdd(shortCommand= shortCommand, longCommand= longCommand):
#
if self.__acceptedDuplicate:
# accept
_continue = True
# not exist
else:
_continue = True
#
if _continue:
# increase power by 2
self.__id *= self.__numIncrease
# object
item = CommandAdd()
# item
item.description= description
# main key
item.cmdLong = f'{self.OptionPrefixSymbol.LONG}{longCommand}' if longCommand else None
item.cmdShort = f'{self.OptionPrefixSymbol.SHORT}{shortCommand}' if shortCommand else None
# special value
item.id = self.__id
item.priority = priority
# add to list
self.__commandList.append(item)
self.__console(f'Command Addition')
self.__console(f'short: {item.cmdShort}, long: {item.cmdLong}, description: {item.description=}')
def catchCommand(self) -> list:
"""
:return:
"""
return self.__args
def getCommand(self) -> list:
"""
:return:
"""
return self.__commandList
def getNumByCommand(self, command: str, addPrefix: bool= False) -> int:
"""
:param command:
:param addPrefix:
:return:
"""
for i in range(self.__commandList.__len__()):
#
if addPrefix:
if (self.__appendPrefixShort(command) == self.__commandList[i].cmdShort) or \
(self.__appendPrefixLong(command) == self.__commandList[i].cmdLong):
#
return self.__commandList[i].id
#
else:
# check with short and long command
if command in [self.__commandList[i].cmdShort, self.__commandList[i].cmdLong]:
# num or id
return self.__commandList[i].id
#
return 0
def removeCommand(self, command: str, appendPrefix: bool= True) -> None:
"""
:param command:
:param appendPrefix:
:return:
"""
# remove short or long command from list
try:
#
for i in range(self.__commandList.__len__()):
# found
found = False
# with prefix - or --
if appendPrefix:
if (self.__appendPrefixShort(command) == self.__commandList[i].cmdShort):
found = True
elif (self.__appendPrefixLong(command) == self.__commandList[i].cmdLong):
found = True
#
else:
if command == self.__commandList[i].cmdShort or command == self.__commandList[i].cmdLong:
found = True
#
if found:
self.__console(f'Command Removing')
self.__console(f'short: {self.__commandList[i].cmdShort}, long: {self.__commandList[i].cmdLong}, description: {self.__commandList[i].description=}')
except Exception as e:
self.__debug(func= 'removeCommand', content= f'Exception: {str(e)}')
def run(self) -> None:
"""
:return:
"""
# string token
for arg in sys.argv[1:]:
# verify
self.__hatchArg(arg)
def setAssignSymbol(self, symbol: str= OptionDelimiterSymbol.EQUAL) -> None:
"""
:param symbol:
:return:
"""
self.__commandAssignedSymbol = symbol
def show(self) -> None:
"""
:return:
"""
lineTable = '=========================================================='
lineRowFirst= '+--------------+------------------------------------------'
lineRowNext = '+--------------+---------------------'
# added
self.__print(f'{lineTable}')
self.__print(f'Print all added commands')
self.__print(f'Short Command \tLong Command \t\tDescription')
self.__print(f'{lineRowFirst}')
# loop to print all element
for a in self.getCommand():
self.__print(f' {a.cmdShort} \t\t{a.cmdLong} \n description: {a.description}')
self.__print(f'{lineRowNext if a != self.getCommand()[-1] else ""}')
self.__print(f'{lineRowFirst}')
#
if len(self.catchCommand()) > 0:
# caught
self.__print(f'{lineTable}')
self.__print(f'Print all caught commands')
self.__print(f'Short/Long Command \tValue')
self.__print(f'{lineRowFirst}')
# loop to print all element
for c in self.catchCommand():
self.__print(f' {c.cmdShort or c.cmdLong} \t\t{c.value or ""}')
self.__print(f'{lineRowNext if c != self.catchCommand()[-1] else ""}')
self.__print(f'{lineRowFirst}')