| 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 | from collections import OrderedDict |
||
| 10 | def parse_cli(arg_list=None, |
||
| 11 | origin=os.getcwd(), |
||
| 12 | arg_parser=None, |
||
| 13 | key_value_delimiters=('=', ':'), |
||
| 14 | comment_seperators=(), |
||
| 15 | key_delimiters=(',',), |
||
| 16 | section_override_delimiters=(".",)): |
||
| 17 | """ |
||
| 18 | Parses the CLI arguments and creates sections out of it. |
||
| 19 | |||
| 20 | :param arg_list: The CLI argument list. |
||
| 21 | :param origin: Directory used to interpret relative |
||
| 22 | paths given as argument. |
||
| 23 | :param arg_parser: Instance of ArgParser that is used to |
||
| 24 | parse none-setting arguments. |
||
| 25 | :param key_value_delimiters: Delimiters to separate key and value |
||
| 26 | in setting arguments. |
||
| 27 | :param comment_seperators: Allowed prefixes for comments. |
||
| 28 | :param key_delimiters: Delimiter to separate multiple keys of |
||
| 29 | a setting argument. |
||
| 30 | :param section_override_delimiters: The delimiter to delimit the section |
||
| 31 | from the key name (e.g. the '.' in |
||
| 32 | sect.key = value). |
||
| 33 | :return: A dictionary holding section names |
||
| 34 | as keys and the sections themselves |
||
| 35 | as value. |
||
| 36 | """ |
||
| 37 | # Note: arg_list can also be []. Hence we cannot use |
||
| 38 | # `arg_list = arg_list or default_list` |
||
| 39 | arg_list = sys.argv[1:] if arg_list is None else arg_list |
||
| 40 | arg_parser = arg_parser or default_arg_parser() |
||
| 41 | origin += os.path.sep |
||
| 42 | sections = OrderedDict(default=Section('Default')) |
||
| 43 | line_parser = LineParser(key_value_delimiters, |
||
| 44 | comment_seperators, |
||
| 45 | key_delimiters, |
||
| 46 | {}, |
||
| 47 | section_override_delimiters) |
||
| 48 | |||
| 49 | for arg_key, arg_value in sorted( |
||
| 50 | vars(arg_parser.parse_args(arg_list)).items()): |
||
| 51 | if arg_key == 'settings' and arg_value is not None: |
||
| 52 | parse_custom_settings(sections, |
||
| 53 | arg_value, |
||
| 54 | origin, |
||
| 55 | line_parser) |
||
| 56 | else: |
||
| 57 | if isinstance(arg_value, list): |
||
| 58 | arg_value = ",".join([str(val) for val in arg_value]) |
||
| 59 | |||
| 60 | append_to_sections(sections, |
||
| 61 | arg_key, |
||
| 62 | arg_value, |
||
| 63 | origin, |
||
| 64 | from_cli=True) |
||
| 65 | |||
| 66 | return sections |
||
| 67 | |||
| 93 |