我已經寫了這個python程序。每當我運行腳本使用參數如無法使用Optparse將參數傳遞給python
python script.py -t它返回我當前時間在unixtime。
,但每當我試圖傳遞一個參數一樣
蟒蛇script.py -c 1325058720它說沒有定義LMT。所以我刪除了LMT從
LMT = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.localtime())
然後,它只是跳過我的論點,並返回LOCALTIME當前時間。
有人可以幫助我在LMT中傳遞參數並將其轉換爲可讀時間格式。我需要將參數傳遞給它並以本地時間可讀格式查看輸出
import optparse
import re
import time
GMT = int(time.time())
AMT = 123456789
LMT = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.localtime(LMT))
VERBOSE=False
def report(output,cmdtype="UNIX COMMAND:"):
#Notice the global statement allows input from outside of function
if VERBOSE:
print "%s: %s" % (cmdtype, output)
else:
print output
#Function to control option parsing in Python
def controller():
global VERBOSE
p = optparse.OptionParser()
p.add_option('--time', '-t', action="store_true", help='gets current time in epoch')
p.add_option('--nums', '-n', action="store_true", help='gets the some random number')
p.add_option('--conv', '-c', action="store_true", help='convert epoch to readable')
p.add_option('--verbose', '-v',
action = 'store_true',
help='prints verbosely',
default=False)
#Option Handling passes correct parameter to runBash
options, arguments = p.parse_args()
if options.verbose:
VERBOSE=True
if options.time:
value = GMT
report(value, "GMT")
elif options.nums:
value = AMT
report(value, "AMT")
elif options.conv:
value = LMT
report(value, "LMT")
else:
p.print_help()
我錯了,訪問變量之外的函數沒有點擊我。 – shobhit 2011-12-28 10:52:28