[python]getopt函数只能按指定顺序解析参数么
英雄@末路 2018-11-20 05:00:36 刚学习使用python,写个小程序,需要带参数,用了getopt,不过从网上的例子看,解析方法都是按短选项,长选项,附加选项的顺序来的。
比如网上的例子:
opts, args = getopt.getopt(sys.argv[1:], "ho:", ["help", "output="])
输入
python test1.py '-h -o file --help --output=out file1 file2'
解析opts包括: -h, -o (file), --help, --output= (out)
args包括 file1, file2
尝试这将输入参数顺序调整
python test1.py ' file1 file2 -h -o file --help --output=out'
结果opts为空
参数都跑到args里面去了。
这个是getopt 就这么规定的,没办法改变,要想不按照那个顺序,只能自己实现getopt?
还是实现代码上修改一下就可以了?