bash completion of makefile target

前端 未结 6 2094
小蘑菇
小蘑菇 2021-01-31 01:59

Suppose I have a simple makefile like:

hello:
   echo \"hello world\"

bye:
   echo \"bye bye\"

Then in bash I want something like:

6条回答
  •  渐次进展
    2021-01-31 02:35

    Here is a completion script that looks at the .PHONY: declaration.

    _make_phony_words() {
      local opt_revert
    
      if [ -n "${BASH_VERSION:-}" ]; then
        shopt -q nullglob || {
          opt_revert=1 ; shopt -s nullglob ;
        }
    
      elif [ -n "${ZSH_VERSION:-}" ]; then
        [[ -o nullglob ]] || {
          opt_revert=1 ; setopt nullglob
        }
      fi
    
      for f in ./?akefile ./*.make ; do
        sed -nEe '/^.PHONY/ { s/^.PHONY:[ ]?// ; p ; } ' "$f" | tr ' ' $'\n' | sort -u
      done
    
      if [ -n "$opt_revert" ]; then
    
        [ -n "${ZSH_VERSION:-}" ] && unsetopt nullglob
        [ -n "${BASH_VERSION:-}" ] && shopt -u nullglob
      fi
      unset opt_revert
    
    }
    
    _make_phony_complete() {
      local cur="${COMP_WORDS[COMP_CWORD]}"
    
      COMPREPLY+=( $(compgen -W "$( _make_phony_words )" -- ${cur}) )
    
    }
    complete -F _make_phony_complete make
    

提交回复
热议问题