Last active
March 26, 2020 18:45
-
-
Save jtprince/85036a0a4e40650e475ab183c72f27c1 to your computer and use it in GitHub Desktop.
Appending blocks of text to argparse help message
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" 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