#!/usr/bin/python
#
# random-string - Generate random, printable strings.
#

import optparse
import random
import string


# Gargle the arguments

opt_parser = optparse.OptionParser()
opt_parser.add_option("-l", "--length",
                      help="Length of string produced",
                      action="store", type="int", dest="length",
                      default=32)

opt_parser.add_option("-r", "--randlength",
                      help="Randomize the string length",
                      action="store_true", dest="randlength")

opt_parser.add_option("-s", "--safe",
                      help="Produce a conservative, non-confusing string",
                      action="store_true", dest="safe")

(options, args) = opt_parser.parse_args()

if options.length < 1:
    opt_parser.error('Length must be greater than zero.')
    exit(1)


#
# Main Program
#

random.seed()

remaining = options.length
if options.randlength:
    half = int(remaining/2)
    remaining = half + int(random.random() * half)


charset = string.digits + string.ascii_letters
if not options.safe:
    charset += string.punctuation
charset_len = len(charset)


string = ""

while remaining:
    string += charset[int((random.random() * charset_len))]
    remaining -= 1

print string
