Completed
Pull Request — master (#1847)
by Tushar
01:38
created

coalib.parsing.default_arg_parser()   C

Complexity

Conditions 7

Size

Total Lines 182

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 7
dl 0
loc 182
rs 5.3042

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
import argparse
2
import sys
3
4
from coalib.misc import Constants
5
6
7
def default_arg_parser(formatter_class=None):
8
    """
9
    This function creates an ArgParser to parse command line arguments.
10
11
    :param formatter_class: Formatting the arg_parser output into a specific
12
                            form. For example: In the manpage format.
13
    """
14
    formatter_class = formatter_class or argparse.RawDescriptionHelpFormatter
15
16
    entry_point = sys.argv[0]
17
    for entry in ['coala-ci', 'coala-dbus', 'coala-format', 'coala-json',
18
                  'coala-delete-orig', 'coala-html']:
19
        if entry_point.endswith(entry):
20
            parser_type = entry
21
            break
22
    else:
23
        parser_type = 'coala'
24
25
    arg_parser = argparse.ArgumentParser(
26
        formatter_class=formatter_class,
27
        prog="coala",
28
        description="coala is a simple COde AnaLysis Application. Its goal "
29
                    "is to make static code analysis easy and convenient "
30
                    "for all languages. coala uses bears, which are analysis "
31
                    "routines that can be combined arbitrarily.")
32
33
    arg_parser.add_argument('TARGETS',
34
                            nargs='*',
35
                            help="Sections to be executed exclusively.")
36
    arg_parser.add_argument('-c',
37
                            '--config',
38
                            nargs=1,
39
                            metavar='FILE',
40
                            help='Configuration file to be used, defaults to '
41
                                 + repr(Constants.default_coafile))
42
    FIND_CONFIG_HELP = ('Attempt to find config file by checking parent '
43
                        'directories of the current working directory. It is '
44
                        'assumed that the config file is named '
45
                        + repr(Constants.default_coafile) + '. This arg is '
46
                        'ignored if --config is also given')
47
    arg_parser.add_argument('-F',
48
                            '--find-config',
49
                            nargs='?',
50
                            const=True,
51
                            metavar='BOOL',
52
                            help=FIND_CONFIG_HELP)
53
    arg_parser.add_argument('-f',
54
                            '--files',
55
                            nargs='+',
56
                            metavar='FILE',
57
                            help='Files that should be checked')
58
    arg_parser.add_argument('-i',
59
                            '--ignore',
60
                            nargs='+',
61
                            metavar='FILE',
62
                            help='Files that should be ignored')
63
    arg_parser.add_argument('--limit-files',
64
                            nargs='+',
65
                            metavar='FILE',
66
                            help='Files that will be analyzed will be '
67
                                 'restricted to those in the globs listed '
68
                                 'in this argument as well the files setting')
69
    arg_parser.add_argument('-b',
70
                            '--bears',
71
                            nargs='+',
72
                            metavar='NAME',
73
                            help='Names of bears to use')
74
    BEAR_DIRS_HELP = 'Additional directories where bears may lie'
75
    arg_parser.add_argument('-d',
76
                            '--bear-dirs',
77
                            nargs='+',
78
                            metavar='DIR',
79
                            help=BEAR_DIRS_HELP)
80
    LOG_LEVEL_HELP = ("Enum('ERROR','INFO','WARNING','DEBUG') to set level of "
81
                      "log output")
82
    arg_parser.add_argument('-L',
83
                            '--log-level',
84
                            nargs=1,
85
                            choices=['ERROR', 'INFO', 'WARNING', 'DEBUG'],
86
                            metavar='ENUM',
87
                            help=LOG_LEVEL_HELP)
88
    MIN_SEVERITY_HELP = ("Enum('INFO', 'NORMAL', 'MAJOR') to set the minimal "
89
                         "result severity.")
90
    arg_parser.add_argument('-m',
91
                            '--min-severity',
92
                            nargs=1,
93
                            choices=('INFO', 'NORMAL', 'MAJOR'),
94
                            metavar='ENUM',
95
                            help=MIN_SEVERITY_HELP)
96
    SETTINGS_HELP = 'Arbitrary settings in the form of section.key=value'
97
    arg_parser.add_argument('-S',
98
                            '--settings',
99
                            nargs='+',
100
                            metavar='SETTING',
101
                            help=SETTINGS_HELP)
102
    if parser_type == 'coala-json':
103
        arg_parser.add_argument('--text-logs',
104
                                nargs='?',
105
                                const=True,
106
                                metavar='BOOL',
107
                                help='Don\'t display logs as json, display '
108
                                     'them as we normally do in the console.')
109
        arg_parser.add_argument('-o',
110
                                '--output',
111
                                nargs='?',
112
                                const=True,
113
                                metavar='BOOL',
114
                                help='Write the logs as json to a file '
115
                                'where filename is specified as argument.')
116
    if parser_type == 'coala-html':
117
        arg_parser.add_argument('-o',
118
                                '--output',
119
                                nargs='?',
120
                                const=True,
121
                                metavar='BOOL',
122
                                default=Constants.COALA_HTML_DUMP,
123
                                help='Dump the results as json to a file '
124
                                'where filename is specified as argument.')
125
    if parser_type == 'coala':
126
        SHOW_BEARS_HELP = ("Display bears and its metadata with the sections "
127
                           "that they belong to")
128
        arg_parser.add_argument('-B',
129
                                '--show-bears',
130
                                nargs='?',
131
                                const=True,
132
                                metavar='BOOL',
133
                                help=SHOW_BEARS_HELP)
134
        arg_parser.add_argument('-A',
135
                                '--show-all-bears',
136
                                nargs='?',
137
                                const=True,
138
                                metavar='BOOL',
139
                                help="Display all bears.")
140
    SAVE_HELP = ('Filename of file to be saved to, if provided with no '
141
                 'arguments, settings will be stored back to the file given '
142
                 'by -c')
143
    arg_parser.add_argument('-s',
144
                            '--save',
145
                            nargs='?',
146
                            const=True,
147
                            metavar='FILE',
148
                            help=SAVE_HELP)
149
    TAG_HELP = ('Tag results with a specific name. You can access the results'
150
                ' later with that tag.')
151
    arg_parser.add_argument('-t',
152
                            '--tag',
153
                            nargs='?',
154
                            const=True,
155
                            metavar='STRING',
156
                            help=TAG_HELP)
157
158
    DELETE_TAG_HELP = 'Delete pre-tagged results with tag name.'
159
    arg_parser.add_argument('-g',
160
                            '--dtag',
161
                            nargs='?',
162
                            const=True,
163
                            metavar='STRING',
164
                            help=DELETE_TAG_HELP)
165
166
    arg_parser.add_argument("-j",
167
                            "--jobs",
168
                            type=int,
169
                            help="Number of jobs to use in parallel.")
170
171
    arg_parser.add_argument('-v',
172
                            '--version',
173
                            action='version',
174
                            version=Constants.VERSION)
175
176
    arg_parser.add_argument('-n',
177
                            '--no-orig',
178
                            nargs='?',
179
                            const=True,
180
                            help="Deactivate creation of .orig files,"
181
                                 ".orig backup files before applying patches")
182
    arg_parser.add_argument('-r',
183
                            '--relpath',
184
                            nargs='?',
185
                            const=True,
186
                            help="return relative paths for files")
187
188
    return arg_parser
189