1# bash programmable completion for bitcoin-cli(1)
2# Copyright (c) 2012-2019 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_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            importaddress|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            addmultisigaddress)
56                return 0
57                ;;
58            getbalance|gettxout|importaddress|importpubkey|importprivkey|listreceivedbyaddress|listsinceblock)
59                COMPREPLY=( $( compgen -W "true false" -- "$cur" ) )
60                return 0
61                ;;
62        esac
63    fi
64
65    if ((cword > 2)); then
66        case ${words[cword-2]} in
67            addnode)
68                COMPREPLY=( $( compgen -W "add remove onetry" -- "$cur" ) )
69                return 0
70                ;;
71            setban)
72                COMPREPLY=( $( compgen -W "add remove" -- "$cur" ) )
73                return 0
74                ;;
75            fundrawtransaction|getblock|getblockheader|getmempoolancestors|getmempooldescendants|getrawtransaction|gettransaction|listreceivedbyaddress|sendrawtransaction)
76                COMPREPLY=( $( compgen -W "true false" -- "$cur" ) )
77                return 0
78                ;;
79        esac
80    fi
81
82    case "$prev" in
83        backupwallet|dumpwallet|importwallet)
84            _filedir
85            return 0
86            ;;
87        getaddednodeinfo|getrawmempool|lockunspent)
88            COMPREPLY=( $( compgen -W "true false" -- "$cur" ) )
89            return 0
90            ;;
91        getbalance|getnewaddress|listtransactions|sendmany)
92            return 0
93            ;;
94    esac
95
96    case "$cur" in
97        -conf=*)
98            cur="${cur#*=}"
99            _filedir
100            return 0
101            ;;
102        -datadir=*)
103            cur="${cur#*=}"
104            _filedir -d
105            return 0
106            ;;
107        -*=*)	# prevent nonsense completions
108            return 0
109            ;;
110        *)
111            local helpopts commands
112
113            # only parse -help if senseful
114            if [[ -z "$cur" || "$cur" =~ ^- ]]; then
115                helpopts=$($bitcoin_cli -help 2>&1 | awk '$1 ~ /^-/ { sub(/=.*/, "="); print $1 }' )
116            fi
117
118            # only parse help if senseful
119            if [[ -z "$cur" || "$cur" =~ ^[a-z] ]]; then
120                commands=$(_bitcoin_rpc help 2>/dev/null | awk '$1 ~ /^[a-z]/ { print $1; }')
121            fi
122
123            COMPREPLY=( $( compgen -W "$helpopts $commands" -- "$cur" ) )
124
125            # Prevent space if an argument is desired
126            if [[ $COMPREPLY == *= ]]; then
127                compopt -o nospace
128            fi
129            return 0
130            ;;
131    esac
132} &&
133complete -F _bitcoin_cli bitcoin-cli
134
135# Local variables:
136# mode: shell-script
137# sh-basic-offset: 4
138# sh-indent-comment: t
139# indent-tabs-mode: nil
140# End:
141# ex: ts=4 sw=4 et filetype=sh
142