#!/usr/local/bin/python3

import argparse
import os
import string


def genpasswd(length, corpus):
    res = []

    for _ in range(length):
        r = None
        while r is None or r not in corpus:
          try:
            r = os.urandom(1).decode('ascii')
          except:
            pass
        res.append(r)

    return ''.join(res)


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('-l', '--length', type=int, default=32, help='Passwword length')
    parser.add_argument('-x', '--no-special-chars', default=False, action='store_true', help='Use special characters')
    args = parser.parse_args()

    if args.no_special_chars:
        corpus = string.ascii_letters + string.digits
    else:
        corpus = [s for s in string.printable if not s.isspace()]

    print(genpasswd(args.length, corpus), end='')
