Passed
Push — master ( b23eaf...087ae8 )
by Daniel
01:44
created

CommandLineArgumentsManagement.listing_parameter_values()   A

Complexity

Conditions 3

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 20
rs 9.7
c 0
b 0
f 0
cc 3
nop 4
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
8
import argparse
9
10
11
class CommandLineArgumentsManagement:
12
13
    @staticmethod
14
    def listing_parameter_values(self, local_logger, configuration_details, given_parameter_values):
15
        local_logger.info('~' * 50)
16
        local_logger.info('Overview of input parameter given values')
17
        local_logger.info('~' * 50)
18
        parameter_values_dictionary = given_parameter_values.__dict__
19
        for input_key, attributes in configuration_details.items():
20
            # checking first if short key was provided, otherwise consider longer
21
            if input_key in parameter_values_dictionary:
22
                key_value_to_consider = input_key
23
            else:
24
                key_value_to_consider = attributes['option_long'].replace('-', '_')
25
            # having the key consider we determine the value of the current parameter
26
            value_to_consider = parameter_values_dictionary[key_value_to_consider]
27
            # we build the parameter feedback considering "option_description"
28
            # and replacing %s with parameter value
29
            feedback = attributes['option_description'] % value_to_consider
30
            # we finally write the feedback to logger
31
            local_logger.info(feedback)
32
        local_logger.info('~' * 50)
33
34
    def parse_arguments(self, configuration_details):
35
        parser = argparse.ArgumentParser()
36
        for input_key, attributes in configuration_details.items():
37
            action_value = self.translate_default_to_action(attributes['default_value'])
38
            if action_value is None:
39
                parser.add_argument('-' + input_key, '--' + attributes['option_long'],
40
                                    required=attributes['option_required'],
41
                                    default=attributes['default_value'],
42
                                    help=attributes['option_sample_value'])
43
            else:
44
                parser.add_argument('-' + input_key, '--' + attributes['option_long'],
45
                                    required=attributes['option_required'],
46
                                    default=attributes['default_value'],
47
                                    action=action_value)
48
        parser.add_argument('-v', '--verbose', required=False, default=False, action='store_true')
49
        return parser.parse_args()
50
51
    @staticmethod
52
    def translate_default_to_action(given_default_value):
53
        if given_default_value == True:
54
            return 'store_true'
55
        elif given_default_value == False:
56
            return 'store_false'
57
        else:
58
            return None
59