Created
March 27, 2025 09:44
-
-
Save ScriptRaccoon/4c269113e4eca705ac13999df1755944 to your computer and use it in GitHub Desktop.
Password generator CLI (with Python)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Instructions (for macOS)
/usr/local/bin/pwgen
(no file extension)chmod +x /usr/local/bin/pwgen
Now you can type
pwgen
anywhere.Usage