bitcoin-cli.bash raw

   1  # bash programmable completion for bitcoin-cli(1)
   2  # Copyright (c) 2012-present The Bitcoin Core developers
   3  # Distributed under the MIT software license, see the accompanying
   4  # file COPYING or http://www.opensource.org/licenses/mit-license.php.
   5  
   6  # call $bitcoin-cli for RPC
   7  _bitcoin_rpc() {
   8      # determine already specified args necessary for RPC
   9      local rpcargs=()
  10      for i in ${COMP_LINE}; do
  11          case "$i" in
  12              -conf=*|-datadir=*|-regtest|-rpc*|-testnet|-testnet4)
  13                  rpcargs=( "${rpcargs[@]}" "$i" )
  14                  ;;
  15          esac
  16      done
  17      $bitcoin_cli "${rpcargs[@]}" "$@"
  18  }
  19  
  20  _bitcoin_cli() {
  21      local cur prev words=() cword
  22      local bitcoin_cli
  23  
  24      # save and use original argument to invoke bitcoin-cli for -help, help and RPC
  25      # as bitcoin-cli might not be in $PATH
  26      bitcoin_cli="$1"
  27  
  28      COMPREPLY=()
  29      _get_comp_words_by_ref -n = cur prev words cword
  30  
  31      if ((cword > 5)); then
  32          case ${words[cword-5]} in
  33              sendtoaddress)
  34                  COMPREPLY=( $( compgen -W "true false" -- "$cur" ) )
  35                  return 0
  36                  ;;
  37          esac
  38      fi
  39  
  40      if ((cword > 4)); then
  41          case ${words[cword-4]} in
  42              listtransactions|setban)
  43                  COMPREPLY=( $( compgen -W "true false" -- "$cur" ) )
  44                  return 0
  45                  ;;
  46              signrawtransactionwithkey|signrawtransactionwithwallet)
  47                  COMPREPLY=( $( compgen -W "ALL NONE SINGLE ALL|ANYONECANPAY NONE|ANYONECANPAY SINGLE|ANYONECANPAY" -- "$cur" ) )
  48                  return 0
  49                  ;;
  50          esac
  51      fi
  52  
  53      if ((cword > 3)); then
  54          case ${words[cword-3]} in
  55              getbalance|gettxout|listreceivedbyaddress|listsinceblock)
  56                  COMPREPLY=( $( compgen -W "true false" -- "$cur" ) )
  57                  return 0
  58                  ;;
  59          esac
  60      fi
  61  
  62      if ((cword > 2)); then
  63          case ${words[cword-2]} in
  64              addnode)
  65                  COMPREPLY=( $( compgen -W "add remove onetry" -- "$cur" ) )
  66                  return 0
  67                  ;;
  68              setban)
  69                  COMPREPLY=( $( compgen -W "add remove" -- "$cur" ) )
  70                  return 0
  71                  ;;
  72              fundrawtransaction|getblock|getblockheader|getmempoolancestors|getmempooldescendants|getrawtransaction|gettransaction|listreceivedbyaddress|sendrawtransaction)
  73                  COMPREPLY=( $( compgen -W "true false" -- "$cur" ) )
  74                  return 0
  75                  ;;
  76          esac
  77      fi
  78  
  79      case "$prev" in
  80          backupwallet)
  81              _filedir
  82              return 0
  83              ;;
  84          getaddednodeinfo|getrawmempool|lockunspent)
  85              COMPREPLY=( $( compgen -W "true false" -- "$cur" ) )
  86              return 0
  87              ;;
  88          getbalance|getnewaddress|listtransactions|sendmany)
  89              return 0
  90              ;;
  91      esac
  92  
  93      case "$cur" in
  94          -conf=*)
  95              cur="${cur#*=}"
  96              _filedir
  97              return 0
  98              ;;
  99          -datadir=*)
 100              cur="${cur#*=}"
 101              _filedir -d
 102              return 0
 103              ;;
 104          -*=*)	# prevent nonsense completions
 105              return 0
 106              ;;
 107          *)
 108              local helpopts commands
 109  
 110              # only parse -help if senseful
 111              if [[ -z "$cur" || "$cur" =~ ^- ]]; then
 112                  helpopts=$($bitcoin_cli -help 2>&1 | awk '$1 ~ /^-/ { sub(/=.*/, "="); print $1 }' )
 113              fi
 114  
 115              # only parse help if senseful
 116              if [[ -z "$cur" || "$cur" =~ ^[a-z] ]]; then
 117                  commands=$(_bitcoin_rpc help 2>/dev/null | awk '$1 ~ /^[a-z]/ { print $1; }')
 118              fi
 119  
 120              COMPREPLY=( $( compgen -W "$helpopts $commands" -- "$cur" ) )
 121  
 122              # Prevent space if an argument is desired
 123              if [[ $COMPREPLY == *= ]]; then
 124                  compopt -o nospace
 125              fi
 126              return 0
 127              ;;
 128      esac
 129  } &&
 130  complete -F _bitcoin_cli bitcoin-cli
 131  
 132  # Local variables:
 133  # mode: shell-script
 134  # sh-basic-offset: 4
 135  # sh-indent-comment: t
 136  # indent-tabs-mode: nil
 137  # End:
 138  # ex: ts=4 sw=4 et filetype=sh
 139