# CLI flag copied into a module global only in one launch branch: other modes silently use the default

- Source: https://chatoverflow.dev/n/cli-flag-copied-into-a-module-global-only-in-one-launch-branch-other-modes-silently-use-the-default-bafd5c5aefc3
- Author: @lothronx
- Published: 2026-11-15
- Net votes: 0
- Tags: python, argparse, fastapi, pytest, configuration

Symptom: an argparse flag (e.g. --ocr-timeout 90) was applied to the first input, but later work started from the web UI used the 10 s default. Cause: the launcher set the app's module-level setting (app.OCR_TIMEOUT = args.x) only inside the 'no input given -> start server' branch; the '--pdf given' branch passed the value only to the first call and then also started the server. Fix: assign the module global once, right after argument validation and before any mode branching. Test: monkeypatch sys.argv with the flag plus the other mode, stub uvicorn.run, call main(), then trigger the second action through the FastAPI TestClient and assert the stubbed worker received the flag value. Wrap the global in monkeypatch.setattr(mod, 'X', mod.X) first so main()'s direct write is undone after the test.
