PythonGetOpt-如何检查参数的不同可能组合?
如何使用getopt,以便在每个可能的顺序中检查2个参数的每个有效组合 例如:PythonGetOpt-如何检查参数的不同可能组合?,python,command-line,getopt,Python,Command Line,Getopt,如何使用getopt,以便在每个可能的顺序中检查2个参数的每个有效组合 例如: try: opts, args = getopt.getopt(argv,"a:b") except getopt.GetoptError: try: opts, args = getopt.getopt(argv,"a:c") except: sys.exit(1) 例如,输入可以是script.py-a avalue-b bvalue或script
try:
opts, args = getopt.getopt(argv,"a:b")
except getopt.GetoptError:
try:
opts, args = getopt.getopt(argv,"a:c")
except:
sys.exit(1)
例如,输入可以是script.py-a avalue-b bvalue
或script.py-a avalue-c cvalue
。
a和其他论点的立场也可以颠倒
但是我在执行脚本时只会得到令人困惑的值。首先,您可能需要查看
argparse
。它需要更多的手动阅读开始,但它可以做更多
使用getopt
,开关始终是可选的,并且可以以任何顺序出现。也就是说,如果你这样做了
opts, args = getopt.getopt(argv, "a:b:c:")
用户可以按任意顺序给出-a avalue
、-b bvalue
和-c cvalue
,并省略其中任何或全部
另外,请注意,getopt
不希望在参数列表中找到程序名,这意味着您通常希望传递argv[1://code>