Passed
Pull Request — main (#50)
by Peter
04:30
created

pyclean.cli.parse_arguments()   B

Complexity

Conditions 5

Size

Total Lines 59
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 41
nop 0
dl 0
loc 59
rs 8.4293
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
7
from . import __version__, compat, modern
8
9
log = logging.getLogger(__name__)
10
11
12
def parse_arguments():
13
    """
14
    Parse and handle CLI arguments
15
    """
16
    debris_default_topics = ['build', 'cache', 'coverage', 'pytest']
17
    debris_optional_topics = ['tox']
18
19
    parser = argparse.ArgumentParser(
20
        description='Remove byte-compiled files for a package or project.',
21
    )
22
23
    parser.add_argument('--version', action='version', version=__version__)
24
    parser.add_argument('-V', metavar='VERSION', dest='version',
25
                        help='specify Python version to clean')
26
    parser.add_argument('-p', '--package', metavar='PACKAGE',
27
                        action='append', default=[],
28
                        help='Debian package to byte-compile '
29
                             '(may be specified multiple times)')
30
    parser.add_argument('directory', nargs='*',
31
                        help='directory tree to traverse for byte-code')
32
    parser.add_argument('-i', '--ignore', metavar='DIRECTORY', action='extend',
33
                        nargs='+', default=['.git', '.tox', '.venv'],
34
                        help='directory that should be ignored '
35
                             '(may be specified multiple times; '
36
                             'default: %(default)s)')
37
    parser.add_argument('-d', '--debris', metavar='TOPIC', action='extend',
38
                        nargs='*', default=argparse.SUPPRESS,
39
                        choices=debris_default_topics + debris_optional_topics,
40
                        help='remove typical leftovers from well-known '
41
                             'programs (may be specified multiple times; '
42
                             'default: %s)' % debris_default_topics)
43
    parser.add_argument('--legacy', action='store_true',
44
                        help='use legacy Debian implementation (autodetect)')
45
    parser.add_argument('-n', '--dry-run', action='store_true',
46
                        help='show what would be done')
47
48
    verbosity = parser.add_mutually_exclusive_group()
49
    verbosity.add_argument('-q', '--quiet', action='store_true',
50
                           help='be quiet')
51
    verbosity.add_argument('-v', '--verbose', action='store_true',
52
                           help='be more verbose')
53
54
    args = parser.parse_args()
55
    init_logging(args)
56
57
    if not (args.package or args.directory):
58
        parser.error('A directory (or files) or a list of packages '
59
                     'must be specified.')
60
61
    if 'debris' in args:
62
        if args.debris == []:
63
            args.debris = debris_default_topics
64
        log.debug("Debris requested to clean up for: %s", ' '.join(args.debris))
65
    else:
66
        args.debris = []
67
68
    log.debug("Ignored directories: %s", ' '.join(args.ignore))
69
70
    return args
71
72
73
def init_logging(args):
74
    """
75
    Set the log level according to the -v/-q command line options.
76
    """
77
    log_level = logging.FATAL if args.quiet \
78
        else logging.DEBUG if args.verbose \
79
        else logging.INFO
80
    log_format = "%(message)s"
81
    logging.basicConfig(level=log_level, format=log_format)
82
83
84
def main(override=None):
85
    """
86
    Entry point for all scripts
87
    """
88
    args = parse_arguments()
89
    if override or args.legacy:
90
        impl = compat.get_implementation(override=override)
91
        impl.main(args)
92
    else:
93
        modern.pyclean(args)
94
95
96
def py2clean():
97
    """
98
    Forces the use of the implementation for Python 2
99
    """
100
    main('CPython2')
101
102
103
def py3clean():
104
    """
105
    Forces the use of the implementation for Python 3
106
    """
107
    main('CPython3')
108
109
110
def pypyclean():
111
    """
112
    Forces the use of the implementation for PyPy (2+3)
113
    """
114
    main('PyPy2')
115