1
|
|
|
import json |
2
|
|
|
import os.path |
3
|
|
|
import sys |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
class CommandLineArgumentsHandling: |
7
|
|
|
config_details = [] |
8
|
|
|
|
9
|
|
|
def fn_assess_option(self, current_option, established_default_value): |
10
|
|
|
if self.config_details['options'][current_option]['default_value'] == established_default_value: |
11
|
|
|
str_feedback_parts = { |
12
|
|
|
'prefix': 'Fatal Error', |
13
|
|
|
'verdict': 'Expected -' + current_option + '|--' |
14
|
|
|
+ self.config_details['options'][current_option]['option_long'] |
15
|
|
|
+ ' <' + self.config_details['options'][current_option]['option_sample_value'] |
16
|
|
|
+ '> but nothing of that sort has been seen...', |
17
|
|
|
'suffix': ':-(' |
18
|
|
|
} |
19
|
|
|
print(str_feedback_parts['prefix'] |
20
|
|
|
+ '_'*(len(str_feedback_parts['verdict']) - len(str_feedback_parts['prefix']))) |
21
|
|
|
print(str_feedback_parts['verdict'] + ' ' + str_feedback_parts['suffix']) |
22
|
|
|
sys.exit(2) |
23
|
|
|
print('Input file is "' + established_default_value + '"') |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
def fn_build_combined_options(self): |
27
|
|
|
str_combined_options = '' |
28
|
|
|
for option_index, current_option in enumerate(self.config_details['options']): |
29
|
|
|
if current_option is not None: |
30
|
|
|
str_option_crt = '-' + current_option + '|' \ |
31
|
|
|
+ self.config_details['options'][current_option]['option_long'] |
32
|
|
|
if 'option_sample_value' in self.config_details['options'][current_option]: |
33
|
|
|
str_option_crt += ' <' \ |
34
|
|
|
+ self.config_details['options'][current_option]['option_sample_value'] + '>' |
35
|
|
|
if self.config_details['options'][current_option]['option_type'] == 'optional': |
36
|
|
|
str_combined_options += ' [' + str_option_crt + '|if omitted default value will be considered: ' \ |
37
|
|
|
+ str(self.config_details['options'][current_option]['default_value']) \ |
38
|
|
|
+ ']' |
39
|
|
|
else: |
40
|
|
|
str_combined_options += ' ' + str_option_crt |
41
|
|
|
return str_combined_options |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
def fn_build_long_options(self): |
45
|
|
|
str_long_options = [] |
46
|
|
|
for option_index, current_long_option in enumerate(self.config_details['options']): |
47
|
|
|
if current_long_option is not None: |
48
|
|
|
str_long_options.append(option_index) |
49
|
|
|
str_long_options[option_index] = self.config_details['options'][current_long_option]['option_long'] \ |
50
|
|
|
+ '=' |
51
|
|
|
return str_long_options |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
def fn_build_short_options(self): |
55
|
|
|
str_short_options = 'h' |
56
|
|
|
for current_short_option in self.config_details['options']: |
57
|
|
|
if current_short_option is not None: |
58
|
|
|
if self.config_details['options'][current_short_option]['option_type'] == 'mandatory': |
59
|
|
|
str_short_options += current_short_option + ':' |
60
|
|
|
else: |
61
|
|
|
str_short_options += current_short_option |
62
|
|
|
return str_short_options |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
def fn_load_configuration(self): |
66
|
|
|
with open(os.path.dirname(__file__) + "/config.json", 'r') as json_file: |
67
|
|
|
self.config_details = json.load(json_file) |
68
|
|
|
|