diff --git a/src/fastapi_cli/cli.py b/src/fastapi_cli/cli.py index 28afa29..d6f7da9 100644 --- a/src/fastapi_cli/cli.py +++ b/src/fastapi_cli/cli.py @@ -99,6 +99,7 @@ def _run( app: Union[str, None] = None, entrypoint: Union[str, None] = None, proxy_headers: bool = False, + reload_dirs: Union[str, None] = None, forwarded_allow_ips: Union[str, None] = None, ) -> None: with get_rich_toolkit() as toolkit: @@ -185,6 +186,7 @@ def _run( workers=workers, root_path=root_path, proxy_headers=proxy_headers, + reload_dirs=reload_dirs.split(",") if reload_dirs else None, forwarded_allow_ips=forwarded_allow_ips, log_config=get_uvicorn_log_config(), ) @@ -244,6 +246,12 @@ def dev( help="Enable/Disable X-Forwarded-Proto, X-Forwarded-For, X-Forwarded-Port to populate remote address info." ), ] = True, + reload_dirs: Annotated[ + Union[str, None], + typer.Option( + help="Comma separated list of directories to watch for changes in. If not provided, by default the whole current directory will be watched." + ), + ] = None, forwarded_allow_ips: Annotated[ Union[str, None], typer.Option( @@ -286,6 +294,7 @@ def dev( entrypoint=entrypoint, command="dev", proxy_headers=proxy_headers, + reload_dirs=reload_dirs, forwarded_allow_ips=forwarded_allow_ips, ) diff --git a/tests/test_cli.py b/tests/test_cli.py index b87a811..a424664 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -30,6 +30,7 @@ def test_dev() -> None: "workers": None, "root_path": "", "proxy_headers": True, + "reload_dirs": None, "forwarded_allow_ips": None, "log_config": get_uvicorn_log_config(), } @@ -60,6 +61,7 @@ def test_dev_package() -> None: "workers": None, "root_path": "", "proxy_headers": True, + "reload_dirs": None, "forwarded_allow_ips": None, "log_config": get_uvicorn_log_config(), } @@ -96,6 +98,8 @@ def test_dev_args() -> None: "--app", "api", "--no-proxy-headers", + "--reload-dirs", + "api,config", ], ) assert result.exit_code == 0, result.output @@ -109,6 +113,10 @@ def test_dev_args() -> None: "workers": None, "root_path": "/api", "proxy_headers": False, + "reload_dirs": [ + "api", + "config", + ], "forwarded_allow_ips": None, "log_config": get_uvicorn_log_config(), } @@ -139,6 +147,7 @@ def test_dev_env_vars() -> None: "workers": None, "root_path": "", "proxy_headers": True, + "reload_dirs": None, "forwarded_allow_ips": None, "log_config": get_uvicorn_log_config(), } @@ -176,6 +185,7 @@ def test_dev_env_vars_and_args() -> None: "workers": None, "root_path": "", "proxy_headers": True, + "reload_dirs": None, "forwarded_allow_ips": None, "log_config": get_uvicorn_log_config(), } @@ -204,6 +214,7 @@ def test_run() -> None: "workers": None, "root_path": "", "proxy_headers": True, + "reload_dirs": None, "forwarded_allow_ips": None, "log_config": get_uvicorn_log_config(), } @@ -230,6 +241,7 @@ def test_run_trust_proxy() -> None: "workers": None, "root_path": "", "proxy_headers": True, + "reload_dirs": None, "forwarded_allow_ips": "*", "log_config": get_uvicorn_log_config(), } @@ -276,6 +288,7 @@ def test_run_args() -> None: "workers": 2, "root_path": "/api", "proxy_headers": False, + "reload_dirs": None, "forwarded_allow_ips": None, "log_config": get_uvicorn_log_config(), } @@ -307,6 +320,7 @@ def test_run_env_vars() -> None: "workers": None, "root_path": "", "proxy_headers": True, + "reload_dirs": None, "forwarded_allow_ips": None, "log_config": get_uvicorn_log_config(), } @@ -340,6 +354,7 @@ def test_run_env_vars_and_args() -> None: "workers": None, "root_path": "", "proxy_headers": True, + "reload_dirs": None, "forwarded_allow_ips": None, "log_config": get_uvicorn_log_config(), } @@ -375,6 +390,7 @@ def test_dev_help() -> None: assert "The root path is used to tell your app" in result.output assert "The name of the variable that contains the FastAPI app" in result.output assert "Use multiple worker processes." not in result.output + assert "directories to watch for changes in." in result.output def test_run_help() -> None: @@ -396,6 +412,7 @@ def test_run_help() -> None: assert "The root path is used to tell your app" in result.output assert "The name of the variable that contains the FastAPI app" in result.output assert "Use multiple worker processes." in result.output + assert "directories to watch for changes in." not in result.output def test_callback_help() -> None: @@ -426,6 +443,7 @@ def test_dev_with_import_string() -> None: "reload": True, "workers": None, "root_path": "", + "reload_dirs": None, "proxy_headers": True, "log_config": get_uvicorn_log_config(), } @@ -447,6 +465,7 @@ def test_run_with_import_string() -> None: "reload": False, "workers": None, "root_path": "", + "reload_dirs": None, "proxy_headers": True, "log_config": get_uvicorn_log_config(), }