Passed
Pull Request — main (#50)
by Peter
01:07
created

pyclean.cli.parse_arguments()   B

Complexity

Conditions 6

Size

Total Lines 62
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

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