import click import itertools import random import pyperclip lowers = "abcdefghijklmnopqrstuvwxyz" uppers = lowers.upper() decimals = "0123456789" characters = list(lowers + uppers + decimals) @click.command() @click.option("--length", default=16, type=int) def get_random_password(length=16): """ generates a random password and copies it to the clipboard """ choice = [random.choice(characters) for i in range(length)] groups = [iter(choice)] * 4 grouped = ("".join(g) for g in itertools.zip_longest(*groups, fillvalue="")) password = "-".join(grouped) pyperclip.copy(password) click.echo("Copied to clipboard:", err=True) click.echo(password)