Skip to content

Instantly share code, notes, and snippets.

@ScriptRaccoon
Created March 27, 2025 09:44
Show Gist options
  • Save ScriptRaccoon/4c269113e4eca705ac13999df1755944 to your computer and use it in GitHub Desktop.
Save ScriptRaccoon/4c269113e4eca705ac13999df1755944 to your computer and use it in GitHub Desktop.
Password generator CLI (with Python)
#!/usr/bin/env python3
# This Python script can quickly generate passwords.
# Simply type 'pwgen' into the terminal.
# Options:
# -l or --length to set the length. Default is 20.
# -a or --alphanumeric to generate an alphanumeric password: no special characters.
import argparse
import random
import string
import pyperclip
def generate_password(length: int = 20, alphanumeric: bool = False) -> str:
characters = (
string.ascii_letters + string.digits
if alphanumeric
else string.ascii_letters + string.digits + string.punctuation
)
return "".join(random.choices(characters, k=length))
def main():
parser = argparse.ArgumentParser(description="Generate a random password.")
parser.add_argument(
"-l",
"--length",
type=int,
default=20,
help="Length of the password (default: 20)",
)
parser.add_argument(
"-a",
"--alphanumeric",
action="store_true",
help="Generate an alphanumeric password (no special characters)",
)
args = parser.parse_args()
password = generate_password(args.length, args.alphanumeric)
print(password)
pyperclip.copy(password)
print("Password copied to clipboard!")
if __name__ == "__main__":
main()
@ScriptRaccoon
Copy link
Author

ScriptRaccoon commented Mar 27, 2025

Instructions (for macOS)

  1. Move the script file to /usr/local/bin/pwgen (no file extension)
  2. Make it executable with chmod +x /usr/local/bin/pwgen

Now you can type pwgen anywhere.

Usage

pwgen
pwgen -l 30 -a
pwgen --help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment