[FEATURE] Add easy --version
See original GitHub issueIs your feature request related to a problem
I’d like a way to do the standard my-command --version to exit an print the version number of the application. Click offers a version_option decorator that you can put on the entrypoint of your application to achieve this relatively easily.
The solution you would like
The ability to pass in a version string to an application and have the option added automatically. I’d then end up doing something like this:
from importlib.metadata import version
app = typer.Typer(version=version(__package__))
Though if it’s possible to attempt to extract the package version automatically without having to pass it in, that would be even better.
Describe alternatives you’ve considered
I attempted to just add a version option in my callback like below, but I can’t seem to call this without passing in a command.
@app.callback()
def cli(display_version: bool = typer.Option(False, "--version")) -> None:
if display_version:
print(f"Version: {version(__package__)}")
raise typer.Exit()
However if I just try my-command --version it gives me an error saying I need to pass a command. It would be nice to be able to add custom top level options like --help which are allowed to run. Then users could implement things like --version themselves.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:7
- Comments:10 (1 by maintainers)

Top Related StackOverflow Question
@kesavkolla yes you just add a callback function for your app and then use the
--versionparameter in that callback.Docs on callback: https://typer.tiangolo.com/tutorial/commands/callback/
Docs on
--version: https://typer.tiangolo.com/tutorial/options/version/Example combining the two:
would it be even greater if the version value is read from poetry’s pyproject.toml file?
maybe something like a typer plugin for poetry, so it generates this version CLI option automatically?