question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[FEATURE] Add easy --version

See original GitHub issue

Is 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:closed
  • Created 4 years ago
  • Reactions:7
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

11reactions
dbantycommented, May 18, 2020

@kesavkolla yes you just add a callback function for your app and then use the --version parameter 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:

import typer

app = typer.Typer()

def version_callback(value: bool):
    if value:
        typer.echo(f"Awesome CLI Version: {__version__}")
        raise typer.Exit()

@app.callback()
def main(
    version: bool = typer.Option(None, "--version", callback=version_callback, is_eager=True),
):
    # Do other global stuff, handle other global options here
    return

5reactions
shelpercommented, May 27, 2022

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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

iOS 16 - New Features - Apple
See all the latest features, enhancements, app updates, and more in iOS 16 for ... Easily add your previous keys on your new...
Read more >
Media Feature Pack list for Windows N editions
Media Feature Packs by Windows version · Windows 10 N: Select the Start button, then select Settings > Apps > Apps & features...
Read more >
Windows 11 22H2 new features and changes - Pureinfotech
In addition, this new version adds another entry in the right-click ... On Windows 11, the “do not disturb” feature makes it easy...
Read more >
Features and APIs Overview | Android Developers
Starting in Android 13, the system displays a standard visual confirmation when content is added to the clipboard. The new confirmation does the...
Read more >
Build custom maps the easy way with multiple map layers in ...
In particular, the new multiple marks layers feature lets you add an unlimited number of layers to the map. This means you can...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found