1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| import sys import argparse
def cmd(): args = argparse.ArgumentParser(description = 'Personal Information ',epilog = 'Information end ') args.add_argument("name", type = str, help = "Your name") args.add_argument("birth", type = str, help = "birthday") args.add_argument("-r",'--race', type = str, dest = "race", help = u"民族") args.add_argument("-a", "--age", type = int, dest = "age", help = "Your age", default = 0, choices=range(150)) args.add_argument('-s',"--sex", type = str, dest = "sex", help = 'Your sex', default = 'male', choices=['male', 'female']) args.add_argument("-p","--parent",type = str, dest = 'parent', help = "Your parent", default = "None", nargs = '*') args.add_argument("-o","--other", type = str, dest = 'other', help = "other Information",required = False,nargs = '*')
args = args.parse_args() print "argparse.args=",args,type(args) print 'name = %s'%args.name d = args.__dict__ for key,value in d.iteritems(): print '%s = %s'%(key,value)
if __name__=="__main__": cmd()
|