#!/usr/bin/env python """ Example of using python's argparse module to offer a stand-alone option (e.g. extra help screen), which will skip the required parameters (e.g. the filename). Copyright (C) 2016 Assaf Gordon License: MIT """ import sys,argparse class MoreHelpAction(argparse.Action): def __init__(self, option_strings, dest, **kwargs): super(MoreHelpAction, self).__init__(option_strings, '', nargs=0, **kwargs) def __call__(self, parser, namespace, values, option_string=None): text=""" This is an extra help screen. Enjoy it... """ print text sys.exit(0) def parse_command_line(): # Define parameters parser = argparse.ArgumentParser( formatter_class=argparse.RawDescriptionHelpFormatter, description="argparse example", epilog=""" Standard usage requires a filename, but using --more-help will not. Example: # Without a filename, usage error is reported: $ %(prog)s # with a filename, program runs: $ %(prog)s input.txt # with --more-help, a differet help script is show, # not requiring filename parameters $ %(prog)s --more-help """) # Add special action parameter parser.add_argument('--more-help', action=MoreHelpAction, help="Additional information") # Positional parameter parser.add_argument('filename', metavar='FILE', help='file to process'); args = parser.parse_args() return args if __name__ == "__main__": args = parse_command_line() print "File to process:", args.filename