6.21.2.7 Printing a version string

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")

Note that "%prog" is expanded just like it is in usage. Apart from that, version can contain anything you like. 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:

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

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