1
我有一個簡單的python腳本,需要三個參數-f -l -a。我想-a是一個可選參數,正在嘗試/除了最有效的方法來實現這一點(我想如果他們不通過所需的參數,我需要捕捉異常)?這裏是腳本,在此先感謝:可選參數通過python腳本
try:
(options, args) = getopt.getopt(sys.argv[1:], 'f:l:a:', ['first=', 'last=', 'age='])
except getopt.GetoptError:
help()
for option in options:
if option[0] == '-f':
firstName = option[1]
elif option[0] == '-l':
lastName = option[1]
elif option[0] == '-a':
personAge = option[1]
if (firstName is None):
print >>sys.stderr, "Missing first name"
sys.exit(1)
if (lastName is None):
print >>sys.stderr, "Missing first name"
sys.exit(1)
try:
(personAge is None)
except NameError:
print("Setting personAge to unknown")
personAge = 'Unknown'
print firstName + " " + lastName + " " + personAge
您是否認爲大膽製作大字體會讓您的問題更加緊迫? –
@Jack_of_All_Trades剛剛批准了待定編輯。 – Algorithmist