General / custom option like -o customkey=customvalue in typer? #1324
-
        First Check
 Commit to Help
 Example CodeDescriptionI'm using my typer app to pass a json body to an API, the body should be formatted like this: where the module names are supplied by the user. How do I do this in typer? Operating SystemmacOS Operating System DetailsNo response Typer Version0.4.1 Python VersionPython 3.10.4 Additional ContextNo response  | 
  
Beta Was this translation helpful? Give feedback.
      
      
          Answered by
          
            YuriiMotov
          
      
      
        Sep 18, 2025 
      
    
    Replies: 2 comments
-
        
  | 
  
Beta Was this translation helpful? Give feedback.
                  
                    0 replies
                  
                
            -
| 
         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) | 
  
Beta Was this translation helpful? Give feedback.
                  
                    0 replies
                  
                
            
      Answer selected by
        YuriiMotov
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
        
    
You can parse options using callback and save parsed key-value items in global object that you can later use