Passed
Push — main ( 002c08...62641e )
by Peter
01:22
created

pyclean.cli.parse_arguments()   C

Complexity

Conditions 9

Size

Total Lines 76
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 54
nop 0
dl 0
loc 76
rs 6.1721
c 0
b 0
f 0

How to fix   Long Method   

Long Method

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:

1
"""
2
Command line interface implementation for pyclean.
3
"""
4
import argparse
5
import logging
6
import sys
7
8
from . import __version__, compat, modern
9
10
log = logging.getLogger(__name__)
11
12
13
def parse_arguments():
14
    """
15
    Parse and handle CLI arguments
16
    """
17
    debris_default_topics = ['build', 'cache', 'coverage', 'pytest']
18
    debris_optional_topics = ['jupyter', 'tox']
19
    debris_choices = ['all'] + debris_default_topics + debris_optional_topics
20
    ignore_default_items = ['.git', '.hg', '.svn', '.tox', '.venv', 'node_modules']
21
22
    parser = argparse.ArgumentParser(
23
        description='Remove byte-compiled files for a package or project.',
24
    )
25
26
    if sys.version_info < (3, 8):
27
        parser.register('action', 'extend', compat.ExtendAction)
28
29
    parser.add_argument('--version', action='version', version=__version__)
30
    parser.add_argument('-V', metavar='VERSION', dest='version',
31
                        help='specify Python version to clean')
32
    parser.add_argument('-p', '--package', metavar='PACKAGE',
33
                        action='append', default=[],
34
                        help='Debian package to byte-compile '
35
                             '(may be specified multiple times)')
36
    parser.add_argument('directory', nargs='*',
37
                        help='directory tree to traverse for byte-code')
38
    parser.add_argument('-i', '--ignore', metavar='DIRECTORY', action='extend',
39
                        nargs='+', default=ignore_default_items,
40
                        help='directory that should be ignored '
41
                             '(may be specified multiple times; '
42
                             'default: %s)' % ' '.join(ignore_default_items))
43
    parser.add_argument('-d', '--debris', metavar='TOPIC', action='extend',
44
                        nargs='*', default=argparse.SUPPRESS, choices=debris_choices,
45
                        help='remove leftovers from popular Python development '
46
                             'tools (may be specified multiple times; '
47
                             'default: %s)' % ' '.join(debris_default_topics))
48
    parser.add_argument('-e', '--erase', metavar='PATTERN', action='extend',
49
                        nargs='+', default=[],
50
                        help='delete files or folders matching a globbing '
51
                             'pattern (may be specified multiple times); '
52
                             'this will be interactive unless --yes is used.')
53
    parser.add_argument('--legacy', action='store_true',
54
                        help='use legacy Debian implementation (autodetect)')
55
    parser.add_argument('-n', '--dry-run', action='store_true',
56
                        help='show what would be done')
57
58
    verbosity = parser.add_mutually_exclusive_group()
59
    verbosity.add_argument('-q', '--quiet', action='store_true',
60
                           help='be quiet')
61
    verbosity.add_argument('-v', '--verbose', action='store_true',
62
                           help='be more verbose')
63
64
    parser.add_argument('-y', '--yes', action='store_true',
65
                        help='assume yes as answer for interactive questions')
66
67
    args = parser.parse_args()
68
    init_logging(args)
69
70
    if args.yes and not args.erase:
71
        parser.error('Specifying --yes only makes sense with --erase.')
72
73
    if not (args.package or args.directory):
74
        parser.error('A directory (or files) or a list of packages '
75
                     'must be specified.')
76
77
    if 'debris' in args:
78
        if 'all' in args.debris:
79
            args.debris = debris_default_topics + debris_optional_topics
80
        elif not args.debris:
81
            args.debris = debris_default_topics
82
        log.debug("Debris topics to scan for: %s", ' '.join(args.debris))
83
    else:
84
        args.debris = []
85
86
    log.debug("Ignored directories: %s", ' '.join(args.ignore))
87
88
    return args
89
90
91
def init_logging(args):
92
    """
93
    Set the log level according to the -v/-q command line options.
94
    """
95
    log_level = logging.FATAL if args.quiet \
96
        else logging.DEBUG if args.verbose \
97
        else logging.INFO
98
    log_format = "%(message)s"
99
    logging.basicConfig(level=log_level, format=log_format)
100
101
102
def main(override=None):
103
    """
104
    Entry point for all scripts
105
    """
106
    args = parse_arguments()
107
    if override or args.legacy:
108
        impl = compat.get_implementation(override=override)
109
        impl.main(args)
110
    else:
111
        modern.pyclean(args)
112
113
114
def py2clean():
115
    """
116
    Forces the use of the implementation for Python 2
117
    """
118
    main('CPython2')
119
120
121
def py3clean():
122
    """
123
    Forces the use of the implementation for Python 3
124
    """
125
    main('CPython3')
126
127
128
def pypyclean():
129
    """
130
    Forces the use of the implementation for PyPy (2+3)
131
    """
132
    main('PyPy2')
133