-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The --
mechanism for splitting arguments doesn't get forwarded correctly
#25
Comments
Related code search in the Click source code: https://github.com/search?q=repo%3Apallets%2Fclick+%2F%5B%5E-%5D--%5B%5E-%5D%2F&type=code - had to search for |
Here's a test that illustrates the bug: @click.group(cls=DefaultGroup, default="baz", invoke_without_command=True)
def cli_with_argument_and_option():
pass
@cli_with_argument_and_option.command()
@click.argument("argument")
@click.option("--option")
def baz(argument, option):
click.echo(f"baz: argument: {argument}, option: {option}")
def test_cli_with_argument_and_option():
result = r.invoke(
cli_with_argument_and_option, ["argument", "--option", "option"]
)
assert result.output == "baz: argument: argument, option: option\n"
# Now try with a -- prefixed argument
result2 = r.invoke(
cli_with_argument_and_option, ["--", "--starts-with-hyphens"]
)
assert result2.output == "baz: argument: --starts-with-hyphens, option: None\n" Currently fails like this:
|
Stepping through with the debugger it gets to here:
Before that But after that line it is |
More debugger:
So the call to Here's that code in Click: https://github.com/pallets/click/blob/874ca2bc1c30d93a4ac6e36a15ed685eafe89097/src/click/parser.py#L326-L342 |
I think I found it:
It looks like that bit of code there is popping off that |
... and that's as far as I got. I'm lost in Click internals now and I'm not sure how to fix this. Here's my diff from where I was investigating with that test and a breakpoint: diff --git a/click_default_group.py b/click_default_group.py
index 735c54b..f743ce7 100644
--- a/click_default_group.py
+++ b/click_default_group.py
@@ -77,9 +77,17 @@ class DefaultGroup(click.Group):
self.default_cmd_name = cmd_name
def parse_args(self, ctx, args):
+ separator_index = None
+ original_args = args[:]
+ if "--" in args:
+ # Remember where it was so we can put it back later
+ separator_index = args.index("--")
if not args and self.default_if_no_args:
args.insert(0, self.default_cmd_name)
- return super(DefaultGroup, self).parse_args(ctx, args)
+ updated_args = super(DefaultGroup, self).parse_args(ctx, args)
+ if separator_index is not None:
+ breakpoint()
+ return updated_args
def get_command(self, ctx, cmd_name):
if cmd_name not in self.commands:
diff --git a/test.py b/test.py
index f90b80d..1e23b50 100644
--- a/test.py
+++ b/test.py
@@ -28,6 +28,32 @@ def bar():
r = CliRunner()
+@click.group(cls=DefaultGroup, default="baz", invoke_without_command=True)
+def cli_with_argument_and_option():
+ pass
+
+
+@cli_with_argument_and_option.command()
+@click.argument("argument")
+@click.option("--option")
+def baz(argument, option):
+ click.echo(f"baz: argument: {argument}, option: {option}")
+
+
+def test_cli_with_argument_and_option():
+ result = r.invoke(
+ cli_with_argument_and_option, ["argument", "--option", "option"]
+ )
+ assert result.output == "baz: argument: argument, option: option\n"
+ # Now try with a -- prefixed argument
+ result2 = r.invoke(
+ cli_with_argument_and_option, ["--", "--starts-with-hyphens"]
+ , catch_exceptions=False
+ )
+ assert result2.output == "baz: argument: --starts-with-hyphens, option: None\n"
+
+
+
def test_default_command_with_arguments():
assert r.invoke(cli, ['--foo', 'foooo']).output == 'foooo\n'
assert 'no such option' in r.invoke(cli, ['-x']).output.lower() |
Spotted this for my
llm
command, which usesclick-default-group
to set the default command tollm prompt
.If you run this:
llm -- '--help means what?'
You get this error:
But if you run the full command instead:
llm prompt -- '--help means what?'
You get the expected answer:
Bug reported here:
llm -- "--find me"
doesn't work butllm prompt -- "--find me"
does simonw/llm#245The text was updated successfully, but these errors were encountered: