cyclopts icon indicating copy to clipboard operation
cyclopts copied to clipboard

Add ability to stop parsing kwargs at first unknown arg

Open cdb39 opened this issue 2 months ago • 1 comments

Great work on cyclopts btw! I'm generally loving it and trying to migrate a bunch of previously click / Typer apps to it.

One problem I've encountered is there is no way to stop parsing parameter kwargs at the first unknown string, unless you force the caller to add the -- separator. If I was building something new, I could just mandate that, but sadly I'm not.

Example:

from cyclopts import App, Parameter
from typing import Annotated

app = App()

@app.meta.default
def _default(
    *tokens: Annotated[str, Parameter(show=False, allow_leading_hyper=True)],
   param: str,
) -> None:
    print("Meta tokens:", tokens)
    print("Meta param:", param)
    app(tokens)

@app.command
def run(
    *args: Annotated[str, Parameter(allow_leading_hyphen=True)],
    param: str
) -> None:
    print("Run args:", args)
    print("Run param:", param)

If __name__ == "__main__":
    app.meta()

When I run cli.py --param foo run --param bar other args --param baz What I'd like to be able to get is:

Meta tokens: ('run', '--param', 'bar', 'other', 'args', '--param', 'baz')
Meta param: foo
Run args: ('other', 'args', '--param', 'baz')
Run param: bar

What I currently get is a multi-param error.

I can get the behaviour I want by just changing bind.create_bound_arguments to set stop_at_first_unknown=True in the first call to _parse_kw_and_flags, but I'm confident this probably has unintended side-effects!

cdb39 avatar Oct 24 '25 13:10 cdb39

I think this is another good example of #627; I'm going to close this thread to keep the conversation concentrated over there.

BrianPugh avatar Oct 25 '25 17:10 BrianPugh