【问题标题】:How to get all available Command Options to set environment variables?如何获取所有可用的命令选项来设置环境变量?
【发布时间】:2023-03-17 20:25:02
【问题描述】:

flask manual about Setting Command Options 只讲了命令FLASK_RUN_PORT 设置可以通过点击加载的环境变量。

如何找到其他选项并将它们与FLASK_COMMAND_OPTION 模式一起使用?

我想将它设置为我的 vscode launch.json。

【问题讨论】:

    标签: python flask


    【解决方案1】:

    您可以通过在 shell 中执行来访问所有可用的命令:

    flask --help
    
    [...]
    Commands:
          db     Perform database migrations.
          run    Runs a development server.
          shell  Runs a shell in the app context.
    

    如果你想列出给定命令的所有可用选项,例如run

    flask run --help 
    
    Options:
          -h, --host TEXT                 The interface to bind to.
          -p, --port INTEGER              The port to bind to.
          --reload / --no-reload          Enable or disable the reloader.  By default
                                          the reloader is active if debug is enabled.
          --debugger / --no-debugger      Enable or disable the debugger.  By default
                                          the debugger is active if debug is enabled.
          --eager-loading / --lazy-loader
                                          Enable or disable eager loading.  By default
                                          eager loading is enabled if the reloader is
                                          disabled.
          --with-threads / --without-threads
                                          Enable or disable multithreading.
          --help                          Show this message and exit.
    

    因此,您可以将它们与文档示例中的模式一起使用,您只需将名称和选项用下划线连接起来,在 ALLCAPS 中:

    export FLASK_RUN_PORT=8000
    export FLASK_RUN_HOST=0.0.0.0
    

    您还可以定义布尔选项:

    export FLASK_RUN_RELOAD=True   
    export FLASK_RUN_RELOAD=False
    

    注意:flask --help 将列出默认命令,但如果您在执行此帮助 (export FLASK_APP=my_app.py) 之前定义您的应用程序,您还将获得所有自定义命令。

    Commands:
          db      Perform database migrations.
          deploy
          run     Runs a development server.
          shell   Runs a shell in the app context.
          test    perform tests
    

    【讨论】:

    • 嗨,'FLASK_RUN_HOST' 是默认命令吗?我只是想找到其他的默认命令。
    • 所有命令都出现在帮助文档的末尾,您只需将名称和选项用下划线连接起来,全部大写。
    • 对于烧瓶运行,您的方法效果很好。但是,如果我在代码中设置 app.config['FLASK_RUN_PORT']=8000 并运行烧瓶,那么它仍然在端口 5000 上启动
    • @variable 在没有上下文的情况下很难调试,您能否创建一个问题并准确定义您尝试过的内容?
    • 我的意思是通过配置变量设置端口不起作用
    猜你喜欢
    • 1970-01-01
    • 2013-09-25
    • 2018-07-22
    • 1970-01-01
    • 2021-11-20
    • 1970-01-01
    • 2014-11-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多