-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbacon
executable file
·60 lines (48 loc) · 1.36 KB
/
bacon
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
#!/usr/bin/env python
import re
import sys
import getopt
def main(argv):
if not argv:
print("Syntax: bacon [option] <value>")
print("\nOptions:")
print("\t-k --kb: output in kb")
print("\t-m --mb: output in mb")
print("\t-g --gb: output in gb")
print("\t-t --tb: output in tb")
print("\nValue should be a numeric string")
print("optionally followed by k, m, g to specify unit.")
print("If no unit is given bytes is assumed.")
sys.exit(1)
try:
opts, args = getopt.getopt(argv,"kmgt", ["kb","mb","gb", "tb"])
except getopt.GetoptError:
print("u crazy?")
sys.exit(2)
to = "g"
for opt, arg in opts:
if opt in ("-k", "--kb"):
to = "k"
elif opt in ("-m", "--mb"):
to = "m"
elif opt in ("-g", "--gb"):
to = "g"
elif opt in ("-t", "--tb"):
to = "t"
bytes = args[0]
a = {'k' : 1, 'm': 2, 'g' : 3, 't' : 4}
b = a[to]
m = re.match(r"^(\d+)([bkmgtp]?)$", bytes)
if m is None:
print("Invalid input...")
sys.exit()
bytes = m.group(1)
if m.group(2):
b -= a[m.group(2)]
r = float(bytes)
for i in range(b):
r = r / 1024
output = str(int(r)) + to + "b"
print(output)
if __name__ == "__main__":
main(sys.argv[1:])