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