github.com/turingchain2020/turingchain@v1.1.21/cmd/cli/autocomplete/turingchain-cli.bash (about)

     1  #!/bin/bash
     2  
     3  set +x
     4  
     5  function _debug() {
     6      set -x
     7      print '\n debug'
     8      for var in "$@"; do
     9          print "\\t$var"
    10      done
    11      set +x
    12  }
    13  
    14  function _turingchain() {
    15  
    16      set +x
    17      # 这是套路
    18      local cur prev words cword
    19      _init_completion || return
    20      echo "$prev" "$cword" >/dev/null # ignore unused warning
    21      #_debug $cur $prev ${words[*]} $cword
    22  
    23      local commands
    24      #commands=($(${words[0]} 2>&1 | sed '/Available Commands/,/^$/p' -n | grep "  [a-z][a-z]*" -o | xargs))
    25      IFS=" " read -r -a commands <<<"$(${words[0]} 2>&1 | sed '/Available Commands/,/^$/p' -n | grep "  [a-z][_a-z]*" -o | xargs)"
    26      #_debug "commands " ${commands[@]}
    27  
    28      local command i
    29      for ((i = 0; i < ${#words[@]} - 1; i++)); do
    30          if [[ ${commands[*]} =~ ${words[i]} ]]; then
    31              command=${words[i]}
    32              break
    33          fi
    34      done
    35  
    36      #_debug $command $i
    37  
    38      if [ "$command" == "" ]; then
    39          mapfile -t COMPREPLY < <(compgen -W "${commands[*]}" -- "$cur")
    40          return 0
    41      fi
    42  
    43      local sub_commands sub_command sub_idx
    44      for ((i = 0; i < ${#commands[@]} - 1; i++)); do
    45          if [[ ${commands[i]} == "$command" ]]; then
    46              IFS=" " read -r -a sub_commands <<<"$(${words[0]} "$command" 2>&1 | sed '/Available Commands/,/^$/p' -n | grep "  [a-z][_2a-z]*" -o | xargs)"
    47              #_debug "sub_commands " ${sub_commands[@]}
    48  
    49              for ((sub_idx = 0; sub_idx < ${#words[@]} - 1; sub_idx++)); do
    50                  if [[ ${sub_commands[*]} =~ ${words[sub_idx]} ]]; then
    51                      sub_command=${words[sub_idx]}
    52                      break
    53                  fi
    54              done
    55          fi
    56      done
    57  
    58      if [[ $sub_command == "" ]]; then
    59          mapfile -t COMPREPLY < <(compgen -W "${sub_commands[*]}" -- "$cur")
    60          return 0
    61      fi
    62  
    63      #set -x
    64      for ((i = 0; i < ${#sub_commands[@]} - 1; i++)); do
    65          if [[ ${sub_commands[i]} == "$sub_command" ]]; then
    66              IFS=" " read -r -a sub_opts <<<"$(${words[0]} "$command" "$sub_command" 2>&1 | grep "^ " | grep -o ' \-[-a-zA-Z_]*' | sed 's/ //' | xargs)"
    67              mapfile -t COMPREPLY < <(compgen -W "${sub_opts[*]}" -- "$cur")
    68              return 0
    69          fi
    70      done
    71  
    72      mapfile -t COMPREPLY < <(compgen -W "${sub_commands[*]}" -- "$cur")
    73      return 0
    74  
    75  }
    76  
    77  # 用 _subcmd 补全 turingchain-cli
    78  # _subcmd 通过当前光标所在的输入参数过滤可选的子命令
    79  complete -F _turingchain turingchain-cli guodun