在我的python脚本中使用getopt.getopt()函数时,temp返回值保持为空。我错过了什么。
def ParseOpts(cmdName):
shortForm = 'c:n:';
longForm = 'cluster=,node='.split(',');
try:
print sys.argv;
temp, args = getopt.getopt(sys.argv, shortForm, longForm);
print temp;
except getopt.GetoptError:
print 'error !!'命令:
$ python helloWorld.py --cluster=Test --node=Test2
['helloWorld.py', '--cluster=Test', '--node=Test2']
[]发布于 2017-07-24 03:31:21
您混淆了getopt,它只需要一个参数列表,因为它提供了整个sys.argv。它会立即看到文本"helloWorld.py" (它不能将其解析为getopt参数),并假设它已命中参数列表的末尾。您想跳过第一个参数,因为它是程序名。
temp, args = getopt.getopt(sys.argv[1:], shortForm, longForm)https://stackoverflow.com/questions/45272109
复制相似问题