I'm the creator and maintainer of a PHP based command line application for developers. My command's syntax follows the norms of other PHP command line applications, and looks like this
pestle.phar some:command:name ...arguments and options here...
pestle.phar some:command ...arguments and options here...
That is -- colons are used in the command name. This presented problems when I tried using the stock autocomplete features of bash.
Thanks to some other answers I was able to determine this was bash treating the :
as a word separator, and that the bash-completion project (brew install bash-completion
) could help. I've been able to pull a script together that sort of works -- but I'm stuck on a few things and don't understand the system well enough to diagnose on my own. Also, my bash scripting skills are rusty.
Here's a sample of something that sort of works.
#File: /usr/local/etc/bash_completion.d/pestle-autocomplete.sh
#!/bin/bash
_commandList ()
{
echo "codecept:convert-selenium-id-for-codecept"
echo "magento2:check-templates"
echo "magento2:generate:acl"
#.. other comand names..
return 0;
}
_pestleAutocomplete ()
{
local cur
local all
_get_comp_words_by_ref -n : cur
all=$(_commandList)
COMPREPLY=( $(compgen -W "$all" $cur) )
__ltrim_colon_completions "$cur"
return 0
}
complete -F _pestleAutocomplete -o filenames pestle.phar
If I type $ pestle.phar mag
and then hit tab, the follow is output in my shell
$ pestle.phar magento2\:
That is, magento2:
auto completes, but the :
is escaped with a slash. If I complete a command name, this "works", but looks a little funny. So question 1 -- how do I get rid of that slash, and where's it coming from.
Second -- I don't understand what the __ltrim_colon_completions
command does. In the examples I've seen, its usually used something like
_mytool()
{
local cur
_get_comp_words_by_ref -n : cur
# my implementation here
__ltrim_colon_completions "$cur"
}
complete -F _mytool mytool
However, to my naive eyes -- that seems to be doing nothing. Its an extraneous call at the end of of the script using a local variable. I assume it changes some state in the bash-completion system, but again, my knowledge there is incomplete. I'm not sure if its related to my escaping problem.
If anyone here has a concrete answer, or can get me pointed in the right direction, I'd appreciate it. OS X 10.11, El Cap, stock bash -- if it matters.