| 71 | # ------------------------------------------ |
| 72 | |
| 73 | class BashHandler(Handler): |
| 74 | def __init__(self): |
| 75 | super(BashHandler, self).__init__("stoke.bash") |
| 76 | |
| 77 | def handle(self, command): |
| 78 | self.writeln("# Bash completion script (auto-generated; do not modify!)") |
| 79 | self.writeln("") |
| 80 | self.writeln("_stoke()") |
| 81 | self.writeln("{") |
| 82 | self.writeln(" local prev=${COMP_WORDS[COMP_CWORD-1]}") |
| 83 | self.writeln(" local cur=${COMP_WORDS[COMP_CWORD]}") |
| 84 | self.writeln(" COMPREPLY=( $(compgen -A file -- $cur) )") |
| 85 | |
| 86 | def aux(command, depth): |
| 87 | self.write_cmd(command, depth) |
| 88 | for sub in command.subcommands: |
| 89 | aux(sub, depth+1) |
| 90 | aux(command, 1) |
| 91 | |
| 92 | self.writeln("}") |
| 93 | self.writeln("") |
| 94 | self.writeln("complete -F _stoke stoke") |
| 95 | |
| 96 | def write_cmd(self, command, depth): |
| 97 | comps = [] |
| 98 | if command.has_subcommands(): |
| 99 | for sub in command.subcommands: |
| 100 | comps.append(sub.name) |
| 101 | else: |
| 102 | for arg in command.arguments: |
| 103 | for param in arg.params: |
| 104 | comps.append(param) |
| 105 | |
| 106 | self.writeln(" if [ $COMP_CWORD -eq " + str(depth) + " -a $prev == " + command.name + " ]") |
| 107 | self.writeln(" then") |
| 108 | self.writeln(" COMPREPLY=( $(compgen -W \"" + " ".join(comps) + "\" -- $cur) )") |
| 109 | self.writeln(" fi") |
| 110 | |
| 111 | def finish(self): |
| 112 | super(BashHandler, self).finish() |
| 113 | |
| 114 | |
| 115 | |