Last active
January 27, 2016 11:33
-
-
Save evertrol/09d7fe69efb65bbc35d2 to your computer and use it in GitHub Desktop.
Python argparse -h/--help like git
This file contains 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
"""The doc string | |
Very lengthy documentation | |
""" | |
import argparse | |
import pydoc | |
class HelpAction(argparse.Action): | |
def __call__(self, parser, namespace, values, option_string=None): | |
if option_string == '-h': | |
parser.print_help() | |
parser.exit() | |
elif option_string == '--help': | |
pydoc.pager(__doc__) | |
parser.exit() | |
parser = argparse.ArgumentParser(add_help=False) | |
parser.add_argument('whatever') | |
parser.add_argument('-h', '--help', nargs=0, action=HelpAction, | |
help="Display this help") | |
parser.parse_args() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment