1# bash programmable completion for bitcoin-cli(1)
2# Copyright (c) 2012-2016 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)
13                rpcargs=( "${rpcargs[@]}" "$i" )
14                ;;
15        esac
16    done
17    $bitcoin_cli "${rpcargs[@]}" "$@"
18}
19
20# Add wallet accounts to COMPREPLY
21_bitcoin_accounts() {
22    local accounts
23    accounts=$(_bitcoin_rpc listaccounts | awk -F '"' '{ print $2 }')
24    COMPREPLY=( "${COMPREPLY[@]}" $( compgen -W "$accounts" -- "$cur" ) )
25}
26
27_bitcoin_cli() {
28    local cur prev words=() cword
29    local bitcoin_cli
30
31    # save and use original argument to invoke bitcoin-cli for -help, help and RPC
32    # as bitcoin-cli might not be in $PATH
33    bitcoin_cli="$1"
34
35    COMPREPLY=()
36    _get_comp_words_by_ref -n = cur prev words cword
37
38    if ((cword > 5)); then
39        case ${words[cword-5]} in
40            sendtoaddress)
41                COMPREPLY=( $( compgen -W "true false" -- "$cur" ) )
42                return 0
43                ;;
44        esac
45    fi
46
47    if ((cword > 4)); then
48        case ${words[cword-4]} in
49            importaddress|listtransactions|setban)
50                COMPREPLY=( $( compgen -W "true false" -- "$cur" ) )
51                return 0
52                ;;
53            signrawtransactionwithkey|signrawtransactionwithwallet)
54                COMPREPLY=( $( compgen -W "ALL NONE SINGLE ALL|ANYONECANPAY NONE|ANYONECANPAY SINGLE|ANYONECANPAY" -- "$cur" ) )
55                return 0
56                ;;
57        esac
58    fi
59
60    if ((cword > 3)); then
61        case ${words[cword-3]} in
62            addmultisigaddress)
63                _bitcoin_accounts
64                return 0
65                ;;
66            getbalance|gettxout|importaddress|importpubkey|importprivkey|listreceivedbyaccount|listreceivedbyaddress|listsinceblock)
67                COMPREPLY=( $( compgen -W "true false" -- "$cur" ) )
68                return 0
69                ;;
70        esac
71    fi
72
73    if ((cword > 2)); then
74        case ${words[cword-2]} in
75            addnode)
76                COMPREPLY=( $( compgen -W "add remove onetry" -- "$cur" ) )
77                return 0
78                ;;
79            setban)
80                COMPREPLY=( $( compgen -W "add remove" -- "$cur" ) )
81                return 0
82                ;;
83            fundrawtransaction|getblock|getblockheader|getmempoolancestors|getmempooldescendants|getrawtransaction|gettransaction|listaccounts|listreceivedbyaccount|listreceivedbyaddress|sendrawtransaction)
84                COMPREPLY=( $( compgen -W "true false" -- "$cur" ) )
85                return 0
86                ;;
87            move|setaccount)
88                _bitcoin_accounts
89                return 0
90                ;;
91        esac
92    fi
93
94    case "$prev" in
95        backupwallet|dumpwallet|importwallet)
96            _filedir
97            return 0
98            ;;
99        getaddednodeinfo|getrawmempool|lockunspent|setgenerate)
100            COMPREPLY=( $( compgen -W "true false" -- "$cur" ) )
101            return 0
102            ;;
103        getaccountaddress|getaddressesbyaccount|getbalance|getnewaddress|getreceivedbyaccount|listtransactions|move|sendfrom|sendmany)
104            _bitcoin_accounts
105            return 0
106            ;;
107    esac
108
109    case "$cur" in
110        -conf=*)
111            cur="${cur#*=}"
112            _filedir
113            return 0
114            ;;
115        -datadir=*)
116            cur="${cur#*=}"
117            _filedir -d
118            return 0
119            ;;
120        -*=*)	# prevent nonsense completions
121            return 0
122            ;;
123        *)
124            local helpopts commands
125
126            # only parse -help if senseful
127            if [[ -z "$cur" || "$cur" =~ ^- ]]; then
128                helpopts=$($bitcoin_cli -help 2>&1 | awk '$1 ~ /^-/ { sub(/=.*/, "="); print $1 }' )
129            fi
130
131            # only parse help if senseful
132            if [[ -z "$cur" || "$cur" =~ ^[a-z] ]]; then
133                commands=$(_bitcoin_rpc help 2>/dev/null | awk '$1 ~ /^[a-z]/ { print $1; }')
134            fi
135
136            COMPREPLY=( $( compgen -W "$helpopts $commands" -- "$cur" ) )
137
138            # Prevent space if an argument is desired
139            if [[ $COMPREPLY == *= ]]; then
140                compopt -o nospace
141            fi
142            return 0
143            ;;
144    esac
145} &&
146complete -F _bitcoin_cli bitcoin-cli
147
148# Local variables:
149# mode: shell-script
150# sh-basic-offset: 4
151# sh-indent-comment: t
152# indent-tabs-mode: nil
153# End:
154# ex: ts=4 sw=4 et filetype=sh
155