forked from RachelQChen/gua.help
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelp
65 lines (48 loc) · 1.21 KB
/
help
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
#!/usr/bin/python
import sys
import os
import json
__version__ = 1.0
__author__ = 'gua'
home_dir = os.path.expanduser('~')
db_file = '.gua.help.json'
db_path = os.path.join(home_dir, db_file)
help_dict = {}
def init():
if not os.path.exists(db_path):
return
with open(db_path, 'r') as f:
content = f.read()
try:
help_dict.update(json.loads(content))
except:
pass
def usage():
print('help usage:\n')
print('$help action')
print(' print help for action')
print('$help action "action arguments"')
print(' save help for action')
def print_help():
spoon = sys.argv[1]
not_found = ['help "{}" not found'.format(spoon)]
helps = help_dict.get(spoon, not_found)
for i, h in enumerate(helps):
print(h)
def save_help():
print('help saved')
spoon = sys.argv[1]
help_dict[spoon] = help_dict.get(spoon, []) + sys.argv[2:]
with open(db_path, 'w') as f:
f.write(json.dumps(help_dict, indent=2))
def main():
argc = len(sys.argv)
actions = {
2: print_help,
3: save_help,
}
action = actions.get(argc, usage)
action()
if __name__ == '__main__':
init()
main()