Skip to content
Discussion options

You must be logged in to vote

You can parse options using callback and save parsed key-value items in global object that you can later use

from typing import Annotated, Optional

import typer

options: dict[str, str] = {}

def parse_option(value: list[str]):
    for opt in value:
        k, v = opt.split("=", maxsplit=1)
        options[k] = v
    return value

def main(
    o: Annotated[Optional[list[str]], typer.Option("-o", callback=parse_option)] = None,
):
    typer.echo(options)

if __name__ == "__main__":
    typer.run(main)
$ python main.py -o a=1 -o b=2 -o passwordlogin=false
{'a': '1', 'b': '2', 'passwordlogin': 'false'}

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 YuriiMotov
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 #397 on September 18, 2025 13:34.