options.py 11 KB

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