view python/password.py @ 921:02b31a76e2c4

get DEST from command line
author Jeff Hammel <k0scist@gmail.com>
date Sun, 02 Jun 2024 16:35:01 -0700
parents 5e05c41337e3
children
line wrap: on
line source

#!/usr/bin/env python

"""
password generator
"""

import argparse
import random
import string
import sys


def generate_password(population=string.ascii_letters+string.digits,
                      length=12):
    """
    returns a random password `length` characters long
    sampled from `population`
    """

    return ''.join(random.sample(population, length))


def main(args=sys.argv[1:]):

    # parse command line
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('-l', '--length', dest='length',
                        type=int, default=12,
                        help="length of password to generate [DEFAULT: %(default)s]")
    options = parser.parse_args(args)

    # print generated password
    print (generate_password(length=options.length))

if __name__ == '__main__':
    main()