def _handle_exit(sigid, frame): (msg, rc) = SIG_EXIT[sigid] contents = StringIO() contents.write("%s, prgname [%s], version [%s]\n" % (msg, _prgname, myversion.version_string())) _pprint_frame(frame, 1, BACK_FRAME_TRACE_DEPTH, contents) print "%s" % (contents.getvalue()) sys.exit(rc)
def main(): # http://python.usyiyi.cn/python_278/library/argparse.html # dest=files, argparse.FileType parser = argparse.ArgumentParser() # Top Level args parser.add_argument('--version', '-v', action='version', version='%(prog)s ' + (myversion.version_string())) parser.add_argument('--require', '-r', action='store_const', help=('this option is mantandary'), required=True, const='yes') parser.add_argument('--debug', '-d', action='store_true', help=('action store_true testing ' ' (default: %(default)s)'), default=False) parser.add_argument('--mode', '-m', action='store', help=('action store testing and choice testing ' '(default: %(default)s)'), default='config', choices=('init', 'config', 'final')) parser.add_argument('--config', '-c', action='store_const', help=('action store_const testing'), const=56) parser.add_argument('--file', '-f', action='append', help=('action append testing, add more files')) subparsers = parser.add_subparsers() parser_init = subparsers.add_parser('init', help=('test subparsers function')) parser_init.add_argument('--local', '-l', action='store_true', help="start in local mode (default: %(default)s)", default=False) parser_init.set_defaults(action=('init', main_init)) args = parser.parse_args() print "args.debug = %d" % (args.debug) print "args.config = %d" % (args.debug) print "args.mode = %s" % (args.mode) print "args.required= %s" % (args.require) if args.file is not None: print "args.file type = %s" % (type(args.file)) print "args.file = %s" % (args.file) if args.action is not None: print "args.action =%s" % (type(args.action)) (name, func) = args.action print "args.action name = %s" % (name) print "args.local= %s" % (args.local) func() print 'hello'