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

pyclean.cli.parse_arguments()   B

Complexity

Conditions 6

Size

Total Lines 63
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

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