Python optparse:parse.error()--->TypeError:error()缺少1个必需的位置参数:“msg”
在新的Python 3.4.3上运行此跟踪路由模块时遇到问题。在python的早期版本中,我能够运行该模块,但现在我无法运行。当我尝试调用optparse的parser.error函数时,我一直收到这个回溯错误Python optparse:parse.error()--->TypeError:error()缺少1个必需的位置参数:“msg”,python,typeerror,parse-error,optparse,msg,Python,Typeerror,Parse Error,Optparse,Msg,在新的Python 3.4.3上运行此跟踪路由模块时遇到问题。在python的早期版本中,我能够运行该模块,但现在我无法运行。当我尝试调用optparse的parser.error函数时,我一直收到这个回溯错误 TypeError: error() missing 1 required positional argument: 'msg'* 下面是我的代码片段 if __name__ == "__main__": parser = optparse.OptionParser(usag
TypeError: error() missing 1 required positional argument: 'msg'*
下面是我的代码片段
if __name__ == "__main__":
parser = optparse.OptionParser(usage="%prog [options] hostname")
parser.add_option("-p", "--port", dest="port",
help="Port to use for socket connection [default:%default]",
default=33434, metavar="PORT")
parser.add_option("-m", "--max-hops", dest="max_hops",
help="Max hops before giving up [default: %default]",
default=30, metavar="MAXHOPS")
(options, args) = parser.parse_args()
if len(args) != 1:
parser.error()
else:
dest_name = args[0]
sys.exit(main(dest_name=dest_name,
port=int(options.port),
max_hops=int(options.max_hops)))
if语句中不断出现错误
if len(args) != 1:
parser.error()
然后我试了这个
if len(args) != 1:
parser.error("Incorrect number of arguments")
当我这次尝试运行模块时,我收到了这个错误
Usage: TraceRoute.py [options] hostname
TraceRoute.py: error: Incorrect number of arguments
我希望得到一些关于如何修复此代码以防止此错误的反馈,以及关于如何正确使用python 3.4.3中的optparse库的更多信息,因为python 3.2已弃用。你应该考虑切换到AgPARSE。你是正确的关于如何修复你的第一个问题-这么好的工作。至于您的第二个问题,当您得到它时,您将传递什么选项?从我为纠正我的问题所做的研究来看,新版本的python似乎不推荐使用optparse库,而我将不得不使用argparse库。在编写代码时,尤其是在python中,我仍然不熟悉使用解析。