def handle_opt():
        global cluster_name, cluster_name_prefix, colo, decom_hostlist, decom_node
        try:
                opts, args = getopt.getopt(sys.argv[1:], "hc:s:f:n:", ["help", "cluster=", "colo=", "file=", "hostname="])
        except getopt.GetoptError as err:
                usage()
                sys.exit(wrong_parameter)
        for op, value in opts:
                if op in ("-c", "--cluster"):
                        cluster_name_prefix = str.lower(value)
                        print cluster_name_prefix
                elif op in ("-f", "--file"):
                        decom_hostlist = str.lower(value)
                        print decom_hostlist
                elif op in ("-n", "--hostname"):
                        decom_node = str.lower(value)
                        print decom_node
                elif op in ("-s", "--colo"):
                        colo = str.lower(value)
                        print colo
                elif op in ("-h", "--help"):
                        usage()
                        sys.exit(wrong_parameter)
        cluster_name = cluster_name_prefix + "-" + colo

以上脚本命名为handle_opt.py

用法如下:

python handle_opt.py --cluster Hera --colo PHX --hostname $server_name

此脚本对参数大小写不敏感,同时参数支持短选项和长选项。