pythonpython-3.xcommand-line-interfacetyper

How do I get typer to accept the short `-h` as well as the long `--help` to output help text?


Out of the box, Typer CLIs only recognize the long help option --help to display the help text.

I would like to also accept the short option -h but I can't figure out how. I've searched the docs to no avail.

Do I need to alias -h to --help and if so, how do I do that?


Solution

  • The key is to use context_settings={"help_option_names": ["-h", "--help"]})

    As suggested by @jvx8ss in the comments, one needs to convert a typer.run app to one using @app.command() decorators.

    Here is a minimal working example:

    import typer
    
    app = typer.Typer(context_settings={"help_option_names": ["-h", "--help"]})
    
    
    @app.command()
    def main(name: str):
        print(f"Hello {name}")
    
    
    if __name__ == "__main__":
        app()