I'm currently starting up the server with the following Uvicorn command:
main:app --host 0.0.0.0 --port 8003 --access-log
And I would like to add an extra argument --foo
such that it works as argparse's "store_true" action so I can optionally execute a function during my startup.
With Python, using argparse I can achieve this executing the command main.py --migrate
:
parser = argparse.ArgumentParser(description="Startup")
parser.add_argument("--foo", dest="run_foo", action="store_true")
args = parser.parse_args()
@app.on_event("startup")
async def startup_event():
if args.run_foo:
foo()
Where app
is my FastAPI instance. However, I get an uvicorn: error: unrecognized arguments: main:app --host 0.0.0.0 --port 8003 --access-log
when I try to execute it using Uvicorn. Is there a way to do this?