__main__.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # Source code: https://github.com/hamdanal/rich-argparse
  2. # MIT license: Copyright (c) Ali Hamdan <ali.hamdan.dev@gmail.com>
  3. from __future__ import annotations
  4. if __name__ == "__main__":
  5. import argparse
  6. import sys
  7. from rich.terminal_theme import DIMMED_MONOKAI
  8. from rich_argparse import HelpPreviewAction, RichHelpFormatter
  9. parser = argparse.ArgumentParser(
  10. prog="python -m rich_argparse",
  11. formatter_class=RichHelpFormatter,
  12. description=(
  13. "This is a [link https://pypi.org/project/rich]rich[/]-based formatter for "
  14. "[link https://docs.python.org/3/library/argparse.html#formatter-class]"
  15. "argparse's help output[/].\n\n"
  16. "It enables you to use the powers of rich like markup and highlights in your CLI help. "
  17. ),
  18. epilog=":link: Read more at https://github.com/hamdanal/rich-argparse#usage.",
  19. )
  20. parser.add_argument(
  21. "formatter-class",
  22. help=(
  23. "Simply pass `formatter_class=RichHelpFormatter` to the argument parser to get a "
  24. "colorful help like this."
  25. ),
  26. )
  27. parser.add_argument(
  28. "styles",
  29. help="Customize your CLI's help with the `RichHelpFormatter.styles` dictionary.",
  30. )
  31. parser.add_argument(
  32. "--highlights",
  33. metavar="REGEXES",
  34. help=(
  35. "Highlighting the help text is managed by the list of regular expressions "
  36. "`RichHelpFormatter.highlights`. Set to empty list to turn off highlighting.\n"
  37. "See the next two options for default values."
  38. ),
  39. )
  40. parser.add_argument(
  41. "--syntax",
  42. default=RichHelpFormatter.styles["argparse.syntax"],
  43. help=(
  44. "Text inside backticks is highlighted using the `argparse.syntax` style "
  45. "(default: %(default)r)"
  46. ),
  47. )
  48. parser.add_argument(
  49. "-o",
  50. "--option",
  51. metavar="METAVAR",
  52. help="Text that looks like an --option is highlighted using the `argparse.args` style.",
  53. )
  54. group = parser.add_argument_group(
  55. "more arguments",
  56. description=(
  57. "This is a custom group. Group names are [italic]*Title Cased*[/] by default. Use the "
  58. "`RichHelpFormatter.group_name_formatter` function to change their format."
  59. ),
  60. )
  61. group.add_argument(
  62. "--more",
  63. nargs="*",
  64. help="This formatter works with subparsers, mutually exclusive groups and hidden arguments.",
  65. )
  66. mutex = group.add_mutually_exclusive_group()
  67. mutex.add_argument(
  68. "--rich",
  69. action="store_true",
  70. help="Rich and poor are mutually exclusive. Choose either one but not both.",
  71. )
  72. mutex.add_argument(
  73. "--poor", action="store_false", dest="rich", help="Does poor mean --not-rich 😉?"
  74. )
  75. mutex.add_argument("--not-rich", action="store_false", dest="rich", help=argparse.SUPPRESS)
  76. parser.add_argument(
  77. "--generate-rich-argparse-preview",
  78. action=HelpPreviewAction,
  79. path="rich-argparse.svg",
  80. export_kwds={"theme": DIMMED_MONOKAI},
  81. )
  82. # There is no program to run, always print help (except for the hidden --generate option)
  83. # You probably don't want to do this in your own code.
  84. if any(arg.startswith("--generate") for arg in sys.argv):
  85. parser.parse_args()
  86. else:
  87. parser.print_help()