无法使用 Optparse 将参数传递给 python
我写了这个Python程序。每当我使用 python script.py -t 等参数运行脚本时,
它都会返回 unixtime 中的当前时间。
但每当我尝试传递像
python script.py -c 1325058720 这样的参数时,它就会说 LMT 未定义。所以我从 Then 中删除了 LMT
LMT = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.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()
i have written this python program. whenever i run the script using parameters like
python script.py -t It returns me current time in unixtime.
but whenever i try to pass an argument like
python script.py -c 1325058720 It says LMT is not defined. So i removed the LMT from the
LMT = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.localtime())
Then it just skip my argument and returns the current time in Localtime.
Can someone please help me to pass an argument in the LMT and convert it to Readable time format. I need to pass an argument to it and see the output in the localtime readable format
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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我错误地访问了没有点击我的函数外部的变量。
I was wrong to access the variable outside the function which didn't clicked me.
您传入的参数完全无关。在 optparse 甚至尝试查看您的参数之前,就会执行此行:
正如您所指出的,LMT 未定义,并且会引发错误。
我不知道你对 LMT 的期望是什么。 time.localtime() 将几秒从纪元转换为本地时间,因为您想要当前时间(如果我理解您的话),您不需要传递任何内容。
所以其实你首先说:
这是错的,不是的。尝试一下,你就会看到。它会给您一个
NameError: name 'LMT' is not Defined
。The parameters you pass in are completely irrelevant. Way before optparse even tries to look at your parameters, this line is executed:
And as you point out, LMT is undefined, and will raise an error.
I have no idea what you expect LMT to be at that point. time.localtime() converts a number of seconds from epoch to localtime, since you want the current time (if I understand you) you don't need to pass in anything.
So in fact, you first say that:
This is wrong, it does not. Try it and you'll see. It gives you a
NameError: name 'LMT' is not defined
.