| Conditions | 9 |
| Total Lines | 129 |
| Code Lines | 99 |
| 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 | """ |
||
| 13 | def parse_arguments(): |
||
| 14 | """ |
||
| 15 | Parse and handle CLI arguments |
||
| 16 | """ |
||
| 17 | debris_default_topics = ['cache', 'coverage', 'package', 'pytest', 'ruff'] |
||
| 18 | debris_optional_topics = ['jupyter', 'mypy', 'tox'] |
||
| 19 | debris_choices = ['all'] + debris_default_topics + debris_optional_topics |
||
| 20 | ignore_default_items = [ |
||
| 21 | '.git', |
||
| 22 | '.hg', |
||
| 23 | '.svn', |
||
| 24 | '.tox', |
||
| 25 | '.venv', |
||
| 26 | 'node_modules', |
||
| 27 | 'venv', |
||
| 28 | ] |
||
| 29 | |||
| 30 | parser = argparse.ArgumentParser( |
||
| 31 | description='Remove byte-compiled files for a package or project.', |
||
| 32 | ) |
||
| 33 | |||
| 34 | if sys.version_info < (3, 8): |
||
| 35 | parser.register('action', 'extend', compat.ExtendAction) |
||
| 36 | |||
| 37 | parser.add_argument('--version', action='version', version=__version__) |
||
| 38 | parser.add_argument( |
||
| 39 | '-V', |
||
| 40 | metavar='VERSION', |
||
| 41 | dest='version', |
||
| 42 | help='specify Python version to clean', |
||
| 43 | ) |
||
| 44 | parser.add_argument( |
||
| 45 | '-p', |
||
| 46 | '--package', |
||
| 47 | metavar='PACKAGE', |
||
| 48 | action='append', |
||
| 49 | default=[], |
||
| 50 | help='Debian package to byte-compile ' '(may be specified multiple times)', |
||
| 51 | ) |
||
| 52 | parser.add_argument( |
||
| 53 | 'directory', |
||
| 54 | nargs='*', |
||
| 55 | help='directory tree to traverse for byte-code', |
||
| 56 | ) |
||
| 57 | parser.add_argument( |
||
| 58 | '-i', |
||
| 59 | '--ignore', |
||
| 60 | metavar='DIRECTORY', |
||
| 61 | action='extend', |
||
| 62 | nargs='+', |
||
| 63 | default=ignore_default_items, |
||
| 64 | help='directory that should be ignored (may be specified multiple times;' |
||
| 65 | ' default: %s)' % ' '.join(ignore_default_items), |
||
| 66 | ) |
||
| 67 | parser.add_argument( |
||
| 68 | '-d', |
||
| 69 | '--debris', |
||
| 70 | metavar='TOPIC', |
||
| 71 | action='extend', |
||
| 72 | nargs='*', |
||
| 73 | default=argparse.SUPPRESS, |
||
| 74 | choices=debris_choices, |
||
| 75 | help='remove leftovers from popular Python development tools' |
||
| 76 | ' (may be specified multiple times; optional: all %s; default: %s)' |
||
| 77 | % ( |
||
| 78 | ' '.join(debris_optional_topics), |
||
| 79 | ' '.join(debris_default_topics), |
||
| 80 | ), |
||
| 81 | ) |
||
| 82 | parser.add_argument( |
||
| 83 | '-e', |
||
| 84 | '--erase', |
||
| 85 | metavar='PATTERN', |
||
| 86 | action='extend', |
||
| 87 | nargs='+', |
||
| 88 | default=[], |
||
| 89 | help='delete files or folders matching a globbing pattern (may be specified' |
||
| 90 | ' multiple times); this will be interactive unless --yes is used.', |
||
| 91 | ) |
||
| 92 | parser.add_argument( |
||
| 93 | '--legacy', |
||
| 94 | action='store_true', |
||
| 95 | help='use legacy Debian implementation (autodetect)', |
||
| 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.yes and not args.erase: |
||
| 124 | parser.error('Specifying --yes only makes sense with --erase.') |
||
| 125 | |||
| 126 | if not (args.package or args.directory): |
||
| 127 | msg = 'A directory (or files) or a list of packages must be specified.' |
||
| 128 | parser.error(msg) |
||
| 129 | |||
| 130 | if 'debris' in args: |
||
| 131 | if 'all' in args.debris: |
||
| 132 | args.debris = debris_default_topics + debris_optional_topics |
||
| 133 | elif not args.debris: |
||
| 134 | args.debris = debris_default_topics |
||
| 135 | log.debug('Debris topics to scan for: %s', ' '.join(args.debris)) |
||
| 136 | else: |
||
| 137 | args.debris = [] |
||
| 138 | |||
| 139 | log.debug('Ignored directories: %s', ' '.join(args.ignore)) |
||
| 140 | |||
| 141 | return args |
||
| 142 | |||
| 191 |