打印這檔節目,我有:檢測,如果沒有命令行參數提供
from sys import argv
script, arg1 = argv
def program(usr_input, arg1):
if(usr_input == arg1):
print "CLI argument and user input are identical"
else:
print "CLI argument and user input aren't identical"
if arg1 != "":
usr_input = raw_input("enter something: ")
program(usr_input, arg1)
else:
print "You have not entered a CLI argument at all."
我得到:
Traceback (most recent call last):
File "filename.py", line 3, in <module>
script, arg1 = argv
ValueError: need more than 1 value to unpack
如何檢測缺乏命令行參數,並拋出錯誤/異常,而不是收到此錯誤?
的argv的只是參數的數組...所以檢查它有多長。 –
考慮一個最小的測試用例:'a,b = [「oops」]'爲什麼這不起作用?如何修復/避免? – 2012-12-24 03:34:07