Skip to content
This repository was archived by the owner on Sep 13, 2023. It is now read-only.

Error on extra cli arguments #461

Merged
merged 5 commits into from
Nov 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions mlem/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import click
import typer
from click import Abort, ClickException, Command, HelpFormatter, Parameter
from click.exceptions import Exit, MissingParameter
from click.exceptions import Exit, MissingParameter, NoSuchOption
from pydantic import ValidationError
from typer import Context, Option, Typer
from typer.core import TyperCommand, TyperGroup
Expand Down Expand Up @@ -133,6 +133,8 @@ def make_context(
**extra: Any,
) -> Context:
args_copy = args[:]
if self.dynamic_options_generator:
extra["ignore_unknown_options"] = True
ctx = super().make_context(info_name, args, parent, **extra)
if not self.dynamic_options_generator:
return ctx
Expand All @@ -146,6 +148,16 @@ def make_context(
params.update(ctx.params)

if ctx.args == extra_args:
if not self.ignore_unknown_options:
from difflib import get_close_matches

opt = "--" + get_extra_keys(extra_args)[0]
possibilities = get_close_matches(
opt, {f"--{o}" for o in params}
)
raise NoSuchOption(
opt, possibilities=possibilities, ctx=ctx
)
break
extra_args = ctx.args

Expand Down Expand Up @@ -382,9 +394,7 @@ def mlem_command(
def decorator(f):
context_settings = kwargs.get("context_settings", {})
if dynamic_options_generator:
context_settings.update(
{"allow_extra_args": True, "ignore_unknown_options": True}
)
context_settings.update({"allow_extra_args": True})
if no_pass_from_parent is not None:
_pass_from_parent = [
a
Expand Down
11 changes: 10 additions & 1 deletion tests/cli/test_declare.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ def test_declare_list(runner: Runner, tmp_path, args, res):
def test_declare_dict(runner: Runner, tmp_path, args, res):
result = runner.invoke(
f"declare builder pip {make_posix(str(tmp_path))} --package_name lol --target lol "
+ args
+ args,
raise_on_error=True,
)
assert result.exit_code == 0, (result.exception, result.output)
builder = load_meta(str(tmp_path))
Expand Down Expand Up @@ -502,3 +503,11 @@ def test_declare_deployment_env(
meta = load_meta(path, force_type=MlemDeploymentMock)

assert meta.env == env_value


def test_declare_unknown_option_raises(runner: Runner):
with pytest.raises(RuntimeError, match=".*No such option.*"):
runner.invoke(
f"--tb declare deployment {MlemDeploymentMock.type} nowhere --nonexistent_option value",
raise_on_error=True,
)