Skip to content
This repository was archived by the owner on Nov 29, 2023. It is now read-only.

Commit c8e7ed2

Browse files
author
gongysh
committed
Generate bash_completion string so that we can use bash completion.
Bug #1063500 To install, copy tools/quantum.bash_completion to /etc/bash_completion.d/quantum Change-Id: I0afff3967c63111854455226fc90092f5bc7845a
1 parent 3e19fc0 commit c8e7ed2

2 files changed

Lines changed: 48 additions & 0 deletions

File tree

‎quantumclient/shell.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,24 @@ def build_option_parser(self, description, version):
278278

279279
return parser
280280

281+
def _bash_completion(self):
282+
"""
283+
Prints all of the commands and options to stdout so that the
284+
quantum's bash-completion script doesn't have to hard code them.
285+
"""
286+
commands = set()
287+
options = set()
288+
for option, _action in self.parser._option_string_actions.items():
289+
options.add(option)
290+
for command_name, command in self.command_manager:
291+
commands.add(command_name)
292+
cmd_factory = command.load()
293+
cmd = cmd_factory(self, None)
294+
cmd_parser = cmd.get_parser('')
295+
for option, _action in cmd_parser._option_string_actions.items():
296+
options.add(option)
297+
print ' '.join(commands | options)
298+
281299
def run(self, argv):
282300
"""Equivalent to the main program for the application.
283301
@@ -289,6 +307,9 @@ def run(self, argv):
289307
command_pos = -1
290308
help_pos = -1
291309
for arg in argv:
310+
if arg == 'bash-completion':
311+
self._bash_completion()
312+
return 0
292313
if arg in COMMANDS[self.api_version]:
293314
if command_pos == -1:
294315
command_pos = index

‎tools/quantum.bash_completion‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
_quantum_opts="" # lazy init
2+
_quantum_flags="" # lazy init
3+
_quantum_opts_exp="" # lazy init
4+
_quantum()
5+
{
6+
local cur prev nbc cflags
7+
COMPREPLY=()
8+
cur="${COMP_WORDS[COMP_CWORD]}"
9+
prev="${COMP_WORDS[COMP_CWORD-1]}"
10+
11+
if [ "x$_quantum_opts" == "x" ] ; then
12+
nbc="`quantum bash-completion | sed -e "s/\s-h\s/\s/"`"
13+
_quantum_opts="`echo "$nbc" | sed -e "s/--[a-z0-9_-]*//g" -e "s/\s\s*/ /g"`"
14+
_quantum_flags="`echo " $nbc" | sed -e "s/ [^-][^-][a-z0-9_-]*//g" -e "s/\s\s*/ /g"`"
15+
_quantum_opts_exp="`echo "$_quantum_opts" | sed -e "s/\s/|/g"`"
16+
fi
17+
18+
if [[ " ${COMP_WORDS[@]} " =~ " "($_quantum_opts_exp)" " && "$prev" != "help" ]] ; then
19+
COMPLETION_CACHE=~/.quantumclient/*/*-cache
20+
cflags="$_quantum_flags "$(cat $COMPLETION_CACHE 2> /dev/null | tr '\n' ' ')
21+
COMPREPLY=($(compgen -W "${cflags}" -- ${cur}))
22+
else
23+
COMPREPLY=($(compgen -W "${_quantum_opts}" -- ${cur}))
24+
fi
25+
return 0
26+
}
27+
complete -F _quantum quantum

0 commit comments

Comments
 (0)