Skip to content

Commit 5e76742

Browse files
committed
basic extraction done. However, not sure why some of the regexes capture extra whitespace and strings. I tried setting it to be a pessimistic pony but it didn't work.
1 parent f26d633 commit 5e76742

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

tools/bash_completion/generator.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/env python
2+
3+
from collections import defaultdict
4+
import re
5+
import subprocess
6+
7+
commandRegex = r"^\s+(?P<command>\w+)\s+(?P<helptxt>[a-zA-Z ]*)$" # This one extracts single commands and the help txt
8+
# Why the hell do spaces get regexed in command1 ?
9+
subcommandRegex = r"^\s+(?P<command1>-+[a-zA-Z_\-]+(?P<modifier1>\s+[A-Z_\-]+){0,1})(?P<command2>,\s+-+[a-zA-Z_-]+(?P<modifier2>\s+[A-Z_-]+){0,1}){0,1}\s+(?P<helptxt>.*)$" # Gets just about everything
10+
11+
def getHelpTxt(command=None):
12+
if command:
13+
p = subprocess.Popen(["mbed", command, "-h"], stdout=subprocess.PIPE)
14+
else:
15+
p = subprocess.Popen(["mbed", "-h"], stdout=subprocess.PIPE)
16+
out, err = p.communicate()
17+
return out
18+
19+
if __name__ == '__main__':
20+
#commands = defaultdict(defaultdict(list))
21+
commands = defaultdict(defaultdict)
22+
helpTxt = getHelpTxt()
23+
#print helpTxt
24+
for line in helpTxt.split('\n'):
25+
match = re.search(commandRegex, line)
26+
if match:
27+
print "have match"
28+
g = match.groupdict()
29+
commands[g["command"]]["helptxt"] = g["helptxt"]
30+
commands[g["command"]]["subcommands"] = []
31+
32+
for commandKey in commands:
33+
command = commands[commandKey]
34+
helpTxt = getHelpTxt(commandKey)
35+
for line in helpTxt.split('\n'):
36+
match = re.search(subcommandRegex, line)
37+
if match:
38+
print match.groupdict()
39+
40+
# At this point we have a list of all the commands and sub commands
41+
# for each command create a Bash function
42+
# register each subcommand

0 commit comments

Comments
 (0)