Skip to content
Discussion options

You must be logged in to vote

You can support custom types by specifying parser parameter:

from datetime import datetime
from typing import Annotated

import dateparser
import typer

app = typer.Typer()


def relative_datetime(value: str) -> datetime:
    if dt := dateparser.parse(value):
        return dt
    raise typer.BadParameter(
        "Must be a ISO 8601 format or human readable relative format"
    )


@app.callback()
def main(
    start: Annotated[datetime, typer.Argument(parser=relative_datetime)],
) -> None:
    typer.echo(start)


# app "1 hour ago"
if __name__ == "__main__":
    typer.run(main)
$ python main.py "1 hour ago"
2025-09-18 22:18:46.338319

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by YuriiMotov
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature, enhancement or request investigate
2 participants
Converted from issue

This discussion was converted from issue #452 on September 18, 2025 21:21.