python 的 Optparse
Optparse of python
我是 python 的 optparse 的新手并尝试了以下操作:
def plot():
x=[1,2,3]
y=[4,5,6]
plt.plot(x,y)
plt.savefig('trial.pdf')
parser=OptionParser()
parser.add_option("-p", "--plot", action="callback", callback=plot)
(options, args)=parser.parse_args()
我在终端输入 python3 parse.py -p
,它给出了:
Traceback (most recent call last):
File "firstparse.py", line 15, in <module>
(options, args)=parser.parse_args()
File "/usr/lib/python3.5/optparse.py", line 1386, in parse_args
stop = self._process_args(largs, rargs, values)
File "/usr/lib/python3.5/optparse.py", line 1430, in _process_args
self._process_short_opts(rargs, values)
File "/usr/lib/python3.5/optparse.py", line 1535, in _process_short_opts
option.process(opt, value, values, self)
File "/usr/lib/python3.5/optparse.py", line 784, in process
self.action, self.dest, opt, value, values, parser)
File "/usr/lib/python3.5/optparse.py", line 804, in take_action
self.callback(self, opt, value, parser, *args, **kwargs)
TypeError: plot() takes 0 positional arguments but 4 were given
我不太了解位置参数错误。谁能告诉我哪里错了?
查看 documentation:
当使用 callback
时,您提供了一个用于处理传入参数的函数,该函数使用四个参数调用:option, opt_str, value, parser
option
is the Option instance that’s calling the callback
opt_str
is the option string seen on the command-line that’s triggering the callback...
value
is the argument to this option seen on the command-line...
parser
is the OptionParser instance driving the whole thing, mainly useful because you can access some other interesting data through its instance attributes...
本质上,您是在尝试通过 plot
函数将命令行中的参数处理成它的值。
我怀疑您正在尝试从命令行选择操作(例如 plot
)到 运行,所以可能更像是:
parser=ArgumentParser()
parser.add_argument("-p", "--plot", action="store_true")
args = parser.parse_args()
if args.plot is True:
plot()
注意:optparse
自 3.2 以来已被弃用,您现在应该使用 argparse
。
我是 python 的 optparse 的新手并尝试了以下操作:
def plot():
x=[1,2,3]
y=[4,5,6]
plt.plot(x,y)
plt.savefig('trial.pdf')
parser=OptionParser()
parser.add_option("-p", "--plot", action="callback", callback=plot)
(options, args)=parser.parse_args()
我在终端输入 python3 parse.py -p
,它给出了:
Traceback (most recent call last):
File "firstparse.py", line 15, in <module>
(options, args)=parser.parse_args()
File "/usr/lib/python3.5/optparse.py", line 1386, in parse_args
stop = self._process_args(largs, rargs, values)
File "/usr/lib/python3.5/optparse.py", line 1430, in _process_args
self._process_short_opts(rargs, values)
File "/usr/lib/python3.5/optparse.py", line 1535, in _process_short_opts
option.process(opt, value, values, self)
File "/usr/lib/python3.5/optparse.py", line 784, in process
self.action, self.dest, opt, value, values, parser)
File "/usr/lib/python3.5/optparse.py", line 804, in take_action
self.callback(self, opt, value, parser, *args, **kwargs)
TypeError: plot() takes 0 positional arguments but 4 were given
我不太了解位置参数错误。谁能告诉我哪里错了?
查看 documentation:
当使用 callback
时,您提供了一个用于处理传入参数的函数,该函数使用四个参数调用:option, opt_str, value, parser
option
is the Option instance that’s calling the callback
opt_str
is the option string seen on the command-line that’s triggering the callback...
value
is the argument to this option seen on the command-line...
parser
is the OptionParser instance driving the whole thing, mainly useful because you can access some other interesting data through its instance attributes...
本质上,您是在尝试通过 plot
函数将命令行中的参数处理成它的值。
我怀疑您正在尝试从命令行选择操作(例如 plot
)到 运行,所以可能更像是:
parser=ArgumentParser()
parser.add_argument("-p", "--plot", action="store_true")
args = parser.parse_args()
if args.plot is True:
plot()
注意:optparse
自 3.2 以来已被弃用,您现在应该使用 argparse
。