getopt [解析参数]

    import sys,os
    import getopt

    try:
        options,argsErr = getopt.getopt(sys.argv[1:],"hu:c:",["help","user=","cmd="])    # 中间短参数,后面长参数对应. 不带:或=代表不带参数
    except getopt.GetoptError:
        print "Unknown parameters,More info with: %s -h" %(sys.argv[0])
        sys.exit(2)
    if argsErr != []:
        print "Unknown parameters,More info with: %s -h" %(sys.argv[0])
        sys.exit(2)

    for o,a in  options:
        if o in ("-h","--help"):
            print '''Usage: python te.py -u user -c "cmd -options" '''
            sys.exit(2)
        if o in ("-u","--user"):
            user = a
        if o in ("-c","--cmd"):
            cmd = a
    print user,cmd
文档更新时间: 2018-11-21 17:29   作者:RuM