1
1
#!/usr/bin/env python
2
+ # Michael Bartling ([email protected] )
2
3
3
4
from collections import defaultdict
5
+ import pystache
4
6
import re
5
7
import subprocess
6
- import pystache
7
- import pprint
8
8
9
- commandRegex = r"^\s+(?P<command>(--)?\w+)\s+(?P<helptxt>[a-zA-Z ]*)$" # This one extracts single commands and the help txt
9
+ # Top level --version is a pain to deal with so ignoring for now
10
+ # This one extracts single commands and the help txt
11
+ commandRegex = r"^\s+(?P<command>\w+)\s+(?P<helptxt>[a-zA-Z ]*)$"
12
+
10
13
# Why the hell do spaces get regexed in command1 ?
11
- subcommandRegex = r"^\s+(?P<command1>-+[a-zA-Z_\-]+(?P<modifier1>\s+[A-Z_\-]+)?)(?P<command2>,\s+-+[a-zA-Z_-]+(?P<modifier2>\s+[A-Z_-]+)?)?\s+(?P<helptxt>.*)$" # Gets just about everything
14
+ subcommandRegex = r"^\s+(?P<command1>-+[a-zA-Z_\-]+(?P<modifier1>\s+[A-Z_\-]+)?)" \
15
+ r"(?P<command2>,\s+-+[a-zA-Z_-]+(?P<modifier2>\s+[A-Z_-]+)?)?" \
16
+ r"\s+(?P<helptxt>.*)$"
12
17
13
- pp = pprint .PrettyPrinter (indent = 2 )
14
18
15
19
def getHelpTxt (command = None ):
16
20
if command :
@@ -20,11 +24,12 @@ def getHelpTxt(command=None):
20
24
out , err = p .communicate ()
21
25
return out
22
26
27
+
23
28
def parseCommands ():
24
29
commands = defaultdict (defaultdict )
25
30
commands ["COMMAND" ] = []
26
31
helpTxt = getHelpTxt ()
27
- #print helpTxt
32
+ # print helpTxt
28
33
for line in helpTxt .split ('\n ' ):
29
34
match = re .search (commandRegex , line )
30
35
if match :
@@ -41,6 +46,10 @@ def parseCommands():
41
46
commands ["COMMAND" ].append ({"name" : g ["command" ]})
42
47
43
48
for commandKey in commands :
49
+ # Skip
50
+ if commandKey == "COMMAND" :
51
+ continue
52
+
44
53
command = commands [commandKey ]
45
54
helpTxt = getHelpTxt (commandKey )
46
55
for line in helpTxt .split ('\n ' ):
@@ -87,18 +96,61 @@ def parseCommands():
87
96
88
97
return commands
89
98
99
+
90
100
def generateMain (commands ):
91
101
tmplt = ""
92
102
103
+ txt = []
104
+
93
105
with open ("templates/mbed.tmplt" ) as fp :
94
106
tmplt = fp .read ()
95
107
96
- print pystache .render (tmplt , commands )
108
+ txt .append (pystache .render (tmplt , commands ))
109
+
110
+ return txt
111
+
112
+
113
+ def generateCompleters (commands ):
114
+ tmplt = ""
115
+ txt = []
116
+
117
+ with open ("templates/command.tmplt" ) as fp :
118
+ tmplt = fp .read ()
119
+
120
+ for commandKey in commands :
121
+ txt .append (pystache .render (tmplt , commands [commandKey ]))
122
+
123
+ # if need to add hacks add them here
124
+
125
+ return txt
126
+
127
+
128
+ def generateBoilerPlate (commands ):
129
+ tmplt = ""
130
+ txt = []
131
+
132
+ with open ("templates/boilerplate.tmplt" ) as fp :
133
+ txt .append (fp .read ())
134
+
135
+ return txt
136
+
137
+
138
+ def generateScript (commands ):
139
+ txt = []
140
+
141
+ txt .extend (generateBoilerPlate (commands ))
142
+ txt .extend (generateCompleters (commands ))
143
+ txt .extend (generateMain (commands ))
144
+
145
+ with open ("mbed-completion" , "w" ) as fp :
146
+ for x in txt :
147
+ fp .write ("%s\n " % x )
148
+
97
149
98
150
if __name__ == '__main__' :
99
151
commands = parseCommands ()
100
152
101
- generateMain (commands )
153
+ generateScript (commands )
102
154
103
155
# At this point we have a list of all the commands and sub commands
104
156
# for each command create a Bash function
0 commit comments