Prints all the command line options to stderr (or another file).
(self, file: Optional[TextIO] = None)
| 438 | self.run_parse_callbacks() |
| 439 | |
| 440 | def print_help(self, file: Optional[TextIO] = None) -> None: |
| 441 | """Prints all the command line options to stderr (or another file).""" |
| 442 | if file is None: |
| 443 | file = sys.stderr |
| 444 | print("Usage: %s [OPTIONS]" % sys.argv[0], file=file) |
| 445 | print("\nOptions:\n", file=file) |
| 446 | by_group = {} # type: Dict[str, List[_Option]] |
| 447 | for option in self._options.values(): |
| 448 | by_group.setdefault(option.group_name, []).append(option) |
| 449 | |
| 450 | for filename, o in sorted(by_group.items()): |
| 451 | if filename: |
| 452 | print("\n%s options:\n" % os.path.normpath(filename), file=file) |
| 453 | o.sort(key=lambda option: option.name) |
| 454 | for option in o: |
| 455 | # Always print names with dashes in a CLI context. |
| 456 | prefix = self._normalize_name(option.name) |
| 457 | if option.metavar: |
| 458 | prefix += "=" + option.metavar |
| 459 | description = option.help or "" |
| 460 | if option.default is not None and option.default != "": |
| 461 | description += " (default %s)" % option.default |
| 462 | lines = textwrap.wrap(description, 79 - 35) |
| 463 | if len(prefix) > 30 or len(lines) == 0: |
| 464 | lines.insert(0, "") |
| 465 | print(" --%-30s %s" % (prefix, lines[0]), file=file) |
| 466 | for line in lines[1:]: |
| 467 | print("%-34s %s" % (" ", line), file=file) |
| 468 | print(file=file) |
| 469 | |
| 470 | def _help_callback(self, value: bool) -> None: |
| 471 | if value: |