Skip to content

Instantly share code, notes, and snippets.

@jtprince
Last active March 26, 2020 18:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jtprince/85036a0a4e40650e475ab183c72f27c1 to your computer and use it in GitHub Desktop.
Save jtprince/85036a0a4e40650e475ab183c72f27c1 to your computer and use it in GitHub Desktop.
Appending blocks of text to argparse help message
""" An example of appending blocks to a help message. """
import argparse
import sys
TEXT_TO_APPEND = """
examples:
This is how you do things
notes:
Lots of notes
""".strip()
def display_help_and_exit(parser):
parser.print_help()
print(TEXT_TO_APPEND)
exit(0)
class DisplayHelp(argparse.Action):
def __call__(self, parser, *args, **kwargs):
display_help_and_exit(parser)
parser = argparse.ArgumentParser(add_help=False)
parser.add_argument("myarg", help="the arg")
parser.add_argument("-h", "--help", nargs=0, action=DisplayHelp)
args = parser.parse_args()
if len(sys.argv) <= 1:
display_help_and_exit(parser)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment