github.com/osrg/gobgp/v3@v3.30.0/tools/completion/gobgp-completion.bash (about)

     1  # bash completion for gobgp                                -*- shell-script -*-
     2  
     3  . `dirname $BASH_SOURCE`/gobgp-static-completion.bash
     4  . `dirname $BASH_SOURCE`/gobgp-dynamic-completion.bash
     5  
     6  __debug()
     7  {
     8      if [[ -n ${BASH_COMP_DEBUG_FILE} ]]; then
     9          echo "$*" >> "${BASH_COMP_DEBUG_FILE}"
    10      fi
    11  }
    12  
    13  # Homebrew on Macs have version 1.3 of bash-completion which doesn't include
    14  # _init_completion. This is a very minimal version of that function.
    15  __my_init_completion()
    16  {
    17      COMPREPLY=()
    18      _get_comp_words_by_ref cur prev words cword
    19  }
    20  
    21  __index_of_word()
    22  {
    23      local w word=$1
    24      shift
    25      index=0
    26      for w in "$@"; do
    27          [[ $w = "$word" ]] && return
    28          index=$((index+1))
    29      done
    30      index=-1
    31  }
    32  
    33  __contains_word()
    34  {
    35      local w word=$1; shift
    36      for w in "$@"; do
    37          [[ $w = "$word" ]] && return
    38      done
    39      return 1
    40  }
    41  
    42  __handle_reply()
    43  {
    44      __debug "${FUNCNAME}"
    45      case $cur in
    46          -*)
    47              if [[ $(type -t compopt) = "builtin" ]]; then
    48                  compopt -o nospace
    49              fi
    50              local allflags
    51              if [ ${#must_have_one_flag[@]} -ne 0 ]; then
    52                  allflags=("${must_have_one_flag[@]}")
    53              else
    54                  allflags=("${flags[*]} ${two_word_flags[*]}")
    55              fi
    56              COMPREPLY=( $(compgen -W "${allflags[*]}" -- "$cur") )
    57              if [[ $(type -t compopt) = "builtin" ]]; then
    58                  [[ $COMPREPLY == *= ]] || compopt +o nospace
    59              fi
    60              return 0;
    61              ;;
    62      esac
    63  
    64      # check if we are handling a flag with special work handling
    65      local index
    66      __index_of_word "${prev}" "${flags_with_completion[@]}"
    67      if [[ ${index} -ge 0 ]]; then
    68          ${flags_completion[${index}]}
    69          return
    70      fi
    71  
    72      # we are parsing a flag and don't have a special handler, no completion
    73      if [[ ${cur} != "${words[cword]}" ]]; then
    74          return
    75      fi
    76  
    77      local completions
    78      if [[ ${#must_have_one_flag[@]} -ne 0 ]]; then
    79          completions=("${must_have_one_flag[@]}")
    80      elif [[ ${#must_have_one_noun[@]} -ne 0 ]]; then
    81          completions=("${must_have_one_noun[@]}")
    82      else
    83          completions=("${commands[@]}")
    84      fi
    85      COMPREPLY=( $(compgen -W "${completions[*]}" -- "$cur") )
    86  
    87      if [[ ${#COMPREPLY[@]} -eq 0 ]]; then
    88          declare -F __custom_func >/dev/null && __custom_func
    89      fi
    90  }
    91  
    92  # The arguments should be in the form "ext1|ext2|extn"
    93  __handle_filename_extension_flag()
    94  {
    95      local ext="$1"
    96      _filedir "@(${ext})"
    97  }
    98  
    99  __handle_subdirs_in_dir_flag()
   100  {
   101      local dir="$1"
   102      pushd "${dir}" >/dev/null 2>&1 && _filedir -d && popd >/dev/null 2>&1
   103  }
   104  
   105  __handle_flag()
   106  {
   107      __debug "${FUNCNAME}: c is $c words[c] is ${words[c]}"
   108  
   109      # if a command required a flag, and we found it, unset must_have_one_flag()
   110      local flagname=${words[c]}
   111      # if the word contained an =
   112      if [[ ${words[c]} == *"="* ]]; then
   113          flagname=${flagname%=*} # strip everything after the =
   114          flagname="${flagname}=" # but put the = back
   115      fi
   116      __debug "${FUNCNAME}: looking for ${flagname}"
   117      if __contains_word "${flagname}" "${must_have_one_flag[@]}"; then
   118          must_have_one_flag=()
   119      fi
   120  
   121      # skip the argument to a two word flag
   122      if __contains_word "${words[c]}" "${two_word_flags[@]}"; then
   123          c=$((c+1))
   124          # if we are looking for a flags value, don't show commands
   125          if [[ $c -eq $cword ]]; then
   126              commands=()
   127          fi
   128      fi
   129  
   130      if [ ${words[(c-1)]} == "-u" ]; then
   131          url="-u ${words[(c)]}"
   132      fi
   133      if [ ${words[(c-1)]} == "-p" ]; then
   134          port="-p ${words[(c)]}"
   135      fi
   136      # skip the flag itself
   137      c=$((c+1))
   138  
   139  }
   140  
   141  __handle_noun()
   142  {
   143      __debug "${FUNCNAME}: c is $c words[c] is ${words[c]}"
   144  
   145      if __contains_word "${words[c]}" "${must_have_one_noun[@]}"; then
   146          must_have_one_noun=()
   147      fi
   148  
   149      nouns+=("${words[c]}")
   150      c=$((c+1))
   151  }
   152  
   153  __handle_command()
   154  {
   155      __debug "${FUNCNAME}: c is $c words[c] is ${words[c]}"
   156      # echo "${FUNCNAME}: c is $c words[c] is ${words[c]} searched is ${searched} through ${through}"
   157      next_command=""
   158      through="False"
   159      __handle_gobgp_command
   160      searched="False"
   161      if [[ ${through} == "False" ]]; then
   162          if [[ -n ${last_command} ]]; then
   163              next_command="_${last_command}_${words[c]}"
   164          else
   165              next_command="_${words[c]}"
   166          fi
   167      fi
   168  
   169      c=$((c+1))
   170      __debug "${FUNCNAME}: looking for ${next_command}"
   171      # echo "${FUNCNAME}: looking for ${next_command} searched is ${searched} through ${through}"
   172      declare -F $next_command >/dev/null && $next_command
   173  
   174      if [[ ${req_faild} == "True" ]]; then
   175          next_command="__gobgp_null"
   176      fi
   177  }
   178  
   179  __handle_word()
   180  {
   181      if [[ $c -ge $cword ]]; then
   182          __handle_reply
   183          return
   184      fi
   185      __debug "${FUNCNAME}: c is $c words[c] is ${words[c]}"
   186      if [[ "${words[c]}" == -* ]]; then
   187          __handle_flag
   188      elif __contains_word "${words[c]}" "${commands[@]}"; then
   189          __handle_command
   190      else
   191          __handle_noun
   192      fi
   193      __handle_word
   194  }
   195  
   196  __start_gobgp()
   197  {
   198      local cur prev words cword
   199      if declare -F _init_completion >/dev/null 2>&1; then
   200          _init_completion -s || return
   201      else
   202          __my_init_completion || return
   203      fi
   204  
   205      local c=0
   206      local flags=()
   207      local two_word_flags=()
   208      local flags_with_completion=()
   209      local flags_completion=()
   210      local commands=("gobgp")
   211      local must_have_one_flag=()
   212      local must_have_one_noun=()
   213      local last_command
   214      local nouns=()
   215  
   216      req_faild="False"
   217      searched="False"
   218      through="False"
   219      __handle_word
   220  }
   221  
   222  if [[ $(type -t compopt) = "builtin" ]]; then
   223      complete -F __start_gobgp gobgp
   224  else
   225      complete -o nospace -F __start_gobgp gobgp
   226  fi
   227  
   228  # ex: ts=4 sw=4 et filetype=sh