Conditions | 2 |
Total Lines | 97 |
Lines | 0 |
Ratio | 0 % |
Tests | 45 |
CRAP Score | 2 |
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 | #!/usr/bin/env python3 |
||
16 | 1 | def main(args=None, function=None): |
|
17 | """Process command-line arguments and run the program.""" |
||
18 | |||
19 | # Shared options |
||
20 | 1 | debug = argparse.ArgumentParser(add_help=False) |
|
21 | 1 | debug.add_argument('-V', '--version', action='version', version=VERSION) |
|
22 | 1 | group = debug.add_mutually_exclusive_group() |
|
23 | 1 | group.add_argument('-v', '--verbose', action='count', default=0, |
|
24 | help="enable verbose logging") |
||
25 | 1 | group.add_argument('-q', '--quiet', action='store_const', const=-1, |
|
26 | dest='verbose', help="only display errors and prompts") |
||
27 | 1 | project = argparse.ArgumentParser(add_help=False) |
|
28 | 1 | project.add_argument('-r', '--root', metavar='PATH', |
|
29 | help="root directory of the project") |
||
30 | 1 | depth = argparse.ArgumentParser(add_help=False) |
|
31 | 1 | depth.add_argument('-d', '--depth', type=common.positive_int, |
|
32 | default=5, metavar="NUM", |
||
33 | help="limit the number of dependency levels") |
||
34 | 1 | options = argparse.ArgumentParser(add_help=False) |
|
35 | 1 | options.add_argument('-f', '--force', action='store_true', |
|
36 | help="overwrite uncommitted changes in dependencies") |
||
37 | 1 | options.add_argument('-c', '--clean', action='store_true', |
|
38 | help="keep ignored files in dependencies") |
||
39 | 1 | shared = {'formatter_class': common.WideHelpFormatter} |
|
40 | |||
41 | # Main parser |
||
42 | 1 | parser = argparse.ArgumentParser(prog=CLI, description=DESCRIPTION, |
|
|
|||
43 | parents=[debug, project], **shared) |
||
44 | |||
45 | 1 | subs = parser.add_subparsers(help="", dest='command', metavar="<command>") |
|
46 | |||
47 | # Install parser |
||
48 | 1 | info = "get the specified versions of all dependencies" |
|
49 | 1 | sub = subs.add_parser('install', description=info.capitalize() + '.', |
|
50 | help=info, parents=[debug, project, depth, options], |
||
51 | **shared) |
||
52 | 1 | sub.add_argument('name', nargs='*', |
|
53 | help="list of dependencies (`dir` values) to install") |
||
54 | 1 | sub.add_argument('-e', '--fetch', action='store_true', |
|
55 | help="always fetch the latest branches") |
||
56 | |||
57 | # Update parser |
||
58 | 1 | info = "update dependencies to the latest versions" |
|
59 | 1 | sub = subs.add_parser('update', description=info.capitalize() + '.', |
|
60 | help=info, parents=[debug, project, depth, options], |
||
61 | **shared) |
||
62 | 1 | sub.add_argument('name', nargs='*', |
|
63 | help="list of dependencies (`dir` values) to update") |
||
64 | 1 | sub.add_argument('-a', '--all', action='store_true', dest='recurse', |
|
65 | help="update all nested dependencies, recursively") |
||
66 | 1 | group = sub.add_mutually_exclusive_group() |
|
67 | 1 | group.add_argument('-l', '--lock', |
|
68 | action='store_true', dest='lock', default=None, |
||
69 | help="enable recording of versions for later reinstall") |
||
70 | 1 | group.add_argument('-L', '--no-lock', |
|
71 | action='store_false', dest='lock', default=None, |
||
72 | help="disable recording of versions for later reinstall") |
||
73 | |||
74 | # Display parser |
||
75 | 1 | info = "display the current version of each dependency" |
|
76 | 1 | sub = subs.add_parser('list', description=info.capitalize() + '.', |
|
77 | help=info, parents=[debug, project, depth], **shared) |
||
78 | 1 | sub.add_argument('-D', '--no-dirty', action='store_false', |
|
79 | dest='allow_dirty', |
||
80 | help="fail if a source has uncommitted changes") |
||
81 | |||
82 | # Lock parser |
||
83 | 1 | info = "lock the current version of each dependency" |
|
84 | 1 | sub = subs.add_parser('lock', description=info.capitalize() + '.', |
|
85 | help=info, parents=[debug, project], **shared) |
||
86 | 1 | sub.add_argument('name', nargs='*', |
|
87 | help="list of dependencies (`dir` values) to lock") |
||
88 | |||
89 | # Uninstall parser |
||
90 | 1 | info = "delete all installed dependencies" |
|
91 | 1 | sub = subs.add_parser('uninstall', description=info.capitalize() + '.', |
|
92 | help=info, parents=[debug, project], **shared) |
||
93 | 1 | sub.add_argument('-f', '--force', action='store_true', |
|
94 | help="delete uncommitted changes in dependencies") |
||
95 | |||
96 | # Edit parser |
||
97 | 1 | info = "open the configuration file in the default editor" |
|
98 | 1 | sub = subs.add_parser('edit', description=info.capitalize() + '.', |
|
99 | help=info, parents=[debug, project], **shared) |
||
100 | |||
101 | # Parse arguments |
||
102 | 1 | namespace = parser.parse_args(args=args) |
|
103 | |||
104 | # Configure logging |
||
105 | 1 | common.configure_logging(namespace.verbose) |
|
106 | |||
107 | # Run the program |
||
108 | 1 | function, args, kwargs, exit_msg = _get_command(function, namespace) |
|
109 | 1 | if function is None: |
|
110 | 1 | parser.print_help() |
|
111 | 1 | sys.exit(1) |
|
112 | 1 | _run_command(function, args, kwargs, exit_msg) |
|
113 | |||
170 |
Generally, there is nothing wrong with usage of
*
or**
arguments. For readability of the code base, we suggest to not over-use these language constructs though.For more information, we can recommend this blog post from Ned Batchelder including its comments which also touches this aspect.