Skip to content

Commit a8ed9b5

Browse files
committed
Can now generate main
1 parent 457c4f1 commit a8ed9b5

File tree

5 files changed

+153
-5
lines changed

5 files changed

+153
-5
lines changed

tools/bash_completion/generator.py

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@
33
from collections import defaultdict
44
import re
55
import subprocess
6+
import pystache
7+
import pprint
68

7-
commandRegex = r"^\s+(?P<command>\w+)\s+(?P<helptxt>[a-zA-Z ]*)$" # This one extracts single commands and the help txt
9+
commandRegex = r"^\s+(?P<command>(--)?\w+)\s+(?P<helptxt>[a-zA-Z ]*)$" # This one extracts single commands and the help txt
810
# 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
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
12+
13+
pp = pprint.PrettyPrinter(indent=2)
1014

1115
def getHelpTxt(command=None):
1216
if command:
@@ -18,6 +22,7 @@ def getHelpTxt(command=None):
1822

1923
def parseCommands():
2024
commands = defaultdict(defaultdict)
25+
commands["COMMAND"] = []
2126
helpTxt = getHelpTxt()
2227
#print helpTxt
2328
for line in helpTxt.split('\n'):
@@ -27,18 +32,73 @@ def parseCommands():
2732
commands[g["command"]]["helptxt"] = g["helptxt"]
2833
commands[g["command"]]["subcommands"] = []
2934

35+
# Subcommand mustache generation
36+
commands[g["command"]]["DDASH_COMMANDS"] = []
37+
commands[g["command"]]["DASH_COMMANDS"] = []
38+
commands[g["command"]]["COMMAND"] = g["command"]
39+
40+
# Main function generation
41+
commands["COMMAND"].append({"name": g["command"]})
42+
3043
for commandKey in commands:
3144
command = commands[commandKey]
3245
helpTxt = getHelpTxt(commandKey)
3346
for line in helpTxt.split('\n'):
3447
match = re.search(subcommandRegex, line)
3548
if match:
36-
commands[commandKey]["subcommands"].append(match.groupdict())
49+
commandMatch = match.groupdict()
50+
51+
# Clean up the subcommands
52+
command1 = commandMatch["command1"]
53+
command2 = commandMatch["command2"]
54+
55+
if command1:
56+
command1 = re.sub(",", "", command1)
57+
command1.strip()
58+
command1 = command1.split()[0]
59+
if command2:
60+
command2 = re.sub(",", "", command2)
61+
command2.strip()
62+
command2 = command2.split()[0]
63+
64+
# Not sure why the cleaning is even necessary, the regex looks correct
65+
commandMatch["command1"] = command1
66+
commandMatch["command2"] = command2
67+
68+
commands[commandKey]["subcommands"].append(commandMatch)
69+
70+
# Push format for mustache
71+
if command1 and '--' in command1:
72+
commands[commandKey]["DDASH_COMMANDS"].append({"name": command1})
73+
if command2 and '--' in command2:
74+
commands[commandKey]["DDASH_COMMANDS"].append({"name": command2})
3775

38-
print commands
76+
if command1:
77+
m = re.match("^-[a-zA-Z]{1,2}", command1)
78+
if m:
79+
commands[commandKey]["DASH_COMMANDS"].append({"name": command1})
80+
81+
if command2:
82+
m = re.match("^-[a-zA-Z]{1,2}", command2)
83+
if m:
84+
commands[commandKey]["DASH_COMMANDS"].append({"name": command2})
85+
86+
#print command1, command2
87+
88+
return commands
89+
90+
def generateMain(commands):
91+
tmplt = ""
92+
93+
with open("templates/mbed.tmplt") as fp:
94+
tmplt = fp.read()
95+
96+
print pystache.render(tmplt, commands)
3997

4098
if __name__ == '__main__':
41-
parseCommands()
99+
commands = parseCommands()
100+
101+
generateMain(commands)
42102

43103
# At this point we have a list of all the commands and sub commands
44104
# for each command create a Bash function
Binary file not shown.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Templates
2+
3+
Note these templates are inspired by the mustache syntax.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
__mbed_complete_{{COMMAND}}() {
2+
3+
local cur="${COMP_WORDS[COMP_CWORD]}"
4+
local prev="${COMP_WORDS[COMP_CWORD-1]}"
5+
case "$cur" in
6+
--*)
7+
__mbedcomp "
8+
{{#DDASH_COMMANDS}}
9+
{{name}}
10+
{{/DDASH_COMMANDS}}
11+
"
12+
13+
return
14+
;;
15+
-*)
16+
__mbedcomp "
17+
{{#DASH_COMMANDS}}
18+
{{name}}
19+
{{/DASH_COMMANDS}}
20+
"
21+
return
22+
;;
23+
esac
24+
25+
# Right now prev is not supported
26+
#case "$prev" in
27+
# toolchain|-G|--global|-v|--verbose|-vv|--very_verbose)
28+
# declare TOOLCHAINS=$(mbed target --supported | head -n 2 | tail -n 1 | tr '|' '\n' | sed -n '/^ Target/!p' | sed -n '/^ mbed/!p')
29+
# __mbedcomp "${TOOLCHAINS}"
30+
# return
31+
# ;;
32+
#esac
33+
34+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
_mbed () {
3+
local i=1 cmd prev
4+
5+
prev="${COMP_WORDS[COMP_CWORD-1]}"
6+
7+
# find the subcommand
8+
while [[ "$i" -lt "$COMP_CWORD" ]]
9+
do
10+
local s="${COMP_WORDS[i]}"
11+
case "$s" in
12+
--*)
13+
cmd="$s"
14+
break
15+
;;
16+
-*)
17+
;;
18+
*)
19+
cmd="$s"
20+
break
21+
;;
22+
esac
23+
24+
i="$((++i))"
25+
done
26+
27+
# Handle the main command completions
28+
if [[ "$i" -eq "$COMP_CWORD" ]]
29+
then
30+
local cmds="
31+
{{#COMMAND}}
32+
{{name}}
33+
{{/COMMAND}}
34+
"
35+
36+
__mbedcomp "${cmds}"
37+
fi
38+
39+
# Each subcommand has a completion function based on the parent
40+
case "$cmd" in
41+
{{#COMMAND}}
42+
{{name}}) __mbed_complete_{{name}} ;;
43+
{{/COMMAND}}
44+
*) ;;
45+
esac
46+
47+
}
48+
49+
complete -o bashdefault -o default -F _mbed mbed
50+
complete -o bashdefault -o default -F _mbed mbed-cli
51+

0 commit comments

Comments
 (0)