Mehrdad Mirzaei
Mehrdad Mirzaei

Reputation: 93

Run Flask app with arguments in command line

I have a flask app that works fine. However I need to include arguments when running the app in the command line. I can use python app.py arg1 but when I try using flask run arg1 it throws the error:

Error: Got unexpected extra argument (arg1)

Is there any way to add arguments to the flask run command?

Upvotes: 1

Views: 3345

Answers (1)

Alex
Alex

Reputation: 516

Instead of using flask run you could define your entrypoint like so

import sys
from flask import Flask

if __name__ == '__main__':
  foo = sys.argv[1]  
  app = Flask(__name__)
  app.config['foo'] = foo
  app.run()

However, it's probably wiser to create a custom cli command to take arguments for you and start up the flask app. Read more about custom cli commands here.

Upvotes: 1

Related Questions