-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
147 lines (122 loc) · 4.02 KB
/
cli.py
File metadata and controls
147 lines (122 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
"""
Password Generator - CLI Version
Author: RIFINOO
License: MIT License
Command-line interface for the password generator using argparse.
Provides a non-interactive way to generate passwords via command-line arguments.
Role in System:
- Entry point: Run with `python cli.py [options]`
- Uses: generator.py for core password generation
- Features: Command-line arguments, password strength analysis, clipboard integration
- Use case: Scripting, automation, quick password generation
"""
import argparse
import sys
import logging
from generator import PasswordGenerator
try:
import pyperclip
CLIPBOARD_AVAILABLE = True
except ImportError:
CLIPBOARD_AVAILABLE = False
print("Warning: pyperclip not installed. Clipboard functionality disabled.")
logger = logging.getLogger(__name__)
def main():
"""Main CLI entry point."""
parser = argparse.ArgumentParser(
description='Professional Password Generator - Generate secure passwords',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
python cli.py --length 20
python cli.py -l 16 --no-special
python cli.py -l 24 --copy
python cli.py -l 12 --check-strength
"""
)
parser.add_argument(
'-l', '--length',
type=int,
default=16,
help='Password length (default: 16, minimum: 4)'
)
parser.add_argument(
'--no-uppercase',
action='store_true',
help='Exclude uppercase letters'
)
parser.add_argument(
'--no-lowercase',
action='store_true',
help='Exclude lowercase letters'
)
parser.add_argument(
'--no-numbers',
action='store_true',
help='Exclude numbers'
)
parser.add_argument(
'--no-special',
action='store_true',
help='Exclude special characters'
)
parser.add_argument(
'-c', '--copy',
action='store_true',
help='Copy password to clipboard'
)
parser.add_argument(
'--check-strength',
action='store_true',
help='Display password strength analysis'
)
parser.add_argument(
'--entropy',
action='store_true',
help='Display password entropy'
)
args = parser.parse_args()
# Create generator instance
generator = PasswordGenerator()
# Generate password
password = generator.generate_password(
length=args.length,
include_uppercase=not args.no_uppercase,
include_lowercase=not args.no_lowercase,
include_numbers=not args.no_numbers,
include_special=not args.no_special
)
if not password:
print("Error: Failed to generate password. Check your parameters.", file=sys.stderr)
sys.exit(1)
# Display password
print(f"\n{'='*60}")
print(f"Generated Password: {password}")
print(f"{'='*60}\n")
# Display entropy if requested
if args.entropy or args.check_strength:
entropy = generator.calculate_entropy(password)
print(f"Entropy: {entropy} bits\n")
# Display strength analysis if requested
if args.check_strength:
strength_data = generator.check_strength(password)
print(f"Strength: {strength_data['strength']} (Score: {strength_data['score']}/6)")
print(f"Length: {strength_data['length']} characters")
print(f"Entropy: {strength_data['entropy']} bits")
print("\nFeedback:")
for item in strength_data['feedback']:
print(f" {item}")
print()
# Copy to clipboard if requested
if args.copy:
if CLIPBOARD_AVAILABLE:
try:
pyperclip.copy(password)
print("✓ Password copied to clipboard!")
except Exception as e:
print(f"Error copying to clipboard: {e}", file=sys.stderr)
else:
print("Warning: Clipboard functionality not available.", file=sys.stderr)
logger.info("Password generated via CLI")
if __name__ == '__main__':
main()