|
| 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