6.20.2.5 Print a version number

Similar to the brief usage string, optparse can also print a version string for your program. You have to supply the string, as the version argument to OptionParser:

parser = OptionParser(usage="%prog [-f] [-q]", version="%prog 1.0")

version can contain anything you like; %prog is expanded in version just as with usage. When you supply it, optparse automatically adds a --version option to your parser. If it encounters this option on the command line, it expands your version string (by replacing %prog), prints it to stdout, and exits.

For example, if your script is called /usr/bin/foo, a user might do:

$ /usr/bin/foo --version
foo 1.0

See About this document... for information on suggesting changes.