Skip to content

Commit 1543968

Browse files
author
evieira
committed
add cleaner/earlier arg parsing
Signed-off-by: evieira <t01etvi@tryg.dk>
1 parent 60821b9 commit 1543968

2 files changed

Lines changed: 15 additions & 4 deletions

File tree

sqlmesh/cli/main.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@
4444
LOCAL_ONLY_COMMANDS = ("format",)
4545

4646

47+
class _SQLMeshGroup(click.Group):
48+
def parse_args(self, ctx: click.Context, args: t.List[str]) -> t.List[str]:
49+
rest = super().parse_args(ctx, args)
50+
# Preserve the subcommand arguments because Click consumes them before invoking the group callback.
51+
protected_args = getattr(ctx, "_protected_args", None)
52+
if protected_args is None:
53+
protected_args = ctx.protected_args
54+
ctx.meta["subcommand_args"] = tuple(protected_args) + tuple(ctx.args)
55+
return rest
56+
57+
4758
def _sqlmesh_version() -> str:
4859
try:
4960
from sqlmesh import __version__
@@ -53,7 +64,7 @@ def _sqlmesh_version() -> str:
5364
return "0.0.0"
5465

5566

56-
@click.group(no_args_is_help=True)
67+
@click.group(cls=_SQLMeshGroup, no_args_is_help=True)
5768
@click.version_option(version=_sqlmesh_version(), message="%(version)s")
5869
@opt.paths
5970
@opt.config
@@ -118,7 +129,8 @@ def cli(
118129
load = True
119130
# Local-only gating must hold for any number of --paths, so it stays outside the block below.
120131
load_state = ctx.invoked_subcommand not in LOCAL_ONLY_COMMANDS
121-
if ctx.invoked_subcommand == "lint" and "--local" in sys.argv:
132+
# The parent callback constructs Context before Click invokes `lint`, so inspect its parsed args here.
133+
if ctx.invoked_subcommand == "lint" and "--local" in ctx.meta["subcommand_args"]:
122134
load_state = False
123135

124136
if len(paths) == 1:

tests/cli/test_cli.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2305,10 +2305,9 @@ def test_lint_still_loads_state(runner: CliRunner, tmp_path: Path, mocker):
23052305
assert mock.called, "state-sync was never accessed during `lint`"
23062306

23072307

2308-
def test_lint_local_runs_without_state(runner: CliRunner, tmp_path: Path, mocker, monkeypatch):
2308+
def test_lint_local_runs_without_state(runner: CliRunner, tmp_path: Path, mocker):
23092309
mock = _setup_local_only_project(tmp_path, mocker)
23102310
init_spy = mocker.spy(Context, "__init__")
2311-
monkeypatch.setattr("sys.argv", ["sqlmesh", "--paths", str(tmp_path), "lint", "--local"])
23122311

23132312
result = runner.invoke(cli, ["--paths", str(tmp_path), "lint", "--local"])
23142313

0 commit comments

Comments
 (0)