Skip to main content

This Python script will output a randomly generated UUID.

#!/usr/bin/env python

from optparse import OptionParser
from sys import stdout
from uuid import uuid4

"""Generate a random UUID."""


def uuid_str(stripdashes=False):
    if stripdashes:
        return uuid4().hex
    else:
        return str(uuid4())


def main():
    parser = OptionParser(
        description='Generate a random UUID.',
        prog='uuidgen',
        usage='%prog [options]')
    parser.add_option(
        '-s', '--stripdashes', action='store_true', default=False,
        dest='stripdashes',
        help='strip dashes from UUID')
    options, arguments = parser.parse_args()

    stdout.write(uuid_str(options.stripdashes) + '\n')


if __name__ == '__main__':
    main()