1
|
|
|
""" |
2
|
|
|
CommandLineArgumentManagement - library to manage input parameters from command line |
3
|
|
|
|
4
|
|
|
This library allows handling pre-configured arguments to be received from command line and use them |
5
|
|
|
to call the main package functions |
6
|
|
|
""" |
7
|
|
|
# package to handle arguments from command line |
8
|
|
|
import argparse |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
class CommandLineArgumentsManagement: |
12
|
|
|
|
13
|
|
|
@staticmethod |
14
|
|
|
def listing_parameter_values(local_logger, timmer, title, configuration_details, |
15
|
|
|
given_parameter_values): |
16
|
|
|
timmer.start() |
17
|
|
|
local_logger.info('='*50) |
18
|
|
|
local_logger.info(title + ' has started') |
19
|
|
|
local_logger.info('~' * 50) |
20
|
|
|
local_logger.info('Overview of input parameter given values') |
21
|
|
|
local_logger.info('~' * 50) |
22
|
|
|
parameter_values_dictionary = given_parameter_values.__dict__ |
23
|
|
|
for input_key, attributes in configuration_details.items(): |
24
|
|
|
# checking first if short key was provided, otherwise consider longer |
25
|
|
|
if input_key in parameter_values_dictionary: |
26
|
|
|
key_value_to_consider = input_key |
27
|
|
|
else: |
28
|
|
|
key_value_to_consider = attributes['option_long'].replace('-', '_') |
29
|
|
|
# having the key consider we determine the value of the current parameter |
30
|
|
|
value_to_consider = parameter_values_dictionary[key_value_to_consider] |
31
|
|
|
# we build the parameter feedback considering "option_description" |
32
|
|
|
# and replacing %s with parameter value |
33
|
|
|
feedback = attributes['option_description'] % value_to_consider |
34
|
|
|
# we finally write the feedback to logger |
35
|
|
|
local_logger.info(feedback) |
36
|
|
|
local_logger.info('~' * 50) |
37
|
|
|
timmer.stop() |
38
|
|
|
|
39
|
|
|
def parse_arguments(self, configuration_details): |
40
|
|
|
parser = argparse.ArgumentParser() |
41
|
|
|
for input_key, attributes in configuration_details.items(): |
42
|
|
|
action_value = self.translate_default_to_action(attributes['default_value']) |
43
|
|
|
if action_value is None: |
44
|
|
|
parser.add_argument('-' + input_key, '--' + attributes['option_long'], |
45
|
|
|
required=attributes['option_required'], |
46
|
|
|
default=attributes['default_value'], |
47
|
|
|
help=attributes['option_sample_value']) |
48
|
|
|
else: |
49
|
|
|
parser.add_argument('-' + input_key, '--' + attributes['option_long'], |
50
|
|
|
required=attributes['option_required'], |
51
|
|
|
default=attributes['default_value'], |
52
|
|
|
action=action_value) |
53
|
|
|
parser.add_argument('-v', '--verbose', required=False, default=False, action='store_true') |
54
|
|
|
return parser.parse_args() |
55
|
|
|
|
56
|
|
|
@staticmethod |
57
|
|
|
def translate_default_to_action(given_default_value): |
58
|
|
|
if given_default_value is True: |
59
|
|
|
return 'store_true' |
60
|
|
|
elif given_default_value is False: |
61
|
|
|
return 'store_false' |
62
|
|
|
else: |
63
|
|
|
return None |
64
|
|
|
|