Skip to content

Add ability to stop parsing kwargs at first unknown arg #630

@cdb39

Description

@cdb39

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions