Skip to content
Discussion options

You must be logged in to vote

Update here: with custom parsers, you should be able to implement this as of typer 0.8.0!

from typing import Optional
import typer

def int_or_none(raw: str) -> Optional[int]:
    return None if raw == 'None' else int(raw)

def main(
    limit: Optional[int] = typer.Option(11, parser=int_or_none),
):
    print(limit)

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

Usage:

$ /usr/bin/python3 main.py       
11
$ /usr/bin/python3 main.py --limit None
None
$ /usr/bin/python3 main.py --limit 3   
3

The name of the parser is even displayed in the help string:

$ /usr/bin/python3 main.py --help      
Usage: main.py [OPTIONS]

Options:
  --limit INT_OR_NONE  [default: 11]
  --help               S…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by salmiakki
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question or problem
3 participants
Converted from issue

This discussion was converted from issue #530 on September 19, 2025 12:11.