-
Notifications
You must be signed in to change notification settings - Fork 0
/
snippet_make.py
69 lines (57 loc) · 1.59 KB
/
snippet_make.py
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
#!/usr/bin/env python3
import json
def snippet_make(text_file, paste, title, prefix, desc):
if text_file:
with open(text_file, "rt") as f:
snippet_body = [line.rstrip() for line in f.readlines()]
if paste:
print("Enter/Paste your content, hit RETURN, followed by Ctrl-D (Ctrl-Z on Windows) to accept input.\n")
snippet_body = []
while True:
try:
line = input()
except EOFError:
break
snippet_body.append(line)
snippet_object = {
"prefix": prefix,
"body": snippet_body,
"description": "" if not desc else desc
}
snippet_json = f'"{title}": ' + json.dumps(snippet_object, indent = 4)
return snippet_json
if __name__ == "__main__":
from argparse import ArgumentParser
parser = ArgumentParser(description = "Make VS Code Snippet from text file or clipboard")
parser.add_argument(
"--text_file",
dest = "text_file",
help = "Load code from text file")
parser.add_argument(
"--paste",
action = "store_true",
dest = "paste",
help = "Paste code directly into console")
parser.add_argument(
"--title",
dest = "title",
help = "Name of snippet in JSON",
required = True)
parser.add_argument(
"--prefix",
dest = "prefix",
help = "Shortcut to call snippet",
required = True)
parser.add_argument(
"--desc",
dest = "desc",
type = str,
help = "Descriptive description of your snippet (optional)")
args = parser.parse_args()
text_file = args.text_file
paste = args.paste
title = args.title
prefix = args.prefix
desc = args.desc
snippet_json = snippet_make(text_file, paste, title, prefix, desc)
print(f"\n\n{snippet_json}\n\n")