option_parser.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  2. # For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
  3. import optparse # pylint: disable=deprecated-module
  4. from pylint.config.option import Option
  5. def _level_options(group, outputlevel):
  6. return [
  7. option
  8. for option in group.option_list
  9. if (getattr(option, "level", 0) or 0) <= outputlevel
  10. and option.help is not optparse.SUPPRESS_HELP
  11. ]
  12. class OptionParser(optparse.OptionParser):
  13. def __init__(self, option_class, *args, **kwargs):
  14. super().__init__(option_class=Option, *args, **kwargs)
  15. def format_option_help(self, formatter=None):
  16. if formatter is None:
  17. formatter = self.formatter
  18. outputlevel = getattr(formatter, "output_level", 0)
  19. formatter.store_option_strings(self)
  20. result = [formatter.format_heading("Options")]
  21. formatter.indent()
  22. if self.option_list:
  23. result.append(optparse.OptionContainer.format_option_help(self, formatter))
  24. result.append("\n")
  25. for group in self.option_groups:
  26. if group.level <= outputlevel and (
  27. group.description or _level_options(group, outputlevel)
  28. ):
  29. result.append(group.format_help(formatter))
  30. result.append("\n")
  31. formatter.dedent()
  32. # Drop the last "\n", or the header if no options or option groups:
  33. return "".join(result[:-1])
  34. def _match_long_opt(self, opt):
  35. """Disable abbreviations."""
  36. if opt not in self._long_opt:
  37. raise optparse.BadOptionError(opt)
  38. return opt