Skip to content

Is there a way to construct a dataclass directly from the command line inputs? #197

Open
@Jimmy2027

Description

@Jimmy2027

Hi, is there a way to directly construct a dataclass from the command line inputs with typer? Like an argparser would do.
For a large amount of arguments this would improve readability of the code, and would permit inheritances of dataclasses like in the example below:

import typer
from dataclasses import dataclass


@dataclass
class BaseFlags:
    arg1: int = typer.Argument(help='something', default=1)
    arg2: int = typer.Argument(help='something', default=2)


@dataclass
class Flags(BaseFlags):
    arg3: int = typer.Argument(help='something', default=3)
    arg4: int = typer.Argument(help='something', default=4)


def main(args:Flags=Flags()):
    print(args)


if __name__ == '__main__':
    typer.run(main)

Thanks a lot for the great package and your help!

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionQuestion or problem

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions