问题
Defined bash function rte():
$ function rte(){ rostopic echo $@ ; };
and attempted to use rostopic's completion function
$ complete -p rostopic
complete -F _roscomplete_rostopic rostopic
via the command:
$ complete -F _roscomplete_rostopic rte
The above setting can be verified as follows:
$ complete -p rte
complete -F _roscomplete_rostopic rte
However, rte <partial input><tab> does not offer completions.
Question: How to get rte() to use rostopic's completion? I guess when rte()'s completion calls rostopic's completion, the context echo needs to be provided to rostopic's completion (i.e., COMP_WORDS needs to contain echo).
回答1:
Figured out a solution -- now have the following in .bashrc:
function rte(){ rostopic echo "$@" ; };
complete -F _mycomplete_ rte
function _mycomplete_()
{
local fragment=${COMP_WORDS[COMP_CWORD]}
COMP_CWORD=2
COMP_WORDS[0]="rostopic"
COMP_WORDS[1]="echo"
COMP_WORDS[2]=$fragment
COMP_LINE="rostopic echo $fragment"
_roscomplete_rostopic;
}
来源:https://stackoverflow.com/questions/48919499/how-to-reuse-rostopics-completion-function-for-completing-a-custom-bash-functio