Replace dashes with underscores in bash completion

Property escaped_name is added to CompleteShellBase class to retrieve
name without dashes.

Change-Id: Ibc46a2f3a096f2b56826cdb623a37886c1c6b6f9
Closes-bug: #1374579
Co-authored-by: Neil Borle <neil.borle@emc.com>
This commit is contained in:
Maciej Kwiek
2015-12-10 13:57:32 +01:00
parent 85c0fd4f2a
commit e2604f19fb
2 changed files with 21 additions and 2 deletions

View File

@@ -57,9 +57,14 @@ class CompleteShellBase(object):
self.output.write(self.get_header())
self.output.write(" cmds='{0}'\n".format(cmdo))
for datum in data:
datum = (datum[0].replace('-', '_'), datum[1])
self.output.write(' cmds_{0}=\'{1}\'\n'.format(*datum))
self.output.write(self.get_trailer())
@property
def escaped_name(self):
return self.name.replace('-', '_')
class CompleteNoCode(CompleteShellBase):
"""completion with no code
@@ -81,7 +86,7 @@ class CompleteBash(CompleteShellBase):
super(CompleteBash, self).__init__(name, output)
def get_header(self):
return ('_' + self.name + """()
return ('_' + self.escaped_name + """()
{
local cur prev words
COMPREPLY=()
@@ -92,6 +97,8 @@ class CompleteBash(CompleteShellBase):
def get_trailer(self):
return ("""
dash=-
underscore=_
cmd=""
words[0]=""
completed="${cmds}"
@@ -106,6 +113,7 @@ class CompleteBash(CompleteShellBase):
proposed="${cmd}_${var}"
fi
local i="cmds_${proposed}"
i=${i//$dash/$underscore}
local comp="${!i}"
if [ -z "${comp}" ] ; then
break
@@ -127,7 +135,7 @@ class CompleteBash(CompleteShellBase):
fi
return 0
}
complete -F _""" + self.name + ' ' + self.name + '\n')
complete -F _""" + self.escaped_name + ' ' + self.name + '\n')
class CompleteCommand(command.Command):

View File

@@ -128,3 +128,14 @@ def test_complete_command_take_action():
assert " cmds='complete help'\n" in content
assert " cmds_complete='-h --help --name --shell'\n" in content
assert " cmds_help='-h --help'\n" in content
def test_complete_command_remove_dashes():
sot, app, cmd_mgr = given_complete_command()
parsed_args = mock.Mock()
parsed_args.name = "test-take"
parsed_args.shell = "bash"
content = app.stdout.content
assert 0 == sot.take_action(parsed_args)
assert "_test_take()\n" in content[0]
assert "complete -F _test_take test-take\n" in content[-1]