Conditions | 7 |
Total Lines | 57 |
Lines | 0 |
Ratio | 0 % |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | import os |
||
11 | def parse_cli(arg_list=None, |
||
12 | origin=os.getcwd(), |
||
13 | arg_parser=None, |
||
14 | key_value_delimiters=('=', ':'), |
||
15 | comment_seperators=(), |
||
16 | key_delimiters=(',',), |
||
17 | section_override_delimiters=(".",)): |
||
18 | """ |
||
19 | Parses the CLI arguments and creates sections out of it. |
||
20 | |||
21 | :param arg_list: The CLI argument list. |
||
22 | :param origin: Directory used to interpret relative |
||
23 | paths given as argument. |
||
24 | :param arg_parser: Instance of ArgParser that is used to |
||
25 | parse none-setting arguments. |
||
26 | :param key_value_delimiters: Delimiters to separate key and value |
||
27 | in setting arguments. |
||
28 | :param comment_seperators: Allowed prefixes for comments. |
||
29 | :param key_delimiters: Delimiter to separate multiple keys of |
||
30 | a setting argument. |
||
31 | :param section_override_delimiters: The delimiter to delimit the section |
||
32 | from the key name (e.g. the '.' in |
||
33 | sect.key = value). |
||
34 | :return: A dictionary holding section names |
||
35 | as keys and the sections themselves |
||
36 | as value. |
||
37 | """ |
||
38 | # Note: arg_list can also be []. Hence we cannot use |
||
39 | # `arg_list = arg_list or default_list` |
||
40 | arg_list = sys.argv[1:] if arg_list is None else arg_list |
||
|
|||
41 | arg_parser = arg_parser or default_arg_parser() |
||
42 | origin += os.path.sep |
||
43 | sections = OrderedDict(default=Section('Default')) |
||
44 | line_parser = LineParser(key_value_delimiters, |
||
45 | comment_seperators, |
||
46 | key_delimiters, |
||
47 | {}, |
||
48 | section_override_delimiters) |
||
49 | |||
50 | for arg_key, arg_value in sorted( |
||
51 | vars(arg_parser.parse_args(arg_list)).items()): |
||
52 | if arg_key == 'settings' and arg_value is not None: |
||
53 | parse_custom_settings(sections, |
||
54 | arg_value, |
||
55 | origin, |
||
56 | line_parser) |
||
57 | else: |
||
58 | if isinstance(arg_value, list): |
||
59 | arg_value = ",".join([str(val) for val in arg_value]) |
||
60 | |||
61 | append_to_sections(sections, |
||
62 | arg_key, |
||
63 | arg_value, |
||
64 | origin, |
||
65 | from_cli=True) |
||
66 | |||
67 | return sections |
||
68 | |||
113 |