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

coalib.parsing.default_arg_parser()   C

Complexity

Conditions 7

Size

Total Lines 199

Duplication

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