我怎么知道传递给 flask_script 的经理的参数

How can I know the args passed to flask_script's Manager

我有一个 Flask 应用程序,在它的一个脚本命令中我想知道传递给管理器的参数是什么(而不是命令本身),我该怎么做?

$ cat manage.py

#!/usr/bin/env python

from flask import Flask
from flask_script import Manager

app = Flask(__name__)

manager = Manager(app)

manager.add_option("-d", "--debug", dest="debug", action="store_true")

@manager.option('-n', '--name', dest='name', default='joe')
def hello(name):
    # how can I know whether "-d|--debug" is passed in command line
    print("hello", name)

if __name__ == "__main__":
    manager.run()

如果我运行:

$ python manage.py --debug hello

我想检测 '--debug' 是否通过 hello 的函数中的命令行参数传递。我不能只是改变

manager.add_option("-d", "--debug", dest="debug", action="store_true")    

装饰器版本:

@manager.option('-d', '--debug', action='store_true', dest='debug')
@manager.option('-n', '--name', dest='name', default='joe')
def hello(name, debug=False):

因为 '-d|--debug' 被许多命令共享。

全局选项不是传递给命令,而是传递给应用程序创建函数。

参见 add-option 文档。

For this to work, the manager must be initialized with a factory function rather than a Flask instance. Otherwise any options you set will be ignored.

所以你需要做类似

的事情
app = Flask(__name__)

def init_manager(debug):
    app.debug = debug
    return app

manager = Manager(init_manager)

然后访问app.debug