1

I have a Python Cli application based on click framework ( https://click.palletsprojects.com/en/7.x/ ). It is released on Pypi.

How can I change my current Click CLI code so that user receive a message if there is a new release similar to "PIP UPGRADE WARNING" (after the CLI command is finished executing)

lets take a simple example. say the current version of my application is 10.1.2

import click
__version__ = '10.1.2'
@click.command()
@click.version_option(__version__, '-v', '--version', message='%(version)s')
@click.option('--name', default="John",
              help='The person to greet.')
def hello(name):
    """Simple program that greets NAME for a total of COUNT times."""
    click.echo('Hello %s!' % name)

if __name__ == '__main__':
    hello()

current version is 10.1.2

python hello.py --version

10.1.2

If there is a new pypi release and new version is 11.1.1, user should get this output.

python hello.py
Hello John!  

"You are using test version 10.1.2, however version 11.1.1 is available.
You should consider upgrading via the 'pip install --upgrade test' command."

Also, User should also have an ability to disable this feature if they want.

urawesome
  • 198
  • 1
  • 10

1 Answers1

1

I would make a request to PyPI's JSON API to determine the latest released version. If __version__ is not equal to this value, you would call click.echo() to print a warning.

Dustin Ingram
  • 20,502
  • 7
  • 59
  • 82
  • PyPI's JSON API returns all versions and I an extract latest from there. Is there an API thats gives me latest version directly? How to write click.echo so that it runs after all CLI commands and sub commands has finished? I looked at @cli.resultcallback() but I am not sure what is the best way to disable this from running if user doesn't want the warning. – urawesome Jul 23 '20 at 03:09