|
| 1 | +# bash programmable completion for bitcoind(1) |
| 2 | +# Copyright (c) 2012 Christian von Roques <[email protected]> |
| 3 | +# Distributed under the MIT/X11 software license, see the accompanying |
| 4 | +# file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 5 | + |
| 6 | +have bitcoind && { |
| 7 | + |
| 8 | +# call $bitcoind for RPC |
| 9 | +_bitcoin_rpc() { |
| 10 | + # determine already specified args necessary for RPC |
| 11 | + local rpcargs=() |
| 12 | + for i in ${COMP_LINE}; do |
| 13 | + case "$i" in |
| 14 | + -conf=*|-proxy*|-rpc*) |
| 15 | + rpcargs=( "${rpcargs[@]}" "$i" ) |
| 16 | + ;; |
| 17 | + esac |
| 18 | + done |
| 19 | + $bitcoind "${rpcargs[@]}" "$@" |
| 20 | +} |
| 21 | + |
| 22 | +# Add bitcoin accounts to COMPREPLY |
| 23 | +_bitcoin_accounts() { |
| 24 | + local accounts |
| 25 | + accounts=$(_bitcoin_rpc listaccounts | awk '/".*"/ { a=$1; gsub(/"/, "", a); print a}') |
| 26 | + COMPREPLY=( "${COMPREPLY[@]}" $( compgen -W "$accounts" -- "$cur" ) ) |
| 27 | +} |
| 28 | + |
| 29 | +_bitcoind() { |
| 30 | + local cur prev words=() cword |
| 31 | + local bitcoind |
| 32 | + |
| 33 | + # save and use original argument to invoke bitcoind |
| 34 | + # bitcoind might not be in $PATH |
| 35 | + bitcoind="$1" |
| 36 | + |
| 37 | + COMPREPLY=() |
| 38 | + _get_comp_words_by_ref -n = cur prev words cword |
| 39 | + |
| 40 | + if ((cword > 2)); then |
| 41 | + case ${words[cword-2]} in |
| 42 | + listreceivedbyaccount|listreceivedbyaddress) |
| 43 | + COMPREPLY=( $( compgen -W "true false" -- "$cur" ) ) |
| 44 | + return 0 |
| 45 | + ;; |
| 46 | + move|setaccount) |
| 47 | + _bitcoin_accounts |
| 48 | + return 0 |
| 49 | + ;; |
| 50 | + esac |
| 51 | + fi |
| 52 | + |
| 53 | + case "$prev" in |
| 54 | + backupwallet) |
| 55 | + _filedir |
| 56 | + return 0 |
| 57 | + ;; |
| 58 | + setgenerate) |
| 59 | + COMPREPLY=( $( compgen -W "true false" -- "$cur" ) ) |
| 60 | + return 0 |
| 61 | + ;; |
| 62 | + getaccountaddress|getaddressesbyaccount|getbalance|getnewaddress|getreceivedbyaccount|listtransactions|move|sendfrom|sendmany) |
| 63 | + _bitcoin_accounts |
| 64 | + return 0 |
| 65 | + ;; |
| 66 | + esac |
| 67 | + |
| 68 | + case "$cur" in |
| 69 | + -conf=*|-pid=*|-rpcsslcertificatechainfile=*|-rpcsslprivatekeyfile=*) |
| 70 | + cur="${cur#*=}" |
| 71 | + _filedir |
| 72 | + return 0 |
| 73 | + ;; |
| 74 | + -datadir=*) |
| 75 | + cur="${cur#*=}" |
| 76 | + _filedir -d |
| 77 | + return 0 |
| 78 | + ;; |
| 79 | + -*=*) # prevent nonsense completions |
| 80 | + return 0 |
| 81 | + ;; |
| 82 | + *) |
| 83 | + local helpopts commands |
| 84 | + |
| 85 | + # only parse --help if senseful |
| 86 | + if [[ -z "$cur" || "$cur" =~ ^- ]]; then |
| 87 | + helpopts=$($bitcoind --help 2>&1 | awk '$1 ~ /^-/ { sub(/=.*/, "="); print $1 }' ) |
| 88 | + fi |
| 89 | + |
| 90 | + # only parse help if senseful |
| 91 | + if [[ -z "$cur" || "$cur" =~ ^[a-z] ]]; then |
| 92 | + commands=$(_bitcoin_rpc help 2>/dev/null | awk '{ print $1; }') |
| 93 | + fi |
| 94 | + |
| 95 | + COMPREPLY=( $( compgen -W "$helpopts $commands" -- "$cur" ) ) |
| 96 | + |
| 97 | + # Prevent space if an argument is desired |
| 98 | + if [[ $COMPREPLY == *= ]]; then |
| 99 | + compopt -o nospace |
| 100 | + fi |
| 101 | + return 0 |
| 102 | + ;; |
| 103 | + esac |
| 104 | +} |
| 105 | + |
| 106 | +complete -F _bitcoind bitcoind |
| 107 | +} |
| 108 | + |
| 109 | +# Local variables: |
| 110 | +# mode: shell-script |
| 111 | +# sh-basic-offset: 4 |
| 112 | +# sh-indent-comment: t |
| 113 | +# indent-tabs-mode: nil |
| 114 | +# End: |
| 115 | +# ex: ts=4 sw=4 et filetype=sh |
0 commit comments