options.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. """Contains the logic for all of the default options for Flake8."""
  2. import argparse
  3. from flake8 import defaults
  4. from flake8.options.manager import OptionManager
  5. def stage1_arg_parser() -> argparse.ArgumentParser:
  6. """Register the preliminary options on our OptionManager.
  7. The preliminary options include:
  8. - ``-v``/``--verbose``
  9. - ``--output-file``
  10. - ``--append-config``
  11. - ``--config``
  12. - ``--isolated``
  13. - ``--enable-extensions``
  14. """
  15. parser = argparse.ArgumentParser(add_help=False)
  16. parser.add_argument(
  17. "-v",
  18. "--verbose",
  19. default=0,
  20. action="count",
  21. help="Print more information about what is happening in flake8. "
  22. "This option is repeatable and will increase verbosity each "
  23. "time it is repeated.",
  24. )
  25. parser.add_argument(
  26. "--output-file", default=None, help="Redirect report to a file."
  27. )
  28. # Config file options
  29. parser.add_argument(
  30. "--append-config",
  31. action="append",
  32. default=[],
  33. help="Provide extra config files to parse in addition to the files "
  34. "found by Flake8 by default. These files are the last ones read "
  35. "and so they take the highest precedence when multiple files "
  36. "provide the same option.",
  37. )
  38. parser.add_argument(
  39. "--config",
  40. default=None,
  41. help="Path to the config file that will be the authoritative config "
  42. "source. This will cause Flake8 to ignore all other "
  43. "configuration files.",
  44. )
  45. parser.add_argument(
  46. "--isolated",
  47. default=False,
  48. action="store_true",
  49. help="Ignore all configuration files.",
  50. )
  51. # Plugin enablement options
  52. parser.add_argument(
  53. "--enable-extensions",
  54. help="Enable plugins and extensions that are otherwise disabled "
  55. "by default",
  56. )
  57. parser.add_argument(
  58. "--require-plugins",
  59. help="Require specific plugins to be installed before running",
  60. )
  61. return parser
  62. class JobsArgument:
  63. """Type callback for the --jobs argument."""
  64. def __init__(self, arg: str) -> None:
  65. """Parse and validate the --jobs argument.
  66. :param arg: The argument passed by argparse for validation
  67. """
  68. self.is_auto = False
  69. self.n_jobs = -1
  70. if arg == "auto":
  71. self.is_auto = True
  72. elif arg.isdigit():
  73. self.n_jobs = int(arg)
  74. else:
  75. raise argparse.ArgumentTypeError(
  76. f"{arg!r} must be 'auto' or an integer.",
  77. )
  78. def __repr__(self) -> str:
  79. """Representation for debugging."""
  80. return f"{type(self).__name__}({str(self)!r})"
  81. def __str__(self) -> str:
  82. """Format our JobsArgument class."""
  83. return "auto" if self.is_auto else str(self.n_jobs)
  84. def register_default_options(option_manager: OptionManager) -> None:
  85. """Register the default options on our OptionManager.
  86. The default options include:
  87. - ``-q``/``--quiet``
  88. - ``--color``
  89. - ``--count``
  90. - ``--diff``
  91. - ``--exclude``
  92. - ``--extend-exclude``
  93. - ``--filename``
  94. - ``--format``
  95. - ``--hang-closing``
  96. - ``--ignore``
  97. - ``--extend-ignore``
  98. - ``--per-file-ignores``
  99. - ``--max-line-length``
  100. - ``--max-doc-length``
  101. - ``--indent-size``
  102. - ``--select``
  103. - ``--extend-select``
  104. - ``--disable-noqa``
  105. - ``--show-source``
  106. - ``--statistics``
  107. - ``--exit-zero``
  108. - ``-j``/``--jobs``
  109. - ``--tee``
  110. - ``--benchmark``
  111. - ``--bug-report``
  112. """
  113. add_option = option_manager.add_option
  114. add_option(
  115. "-q",
  116. "--quiet",
  117. default=0,
  118. action="count",
  119. parse_from_config=True,
  120. help="Report only file names, or nothing. This option is repeatable.",
  121. )
  122. add_option(
  123. "--color",
  124. choices=("auto", "always", "never"),
  125. default="auto",
  126. help="Whether to use color in output. Defaults to `%(default)s`.",
  127. )
  128. add_option(
  129. "--count",
  130. action="store_true",
  131. parse_from_config=True,
  132. help="Print total number of errors to standard output and "
  133. "set the exit code to 1 if total is not empty.",
  134. )
  135. add_option(
  136. "--diff",
  137. action="store_true",
  138. help="(DEPRECATED) Report changes only within line number ranges in "
  139. "the unified diff provided on standard in by the user.",
  140. )
  141. add_option(
  142. "--exclude",
  143. metavar="patterns",
  144. default=",".join(defaults.EXCLUDE),
  145. comma_separated_list=True,
  146. parse_from_config=True,
  147. normalize_paths=True,
  148. help="Comma-separated list of files or directories to exclude. "
  149. "(Default: %(default)s)",
  150. )
  151. add_option(
  152. "--extend-exclude",
  153. metavar="patterns",
  154. default="",
  155. parse_from_config=True,
  156. comma_separated_list=True,
  157. normalize_paths=True,
  158. help="Comma-separated list of files or directories to add to the list "
  159. "of excluded ones.",
  160. )
  161. add_option(
  162. "--filename",
  163. metavar="patterns",
  164. default="*.py",
  165. parse_from_config=True,
  166. comma_separated_list=True,
  167. help="Only check for filenames matching the patterns in this comma-"
  168. "separated list. (Default: %(default)s)",
  169. )
  170. add_option(
  171. "--stdin-display-name",
  172. default="stdin",
  173. help="The name used when reporting errors from code passed via stdin. "
  174. "This is useful for editors piping the file contents to flake8. "
  175. "(Default: %(default)s)",
  176. )
  177. # TODO(sigmavirus24): Figure out --first/--repeat
  178. # NOTE(sigmavirus24): We can't use choices for this option since users can
  179. # freely provide a format string and that will break if we restrict their
  180. # choices.
  181. add_option(
  182. "--format",
  183. metavar="format",
  184. default="default",
  185. parse_from_config=True,
  186. help="Format errors according to the chosen formatter.",
  187. )
  188. add_option(
  189. "--hang-closing",
  190. action="store_true",
  191. parse_from_config=True,
  192. help="Hang closing bracket instead of matching indentation of opening "
  193. "bracket's line.",
  194. )
  195. add_option(
  196. "--ignore",
  197. metavar="errors",
  198. parse_from_config=True,
  199. comma_separated_list=True,
  200. help=(
  201. f"Comma-separated list of error codes to ignore (or skip). "
  202. f"For example, ``--ignore=E4,E51,W234``. "
  203. f"(Default: {','.join(defaults.IGNORE)})"
  204. ),
  205. )
  206. add_option(
  207. "--extend-ignore",
  208. metavar="errors",
  209. parse_from_config=True,
  210. comma_separated_list=True,
  211. help="Comma-separated list of error codes to add to the list of "
  212. "ignored ones. For example, ``--extend-ignore=E4,E51,W234``.",
  213. )
  214. add_option(
  215. "--per-file-ignores",
  216. default="",
  217. parse_from_config=True,
  218. help="A pairing of filenames and violation codes that defines which "
  219. "violations to ignore in a particular file. The filenames can be "
  220. "specified in a manner similar to the ``--exclude`` option and the "
  221. "violations work similarly to the ``--ignore`` and ``--select`` "
  222. "options.",
  223. )
  224. add_option(
  225. "--max-line-length",
  226. type=int,
  227. metavar="n",
  228. default=defaults.MAX_LINE_LENGTH,
  229. parse_from_config=True,
  230. help="Maximum allowed line length for the entirety of this run. "
  231. "(Default: %(default)s)",
  232. )
  233. add_option(
  234. "--max-doc-length",
  235. type=int,
  236. metavar="n",
  237. default=None,
  238. parse_from_config=True,
  239. help="Maximum allowed doc line length for the entirety of this run. "
  240. "(Default: %(default)s)",
  241. )
  242. add_option(
  243. "--indent-size",
  244. type=int,
  245. metavar="n",
  246. default=defaults.INDENT_SIZE,
  247. parse_from_config=True,
  248. help="Number of spaces used for indentation (Default: %(default)s)",
  249. )
  250. add_option(
  251. "--select",
  252. metavar="errors",
  253. parse_from_config=True,
  254. comma_separated_list=True,
  255. help=(
  256. f"Comma-separated list of error codes to enable. "
  257. f"For example, ``--select=E4,E51,W234``. "
  258. f"(Default: {','.join(defaults.SELECT)})"
  259. ),
  260. )
  261. add_option(
  262. "--extend-select",
  263. metavar="errors",
  264. parse_from_config=True,
  265. comma_separated_list=True,
  266. help=(
  267. "Comma-separated list of error codes to add to the list "
  268. "of selected ones. For example, ``--extend-select=E4,E51,W234``."
  269. ),
  270. )
  271. add_option(
  272. "--disable-noqa",
  273. default=False,
  274. parse_from_config=True,
  275. action="store_true",
  276. help='Disable the effect of "# noqa". This will report errors on '
  277. 'lines with "# noqa" at the end.',
  278. )
  279. # TODO(sigmavirus24): Decide what to do about --show-pep8
  280. add_option(
  281. "--show-source",
  282. action="store_true",
  283. parse_from_config=True,
  284. help="Show the source generate each error or warning.",
  285. )
  286. add_option(
  287. "--no-show-source",
  288. action="store_false",
  289. dest="show_source",
  290. parse_from_config=False,
  291. help="Negate --show-source",
  292. )
  293. add_option(
  294. "--statistics",
  295. action="store_true",
  296. parse_from_config=True,
  297. help="Count errors.",
  298. )
  299. # Flake8 options
  300. add_option(
  301. "--exit-zero",
  302. action="store_true",
  303. help='Exit with status code "0" even if there are errors.',
  304. )
  305. add_option(
  306. "-j",
  307. "--jobs",
  308. default="auto",
  309. parse_from_config=True,
  310. type=JobsArgument,
  311. help="Number of subprocesses to use to run checks in parallel. "
  312. 'This is ignored on Windows. The default, "auto", will '
  313. "auto-detect the number of processors available to use. "
  314. "(Default: %(default)s)",
  315. )
  316. add_option(
  317. "--tee",
  318. default=False,
  319. parse_from_config=True,
  320. action="store_true",
  321. help="Write to stdout and output-file.",
  322. )
  323. # Benchmarking
  324. add_option(
  325. "--benchmark",
  326. default=False,
  327. action="store_true",
  328. help="Print benchmark information about this run of Flake8",
  329. )
  330. # Debugging
  331. add_option(
  332. "--bug-report",
  333. action="store_true",
  334. help="Print information necessary when preparing a bug report",
  335. )