Conditions | 6 |
Total Lines | 107 |
Code Lines | 80 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 | # SPDX-FileCopyrightText: 2019 Peter Bittner <[email protected]> |
||
17 | def parse_arguments(): |
||
18 | """ |
||
19 | Parse and handle CLI arguments. |
||
20 | """ |
||
21 | debris_default_topics = ['cache', 'coverage', 'package', 'pytest', 'ruff'] |
||
22 | debris_optional_topics = ['jupyter', 'mypy', 'tox'] |
||
23 | debris_choices = ['all', *debris_default_topics, *debris_optional_topics] |
||
24 | ignore_default_items = [ |
||
25 | '.git', |
||
26 | '.hg', |
||
27 | '.svn', |
||
28 | '.tox', |
||
29 | '.venv', |
||
30 | 'node_modules', |
||
31 | 'venv', |
||
32 | ] |
||
33 | |||
34 | parser = argparse.ArgumentParser( |
||
35 | description=( |
||
36 | 'Remove bytecode files, cache directories, build and test artifacts ' |
||
37 | 'and other debris in your Python project or elsewhere.' |
||
38 | ), |
||
39 | epilog='Made with ♥ by Painless Software, 🄯 Peter Bittner.', |
||
40 | ) |
||
41 | |||
42 | parser.add_argument('--version', action='version', version=__version__) |
||
43 | parser.add_argument( |
||
44 | 'directory', |
||
45 | nargs='+', |
||
46 | help='directory tree to traverse for bytecode and debris', |
||
47 | ) |
||
48 | parser.add_argument( |
||
49 | '-i', |
||
50 | '--ignore', |
||
51 | metavar='DIRECTORY', |
||
52 | action='extend', |
||
53 | nargs='+', |
||
54 | default=ignore_default_items, |
||
55 | help='directory that should be ignored (may be specified multiple times;' |
||
56 | ' default: %s)' % ' '.join(ignore_default_items), |
||
57 | ) |
||
58 | parser.add_argument( |
||
59 | '-d', |
||
60 | '--debris', |
||
61 | metavar='TOPIC', |
||
62 | action='extend', |
||
63 | nargs='*', |
||
64 | default=argparse.SUPPRESS, |
||
65 | choices=debris_choices, |
||
66 | help='remove leftovers from popular Python development tools' |
||
67 | ' (may be specified multiple times; optional: all %s; default: %s)' |
||
68 | % ( |
||
69 | ' '.join(debris_optional_topics), |
||
70 | ' '.join(debris_default_topics), |
||
71 | ), |
||
72 | ) |
||
73 | parser.add_argument( |
||
74 | '-e', |
||
75 | '--erase', |
||
76 | metavar='PATTERN', |
||
77 | action='extend', |
||
78 | nargs='+', |
||
79 | default=[], |
||
80 | help='delete files or folders matching a globbing pattern (may be specified' |
||
81 | ' multiple times); this will be interactive unless --yes is used.', |
||
82 | ) |
||
83 | parser.add_argument( |
||
84 | '-n', |
||
85 | '--dry-run', |
||
86 | action='store_true', |
||
87 | help='show what would be done', |
||
88 | ) |
||
89 | |||
90 | verbosity = parser.add_mutually_exclusive_group() |
||
91 | verbosity.add_argument('-q', '--quiet', action='store_true', help='be quiet') |
||
92 | verbosity.add_argument( |
||
93 | '-v', |
||
94 | '--verbose', |
||
95 | action='store_true', |
||
96 | help='be more verbose', |
||
97 | ) |
||
98 | |||
99 | parser.add_argument( |
||
100 | '-y', |
||
101 | '--yes', |
||
102 | action='store_true', |
||
103 | help='assume yes as answer for interactive questions', |
||
104 | ) |
||
105 | |||
106 | args = parser.parse_args() |
||
107 | init_logging(args) |
||
108 | |||
109 | if args.yes and not args.erase: |
||
110 | parser.error('Specifying --yes only makes sense with --erase.') |
||
111 | |||
112 | if 'debris' in args: |
||
113 | if 'all' in args.debris: |
||
114 | args.debris = debris_default_topics + debris_optional_topics |
||
115 | elif not args.debris: |
||
116 | args.debris = debris_default_topics |
||
117 | log.debug('Debris topics to scan for: %s', ' '.join(args.debris)) |
||
118 | else: |
||
119 | args.debris = [] |
||
120 | |||
121 | log.debug('Ignored directories: %s', ' '.join(args.ignore)) |
||
122 | |||
123 | return args |
||
124 | |||
147 |