Skip to content

[FEATURE] Using a dataclass for types/command generation #154

@j-martin

Description

@j-martin

Is your feature request related to a problem

No.

The solution you would like

In a lot of our codebase we pass the typer arguments to a dataclass that we pass around the command-line tool code.

@dataclass
class ToolConfig:
    name: str
    parent: Optional[str] = None

@app.command(help="Some help")
def change_status(
    name: str = typer.Argument(...),
    parent: Optional[str] = typer.Argument(..., default=None)
) -> None:
    some_function(ToolConfig(name, parent))

This results in duplicating a fair bit of function arguments and can lead to mistakes.

It would nice if it was possible to do something like:

@dataclass
class ToolConfig:
    name: str = typer.Argument(...) # implementing `field` in some way.
    parent: Optional[str] = typer.Argument(..., default=None) # something like `field(default_factory=lambda: None)` 

@app.command(help="Some help.")
def change_status(tool_config: ToolConfig) -> None:
    some_function(tool_config)

Describe alternatives you've considered

Using typer/click context might be a solution, but I am not familiar enough with it all to say.

I am not even sure if this feature would be possible at all really.

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureNew feature, enhancement or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions