Skip to content

Instantly share code, notes, and snippets.

@evertrol
Last active January 27, 2016 11:33
Show Gist options
  • Save evertrol/09d7fe69efb65bbc35d2 to your computer and use it in GitHub Desktop.
Save evertrol/09d7fe69efb65bbc35d2 to your computer and use it in GitHub Desktop.
Python argparse -h/--help like git
"""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