在您的来源中flask_script
可以看到,"too many arguments"
当执行Command
的属性capture_all_args
设置True
为未在任何地方记录的属性时,可以防止错误。
您可以在运行管理器之前在类上设置该属性
if __name__ == "__main__":
from flask.ext.script import Command
Command.capture_all_args = True
manager.run()
像这样向经理提供的额外参数总是被接受的。
此快速修复的缺点是您无法再以正常方式向命令注册选项或参数。
如果您仍然需要该功能,您可以像这样子类化Manager
并覆盖command
装饰器
class MyManager(Manager):
def command(self, capture_all=False):
def decorator(func):
command = Command(func)
command.capture_all_args = capture_all
self.add_command(func.__name__, command)
return func
return decorator
然后你可以command
像这样使用装饰器
@manager.command(True) # capture all arguments
def use_all(*args):
print("args:", args[0])
@manager.command() # normal way of registering arguments
def normal(name):
print("name", name)
请注意,由于某种原因flask_script
需要use_all
接受可变参数,但会存储参数列表,args[0]
其中有点奇怪。def use_all(args):
不起作用并且失败TypeError "got multiple values for argument 'args'"