Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法使用optpasse将参数传递给python_Python_Optparse - Fatal编程技术网

无法使用optpasse将参数传递给python

无法使用optpasse将参数传递给python,python,optparse,Python,Optparse,我已经编写了这个python程序。每当我使用如下参数运行脚本时 python script.py-t它以unixtime格式返回当前时间 但每当我试图通过这样的辩论 python script.py-c 1325058720它说没有定义LMT。所以我把LMT从 LMT = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.localtime()) 然后它跳过我的参数,返回本地时间中的当前时间 有人能帮我在LMT中传递一个参数,并将其转换为可读的

我已经编写了这个python程序。每当我使用如下参数运行脚本时

python script.py-t它以unixtime格式返回当前时间

但每当我试图通过这样的辩论

python script.py-c 1325058720它说没有定义LMT。所以我把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()

您传入的参数完全不相关。在optparse尝试查看您的参数之前,执行以下行:

LMT = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.localtime(LMT))
正如您所指出的,LMT是未定义的,并且会引发错误。 我不知道你当时对LMT的期望是什么。localtime()将秒数从历元转换为localtime,因为您需要当前时间(如果我理解您的话),所以不需要传入任何内容

所以事实上,你首先说:

python script.py -t  # It returns me current time in unixtime.

这是错误的,事实并非如此。试试看,你会明白的。它给您一个
NameError:name'LMT'未定义

您传入的参数完全不相关。在optparse尝试查看您的参数之前,执行以下行:

LMT = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.localtime(LMT))
正如您所指出的,LMT是未定义的,并且会引发错误。 我不知道你当时对LMT的期望是什么。localtime()将秒数从历元转换为localtime,因为您需要当前时间(如果我理解您的话),所以不需要传入任何内容

所以事实上,你首先说:

python script.py -t  # It returns me current time in unixtime.

这是错误的,事实并非如此。试试看,你会明白的。它提供了一个
NameError:name'LMT'未定义

我在函数外部访问变量时出错,该函数没有单击我

 elif options.conv:
        LMT = options.conv
        LMT= float(LMT)
        LMT = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.localtime(LMT))
        print '%s'% LMT

我访问函数外部的变量是错误的,该函数没有单击我

 elif options.conv:
        LMT = options.conv
        LMT= float(LMT)
        LMT = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.localtime(LMT))
        print '%s'% LMT

我想将任何给定的参数转换为localtime,并在控制台上打印其输出。我已经找到了解决方案。ThanksI希望将任何给定参数转换为localtime,并在控制台上打印其输出。我已经找到了解决方案。ThanksI在函数外部访问变量是错误的,而函数没有单击我。我在函数外部访问变量是错误的,函数没有单击我。