Adding a Version Option to Your App¶

Almost every app out there supports a --version option of some sort that provides the end user with version information. This is pretty important to include, so we’ve added an example below.

from cement.core.foundation import CementApp
from cement.core.controller import CementBaseController

VERSION = '0.9.1'

BANNER = """
My Awesome Application v%s
Copyright (c) 2014 John Doe Enterprises
""" % VERSION

class MyBaseController(CementBaseController):
    class Meta:
        label = 'base'
        description = 'MyApp Does Amazing Things'
        arguments = [
            (['-v', '--version'], dict(action='version', version=BANNER)),
            ]

class MyApp(CementApp):
    class Meta:
        label = 'myapp'
        base_controller = MyBaseController


with MyApp() as app:
    app.run()

This looks like:

$ python myapp.py --help
usage: myapp.py (sub-commands ...) [options ...] {arguments ...}

MyApp Does Amazing Things

optional arguments:
  -h, --help     show this help message and exit
  --debug        toggle debug output
  --quiet        suppress all output
  -v, --version  show program's version number and exit

$ python myapp.py --version
My Awesome Application v0.9.1
Copyright (c) 2014 John Doe Enterprises