-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_parameters.py
181 lines (156 loc) · 7.51 KB
/
simple_parameters.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
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
from optparse import OptionParser
import json, os
'''
Mandatory -
A short name is mandatory to get started
Additionally, a long name, type, default value and help text can be specified
in the options configuration file
Any option without a type is taken as a boolean
Anything with a type must have additional parameter following it
'''
class SimpleParser():
OPTIONS_FILE = 'simple_parameters.json'
VERSION = "Your version goes here. Specify it in the options file"
DESCRIPTION= "A small description of the application goes here. Specify it in the options file"
def __init__(self, options_file = OPTIONS_FILE,
version = VERSION,
description = DESCRIPTION):
#Open the options file and load all the options into json
if os.path.isfile(options_file):
with open(options_file) as infile:
self.all_options = json.load(infile)
if(self.all_options.get('version')):
version = self.all_options.get('version')
if(self.all_options.get('description')):
description = self.all_options.get('description')
self.all_options = self.all_options.get("parameters")
#Create an option parser
self.parser = OptionParser(version = version, description = description)
#Parse through each of the options one by one
if isinstance(self.all_options, dict):
for key, option in self.all_options.items():
self.parse_each_option(option)
elif isinstance(self.all_options, list):
for option in self.all_options:
self.parse_each_option(option)
else:
raise Exception("Options file {} not found.".format(options_file))
def parse_each_option(self, option):
long = option.get('long')
default = option.get('default')
help = option.get('help')
#If the type of the variable is not specified, it is a flag
if not(option.get('type')):
#Store the data into the varible, same as store
action = "store_true"
#Deduce the default value of the variable
if not(isinstance(default, bool)):
if default == 'true' or default == 'True'\
or default == 'Yes' or default == 'yes':
default=True
elif default == 'false' or default == 'False'\
or default == 'no' or default == 'No':
default=False
else:
default = false
#Consider the cases of various optional parameters not being specified
if long and default and help:
self.parser.add_option(option['short'], long
,dest=self.get_var_name(option)
,default = default, action = action
, help=option['help'])
elif default and help :
self.parser.add_option(option['short']
,dest=self.get_var_name(option)
,default = default, action = action
, help=option['help'])
elif long and help:
self.parser.add_option(option['short'], long
,dest=self.get_var_name(option)
, action = action
, help=option['help'])
elif long and default:
self.parser.add_option(option['short'], long
,dest=self.get_var_name(option)
,default = default, action = action)
elif default:
self.parser.add_option(option['short']
,dest=self.get_var_name(option)
,default = default, action = action)
elif long:
self.parser.add_option(option['short'], long
,dest=self.get_var_name(option), action = action)
else:
self.parser.add_option(option['short']
,dest=self.get_var_name(option)
, action = action)
else:
'''
Likely types include string, int, float which should be stored in the
destination variable derived from long/short name
Consider the various optional attributes being specified or not
'''
action = 'store'
if long and default and help:
self.parser.add_option(option['short'], long
,dest=self.get_var_name(option)
, type=option.get('type')
,default = default, action = action
, help=option['help'])
elif default and help :
self.parser.add_option(option['short']
,dest=self.get_var_name(option)
,default = default, action = action
, type=option.get('type')
, help=option['help'])
elif long and help:
self.parser.add_option(option['short'], long
,dest=self.get_var_name(option)
, action = action
, type=option.get('type')
, help=option['help'])
elif long and default:
self.parser.add_option(option['short'], long
,dest=self.get_var_name(option)
, type=option.get('type')
,default = default, action = action)
elif default:
self.parser.add_option(option['short']
,dest=self.get_var_name(option)
, type=option.get('type')
,default = default, action = action)
elif long:
self.parser.add_option(option['short'], long
, type=option.get('type')
,dest=self.get_var_name(option), action = action)
else:
self.parser.add_option(option['short']
, type=option.get('type')
,dest=self.get_var_name(option)
, action = action)
return
def get_var_name(self, options):
#Create the name of the variable from the name of the option
if options.get("long"):
str(options.get("long")).strip("--").replace("-","_")
elif options.get("short"):
str(options.get("short")).strip("-")
return
def resolve_parameters(self, params):
return self.parser.parse_args(params)
#---------------------------------------------------------------
#Just run a basic test
if __name__=="__main__":
#Execute the parse after all the options have been added
import sys
try:
parser = SimpleParser()
(options, args) =\
parser.resolve_parameters(sys.argv)
if(options.file):
parser = SimpleParser(options.file)
(options, args) =\
parser.resolve_parameters(args)
parser.parser.print_help()
except Exception as ex:
print(ex)