| Conditions | 7 |
| Total Lines | 193 |
| Lines | 0 |
| Ratio | 0 % |
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:
If many parameters/temporary variables are present:
| 1 | import argparse |
||
| 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('-I', |
||
| 54 | '--no-config', |
||
| 55 | nargs='?', |
||
| 56 | const=True, |
||
| 57 | metavar='BOOL', |
||
| 58 | help="Run without using any config file") |
||
| 59 | arg_parser.add_argument('-f', |
||
| 60 | '--files', |
||
| 61 | nargs='+', |
||
| 62 | metavar='FILE', |
||
| 63 | help='Files that should be checked') |
||
| 64 | arg_parser.add_argument('-i', |
||
| 65 | '--ignore', |
||
| 66 | nargs='+', |
||
| 67 | metavar='FILE', |
||
| 68 | help='Files that should be ignored') |
||
| 69 | arg_parser.add_argument('--limit-files', |
||
| 70 | nargs='+', |
||
| 71 | metavar='FILE', |
||
| 72 | help='Files that will be analyzed will be ' |
||
| 73 | 'restricted to those in the globs listed ' |
||
| 74 | 'in this argument as well the files setting') |
||
| 75 | arg_parser.add_argument('-b', |
||
| 76 | '--bears', |
||
| 77 | nargs='+', |
||
| 78 | metavar='NAME', |
||
| 79 | help='Names of bears to use') |
||
| 80 | BEAR_DIRS_HELP = 'Additional directories where bears may lie' |
||
| 81 | arg_parser.add_argument('-d', |
||
| 82 | '--bear-dirs', |
||
| 83 | nargs='+', |
||
| 84 | metavar='DIR', |
||
| 85 | help=BEAR_DIRS_HELP) |
||
| 86 | LOG_LEVEL_HELP = ("Enum('ERROR','INFO','WARNING','DEBUG') to set level of " |
||
| 87 | "log output") |
||
| 88 | arg_parser.add_argument('-L', |
||
| 89 | '--log-level', |
||
| 90 | nargs=1, |
||
| 91 | choices=['ERROR', 'INFO', 'WARNING', 'DEBUG'], |
||
| 92 | metavar='ENUM', |
||
| 93 | help=LOG_LEVEL_HELP) |
||
| 94 | MIN_SEVERITY_HELP = ("Enum('INFO', 'NORMAL', 'MAJOR') to set the minimal " |
||
| 95 | "result severity.") |
||
| 96 | arg_parser.add_argument('-m', |
||
| 97 | '--min-severity', |
||
| 98 | nargs=1, |
||
| 99 | choices=('INFO', 'NORMAL', 'MAJOR'), |
||
| 100 | metavar='ENUM', |
||
| 101 | help=MIN_SEVERITY_HELP) |
||
| 102 | SETTINGS_HELP = 'Arbitrary settings in the form of section.key=value' |
||
| 103 | arg_parser.add_argument('-S', |
||
| 104 | '--settings', |
||
| 105 | nargs='+', |
||
| 106 | metavar='SETTING', |
||
| 107 | help=SETTINGS_HELP) |
||
| 108 | if parser_type == 'coala-json': |
||
| 109 | arg_parser.add_argument('--text-logs', |
||
| 110 | nargs='?', |
||
| 111 | const=True, |
||
| 112 | metavar='BOOL', |
||
| 113 | help='Don\'t display logs as json, display ' |
||
| 114 | 'them as we normally do in the console.') |
||
| 115 | arg_parser.add_argument('-o', |
||
| 116 | '--output', |
||
| 117 | nargs='?', |
||
| 118 | const=True, |
||
| 119 | metavar='BOOL', |
||
| 120 | help='Write the logs as json to a file ' |
||
| 121 | 'where filename is specified as argument.') |
||
| 122 | if parser_type == 'coala-html': |
||
| 123 | arg_parser.add_argument('-N', |
||
| 124 | '--noupdate', |
||
| 125 | nargs='?', |
||
| 126 | const=True, |
||
| 127 | metavar='BOOL', |
||
| 128 | default=False, |
||
| 129 | help='Launch webpage from existing results.') |
||
| 130 | arg_parser.add_argument('--nolaunch', |
||
| 131 | nargs='?', |
||
| 132 | const=True, |
||
| 133 | metavar='BOOL', |
||
| 134 | default=False, |
||
| 135 | help='Launch webpage') |
||
| 136 | if parser_type == 'coala': |
||
| 137 | SHOW_BEARS_HELP = ("Display bears and its metadata with the sections " |
||
| 138 | "that they belong to") |
||
| 139 | arg_parser.add_argument('-B', |
||
| 140 | '--show-bears', |
||
| 141 | nargs='?', |
||
| 142 | const=True, |
||
| 143 | metavar='BOOL', |
||
| 144 | help=SHOW_BEARS_HELP) |
||
| 145 | arg_parser.add_argument('-A', |
||
| 146 | '--show-all-bears', |
||
| 147 | nargs='?', |
||
| 148 | const=True, |
||
| 149 | metavar='BOOL', |
||
| 150 | help="Display all bears.") |
||
| 151 | SAVE_HELP = ('Filename of file to be saved to, if provided with no ' |
||
| 152 | 'arguments, settings will be stored back to the file given ' |
||
| 153 | 'by -c') |
||
| 154 | arg_parser.add_argument('-s', |
||
| 155 | '--save', |
||
| 156 | nargs='?', |
||
| 157 | const=True, |
||
| 158 | metavar='FILE', |
||
| 159 | help=SAVE_HELP) |
||
| 160 | TAG_HELP = ('Tag results with a specific name. You can access the results' |
||
| 161 | ' later with that tag.') |
||
| 162 | arg_parser.add_argument('-t', |
||
| 163 | '--tag', |
||
| 164 | nargs='?', |
||
| 165 | const=True, |
||
| 166 | metavar='STRING', |
||
| 167 | help=TAG_HELP) |
||
| 168 | |||
| 169 | DELETE_TAG_HELP = 'Delete pre-tagged results with tag name.' |
||
| 170 | arg_parser.add_argument('-g', |
||
| 171 | '--dtag', |
||
| 172 | nargs='?', |
||
| 173 | const=True, |
||
| 174 | metavar='STRING', |
||
| 175 | help=DELETE_TAG_HELP) |
||
| 176 | |||
| 177 | arg_parser.add_argument("-j", |
||
| 178 | "--jobs", |
||
| 179 | type=int, |
||
| 180 | help="Number of jobs to use in parallel.") |
||
| 181 | |||
| 182 | arg_parser.add_argument('-v', |
||
| 183 | '--version', |
||
| 184 | action='version', |
||
| 185 | version=Constants.VERSION) |
||
| 186 | |||
| 187 | arg_parser.add_argument('-n', |
||
| 188 | '--no-orig', |
||
| 189 | nargs='?', |
||
| 190 | const=True, |
||
| 191 | help="Deactivate creation of .orig files," |
||
| 192 | ".orig backup files before applying patches") |
||
| 193 | arg_parser.add_argument('-r', |
||
| 194 | '--relpath', |
||
| 195 | nargs='?', |
||
| 196 | const=True, |
||
| 197 | help="return relative paths for files") |
||
| 198 | |||
| 199 | return arg_parser |
||
| 200 |