This repository was archived by the owner on Jul 1, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom_cli.py
More file actions
executable file
·57 lines (43 loc) · 1.51 KB
/
random_cli.py
File metadata and controls
executable file
·57 lines (43 loc) · 1.51 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
#!/usr/bin/env python3
"""
a cli app that simply generates a random 64 character alphanumeric string.
"""
__version__ = "1.0.1"
import secrets
import string
import click
@click.command()
@click.option(
'--type',
default='alphanumeric',
help='alphanumeric (default), print, letters, lower, upper, hex')
@click.argument('length', required=False, default=64)
def random_cli(type, length):
if 'alphanumeric' == type:
click.echo(random_alphanumeric_string(length))
elif 'hex' == type:
click.echo(random_hex_string(length))
elif type in ['print', 'printable']:
click.echo(random_printable_string(length))
elif type in ['lower', 'lowercase']:
click.echo(random_string(length, string.ascii_lowercase))
elif type in ['upper', 'uppercase']:
click.echo(random_string(length, string.ascii_uppercase))
elif type in ['letter', 'letters', 'alpha']:
click.echo(random_string(length, string.ascii_letters))
def random_alphanumeric_string(len):
alphanumeric = string.ascii_letters + string.digits
return random_string(len, alphanumeric)
def random_hex_string(len):
return random_string(len, string.hexdigits)
def random_printable_string(len):
printable = string.ascii_letters + string.digits + string.punctuation
return random_string(len, printable)
def random_string(len, characters):
result = ''
while len > 0:
result += secrets.choice(characters)
len -= 1
return result
if __name__ == '__main__':
random_cli()