How to reuse rostopic's completion function for completing a custom bash function?

混江龙づ霸主 提交于 2019-12-24 21:14:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!