python中的getopt怎么理解

在看crfascnn代码的demo时看到这个语句,上网查了一下,可以参考这个博文:

http://blog.csdn.net/chengxuyuanyonghu/article/details/42556885

demo中相关的代码以及我的解释如下:

    try:
        opts, args = getopt.getopt(argv, 'hi:o:g:', ["ifile=", "ofile=", "gpu="]) # argy为运行程序时自己添加的参数,如运行程序:python crfasrnn_demo.py -i my_pic.jpg -o my_out.png -g -1 即可指定自己的图片为my_pic.jpg,输出my_out.png
    except getopt.GetoptError:
        print("crfasrnn_demo.py -i <input_file> -o <output_file> -g <gpu_device>")
        sys.exit(2)

    for opt, arg in opts:
        if opt == '-h':
            print("crfasrnn_demo.py -i <inputfile> -o <outputfile> -g <gpu_device>")
            sys.exit()
        elif opt in ("-i", "ifile"): #若指定了ifile的参数,更新input_file
            input_file = arg
        elif opt in ("-o", "ofile"): #若指定了ofile的参数,更新output_file
            output_file = arg
        elif opt in ("-g", "gpudevice"):
            gpu_device = int(arg)


猜你喜欢

转载自blog.csdn.net/scut_salmon/article/details/79129378