1

My programm can have the following format:

<command_name> <arguments> <options> -hosts <host1> <host2> ... <hostn> (<more_arguments>) ...

I am trying to set bash auto-completion, and based on debian-administration's article, I have done host completion like this :

(...) case "${prev}" in -hosts) _known_hosts_real -a "$cur" return 0 ;; (...) (...) 

Of course after writing -hosts bash is able to complete the name of a single host, but not multiple hosts. Is there a beautiful way to achieve this hack without completing hosts when we deal with other arguments than -hosts?

3
  • try to read multiple command line of being supported by auto completed , and rewrite your autocompletion. Commented Nov 19, 2014 at 17:17
  • Why is zsh tagged? Commented Nov 19, 2014 at 18:08
  • My bad, I thought it was kind of similar. Commented Nov 20, 2014 at 8:02

2 Answers 2

0

If you ignore the (<more_arguments>) case then you can just check whether -hosts is one of the earlier words:

(...) for ((i=1;i<COMP_CWORD;i++)); do if [ "-hosts" == "${COMP_WORDS[i]}" ]; then _known_hosts_real -a "$cur" return 0 fi done case "${prev}" in 
1
  • Thanks, I am using this hack for the moment. Some alternatives may exist, avoiding to check every word and considering the <more_arguments> case. Commented Nov 20, 2014 at 8:05
0

I'm not sure if it's possible to create such autocompletion rule for space-separated hosts values. But if you don't mind using comma-separated lists then:

_myhost () { local cur prev opts _get_comp_words_by_ref cur prev opts='-hosts' servers='name1 name2 name3' if [[ ${cur} == -* ]] ; then COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) else case "${prev}" in -hosts) if [[ "$cur" == *,* ]]; then local realcur prefix realcur=${cur##*,} prefix=${cur%,*} COMPREPLY=( $(compgen -W "${servers}" -P "${prefix}," -- ${realcur}) ) else COMPREPLY=( $(compgen -W "${servers}" -- ${cur}) ) fi ;; *) # do nothing ;; esac fi } complete -F _myhost myhost 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.