-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_flags.py
74 lines (52 loc) · 1.58 KB
/
parse_flags.py
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
from show_animations import option_values
from tools import show_help
import sys
import os
# Variables
received_flags = sys.argv
animation_flags = {
'filler_flags': ["--filler", "-f"],
'saver_flags': ["--saver", "-s"],
'player_flags': ["--player", "-p"]
}
flag_values = {
'filler': 'cat',
'saver': 'flat',
'player': 'cava'
}
options = ["cat", "waves", "cava", "info", "splash", "empty", "flat"]
# Functions
def detect_category(fl):
for category_flags in animation_flags:
if fl in animation_flags[category_flags]:
category = animation_flags[category_flags][0][2:]
break
return category
def parse_flag(fl, opt):
category = detect_category(fl)
flag_values[category] = opt
def parse_option_with_argument(fl, opt):
try:
opt_value = int(opt.split('=')[-1])
except:
opt_value = opt.split('=')[-1]
opt_name = opt.split('=')[0]
parse_flag(fl, opt_name)
opt_name_options = opt_name + '_options'
category = detect_category(fl)
option_values[f'{opt_name_options}'][f'{category}_{opt_name}_option'] = opt_value
# Parsing
for index, fl in enumerate(received_flags, 0):
if index == 0 or fl in options:
continue
elif fl == "--help" or fl == "-h":
show_help()
exit()
elif "=" in fl:
parse_option_with_argument(received_flags[index - 1], fl)
else:
try:
parse_flag(fl, received_flags[index + 1])
except:
os.system("""echo "Something is incorrect. Use '--help' for more information" """)
exit()