Skip to content

Document how to have variadic positional arguments without a leading flag #439

@rec

Description

@rec

Thanks for your work on this very exciting CLI parser.

See #39, which does not appear to cover this.

I want to create a command fing which combines several files, with a usage like this:

usage: fing [-h] PATH [PATH ...]

but the naïve:

def main(*files: Path) -> None:
    print(*files)

gives

usage: /Users/tom/code/fing/fing/__main__.py [-h] [--files [PATH [PATH ...]]]

I don't see any examples of a command which doesn't use flags, and I see this unit test seemingly confirming this behavior.

After some fiddling, I found out that this works:

def main(files: list[Path], /) -> None:
    print(files)

but this doesn't:

def main(files: list[Path]) -> None:
    print(files)

and of course, this isn't even correct Python:

def main(*files: Path, /) -> None:
    print(files)

The above workaround seems very reasonable once you know it: I'm suggesting that it be documented somewhere around https://brentyi.github.io/tyro/examples/basics/#positional-arguments

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions