Skip to content

Commit bbfcbcf

Browse files
committed
Merge bitcoin/bitcoin#24611: Add fish completions
ccba4fe doc: Add completion subdir to contrib/README.md (willcl-ark) 7075848 script: Add fish completions (willcl-ark) a27a445 refactor: Sub-folder bash completions (willcl-ark) Pull request description: The completions are dynamically generated from the respective binary help pages. Completions should be sourced into the shell or added to `$XDG_CONFIG/fish/completions`. See [where to put completions](https://fishshell.com/docs/current/completions.html#where-to-put-completions) for more information. As the completions are auto-generated they should only require as much maintenance as the bash equivalents, which is to say very little! ACKs for top commit: achow101: ACK ccba4fe josibake: ACK bitcoin/bitcoin@ccba4fe Tree-SHA512: fe6ed899ea1fe90f82970bde7739db11dd0c845ccd70b65f28ad5212f75b57d9105a3a7f70ccdff552d5b21fa3fe9c697d128fb10740bae31fe1854e716b4b8b
2 parents 9052d86 + ccba4fe commit bbfcbcf

10 files changed

+313
-0
lines changed

contrib/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,9 @@ Utilities to generate test vectors for the data-driven Bitcoin tests.
4040

4141
### [Verify Binaries](/contrib/verifybinaries) ###
4242
This script attempts to download and verify the signature file SHA256SUMS.asc from bitcoin.org.
43+
44+
Command Line Tools
45+
---------------------
46+
47+
### [Completions](/contrib/completions) ###
48+
Shell completions for bash and fish.
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# Disable files from being included in completions by default
2+
complete --command bitcoin-cli --no-files
3+
4+
function __fish_bitcoin_cli_get_commands_helper
5+
set --local cmd (commandline -oc)
6+
7+
# Don't return commands if '-help or -?' in commandline
8+
if string match --quiet --regex -- '^-help$|^-\?$' $cmd
9+
return
10+
end
11+
12+
# Strip help cmd from token to avoid duplication errors
13+
set --local cmd (string match --invert --regex -- '^help$' $cmd)
14+
# Strip -stdin* options to avoid waiting for input while we fetch completions
15+
# TODO: this appears to be broken when run as tab completion (requires ctrl+c to exit)
16+
set --local cmd (string match --invert --regex -- '^-stdin.*$' $cmd)
17+
18+
# Match, format and return commands
19+
for command in ($cmd help 2>&1 | string match --invert -r '^\=\=.*' | string match --invert -r '^\\s*$')
20+
echo $command
21+
end
22+
end
23+
24+
function __fish_bitcoin_cli_get_commands
25+
argparse 'nohelp' 'commandsonly' -- $argv
26+
set --local commands
27+
28+
# Exclude description, exclude help
29+
if set -q _flag_nohelp; and set -q _flag_commandsonly
30+
set --append commands (__fish_bitcoin_cli_get_commands_helper | string replace -r ' .*$' '' | string match --invert -r 'help')
31+
# Include description, exclude help
32+
else if set -q _flag_nohelp
33+
set --append commands (__fish_bitcoin_cli_get_commands_helper | string replace ' ' \t | string match --invert -r 'help')
34+
# Exclude description, include help
35+
else if set -q _flag_commandsonly
36+
set --append commands (__fish_bitcoin_cli_get_commands_helper | string replace -r ' .*$' '')
37+
# Include description, include help
38+
else
39+
set --append commands (__fish_bitcoin_cli_get_commands_helper | string replace ' ' \t)
40+
end
41+
42+
if string match -q -r '^.*error.*$' $commands[1]
43+
# RPC offline or RPC wallet not loaded
44+
return
45+
else
46+
for command in $commands
47+
echo $command
48+
end
49+
end
50+
end
51+
52+
53+
function __fish_bitcoin_cli_get_options
54+
argparse 'nofiles' -- $argv
55+
set --local cmd (commandline -oc)
56+
# Don't return options if '-help or -?' in commandline
57+
if string match --quiet --regex -- '^-help$|-\?$' $cmd
58+
return
59+
end
60+
set --local options
61+
62+
if set -q _flag_nofiles
63+
set --append options ($cmd -help 2>&1 | string match -r '^ -.*' | string replace -r ' -' '-' | string replace -r '=.*' '=' | string match --invert -r '^.*=$')
64+
else
65+
set --append options ($cmd -help 2>&1 | string match -r '^ -.*' | string replace -r ' -' '-' | string replace -r '=.*' '=' | string match -r '^.*=$')
66+
end
67+
68+
for option in $options
69+
echo $option
70+
end
71+
end
72+
73+
# Add options with file completion
74+
# Don't offer after a command is given
75+
complete \
76+
--command bitcoin-cli \
77+
--no-files \
78+
--condition "not __fish_seen_subcommand_from (__fish_bitcoin_cli_get_commands --commandsonly)" \
79+
--arguments "(__fish_bitcoin_cli_get_options)"
80+
# Enable file completions only if the commandline now contains a `*.=` style option
81+
complete --command bitcoin-cli \
82+
--condition 'string match --regex -- ".*=" (commandline -pt)' \
83+
--force-files
84+
85+
# Add options without file completion
86+
# Don't offer after a command is given
87+
complete \
88+
--command bitcoin-cli \
89+
--no-files \
90+
--condition "not __fish_seen_subcommand_from (__fish_bitcoin_cli_get_commands --commandsonly)" \
91+
--arguments "(__fish_bitcoin_cli_get_options --nofiles)"
92+
93+
# Add commands
94+
# Permit command completions after `bitcoin-cli help` but not after other commands
95+
complete \
96+
--command bitcoin-cli \
97+
--no-files \
98+
--condition "not __fish_seen_subcommand_from (__fish_bitcoin_cli_get_commands --commandsonly --nohelp)" \
99+
--arguments "(__fish_bitcoin_cli_get_commands)"
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Disable files from being included in completions by default
2+
complete --command bitcoin-qt --no-files
3+
4+
# Extract options
5+
function __fish_bitcoinqt_get_options
6+
argparse 'nofiles' -- $argv
7+
set --local cmd (commandline -opc)[1]
8+
set --local options
9+
10+
if set -q _flag_nofiles
11+
set --append options ($cmd -help-debug | string match -r '^ -.*' | string replace -r ' -' '-' | string replace -r '=.*' '=' | string match --invert -r '^.*=$')
12+
else
13+
set --append options ($cmd -help-debug | string match -r '^ -.*' | string replace -r ' -' '-' | string replace -r '=.*' '=' | string match -r '^.*=$')
14+
end
15+
16+
for option in $options
17+
echo $option
18+
end
19+
end
20+
21+
22+
# Add options with file completion
23+
complete \
24+
--command bitcoin-qt \
25+
--arguments "(__fish_bitcoinqt_get_options)"
26+
# Enable file completions only if the commandline now contains a `*.=` style option
27+
complete -c bitcoin-qt \
28+
--condition 'string match --regex -- ".*=" (commandline -pt)' \
29+
--force-files
30+
31+
# Add options without file completion
32+
complete \
33+
--command bitcoin-qt \
34+
--arguments "(__fish_bitcoinqt_get_options --nofiles)"
35+
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Disable files from being included in completions by default
2+
complete --command bitcoin-tx --no-files
3+
4+
# Modified version of __fish_seen_subcommand_from
5+
# Uses regex to detect cmd= syntax
6+
function __fish_bitcoin_seen_cmd
7+
set -l cmd (commandline -oc)
8+
set -e cmd[1]
9+
for i in $cmd
10+
for j in $argv
11+
if string match --quiet --regex -- "^$j.*" $i
12+
return 0
13+
end
14+
end
15+
end
16+
return 1
17+
end
18+
19+
# Extract options
20+
function __fish_bitcoin_tx_get_options
21+
set --local cmd (commandline -oc)[1]
22+
if string match --quiet --regex -- '^-help$|-\?$' $cmd
23+
return
24+
end
25+
26+
for option in ($cmd -help 2>&1 | string match -r '^ -.*' | string replace -r ' -' '-' | string replace -r '=.*' '=')
27+
echo $option
28+
end
29+
end
30+
31+
# Extract commands
32+
function __fish_bitcoin_tx_get_commands
33+
argparse 'commandsonly' -- $argv
34+
set --local cmd (commandline -oc)[1]
35+
set --local commands
36+
37+
if set -q _flag_commandsonly
38+
set --append commands ($cmd -help | sed -e '1,/Commands:/d' -e 's/=/=\t/' -e 's/(=/=/' -e '/^ [a-z]/ p' -e d | string replace -r '\ \ ' '' | string replace -r '=.*' '')
39+
else
40+
set --append commands ($cmd -help | sed -e '1,/Commands:/d' -e 's/=/=\t/' -e 's/(=/=/' -e '/^ [a-z]/ p' -e d | string replace -r '\ \ ' '')
41+
end
42+
43+
for command in $commands
44+
echo $command
45+
end
46+
end
47+
48+
# Add options
49+
complete \
50+
--command bitcoin-tx \
51+
--condition "not __fish_bitcoin_seen_cmd (__fish_bitcoin_tx_get_commands --commandsonly)" \
52+
--arguments "(__fish_bitcoin_tx_get_options)" \
53+
--no-files
54+
55+
# Add commands
56+
complete \
57+
--command bitcoin-tx \
58+
--arguments "(__fish_bitcoin_tx_get_commands)" \
59+
--no-files
60+
61+
# Add file completions for load and set commands
62+
complete \
63+
--command bitcoin-tx \
64+
--condition 'string match --regex -- "(load|set)=" (commandline -pt)' \
65+
--force-files
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Disable files from being included in completions by default
2+
complete --command bitcoin-util --no-files
3+
4+
# Extract options
5+
function __fish_bitcoin_util_get_options
6+
set --local cmd (commandline -opc)[1]
7+
set --local options
8+
9+
set --append options ($cmd -help 2>&1 | string match -r '^ -.*' | string replace -r ' -' '-' | string replace -r '=.*' '=')
10+
11+
for option in $options
12+
echo $option
13+
end
14+
end
15+
16+
# Extract commands
17+
function __fish_bitcoin_util_get_commands
18+
set --local cmd (commandline -opc)[1]
19+
set --local commands
20+
21+
set --append commands ($cmd -help | sed -e '1,/Commands:/d' -e 's/=/=\t/' -e 's/(=/=/' -e '/^ [a-z]/ p' -e d | string replace -r '\ \ ' '')
22+
for command in $commands
23+
echo $command
24+
end
25+
end
26+
27+
# Add options
28+
complete \
29+
--command bitcoin-util \
30+
--condition "not __fish_seen_subcommand_from (__fish_bitcoin_util_get_commands)" \
31+
--arguments "(__fish_bitcoin_util_get_options)"
32+
33+
# Add commands
34+
complete \
35+
--command bitcoin-util \
36+
--condition "not __fish_seen_subcommand_from (__fish_bitcoin_util_get_commands)" \
37+
--arguments "(__fish_bitcoin_util_get_commands)"
38+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Disable files from being included in completions by default
2+
complete --command bitcoin-wallet --no-files
3+
4+
# Extract options
5+
function __fish_bitcoin_wallet_get_options
6+
set --local cmd (commandline -opc)[1]
7+
for option in ($cmd -help 2>&1 | string match -r '^ -.*' | string replace -r ' -' '-' | string replace -r '=.*' '=')
8+
echo $option
9+
end
10+
end
11+
12+
# Extract commands
13+
function __fish_bitcoin_wallet_get_commands
14+
set --local cmd (commandline -opc)[1]
15+
for command in ($cmd -help | sed -e '1,/Commands:/d' -e 's/=/=\t/' -e 's/(=/=/' -e '/^ [a-z]/ p' -e d | string replace -r '\ \ ' '')
16+
echo $command
17+
end
18+
end
19+
20+
# Add options
21+
complete \
22+
--command bitcoin-wallet \
23+
--condition "not __fish_seen_subcommand_from (__fish_bitcoin_wallet_get_commands)" \
24+
--arguments "(__fish_bitcoin_wallet_get_options)"
25+
26+
# Add commands
27+
complete \
28+
--command bitcoin-wallet \
29+
--condition "not __fish_seen_subcommand_from (__fish_bitcoin_wallet_get_commands)" \
30+
--arguments "(__fish_bitcoin_wallet_get_commands)"
31+
32+
# Add file completions for load and set commands
33+
complete --command bitcoin-wallet \
34+
--condition "string match -r -- '(dumpfile|datadir)*=' (commandline -pt)" \
35+
--force-files
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Disable files from being included in completions by default
2+
complete --command bitcoind --no-files
3+
4+
# Extract options
5+
function __fish_bitcoind_get_options
6+
argparse 'nofiles' -- $argv
7+
set --local cmd (commandline -opc)[1]
8+
set --local options
9+
10+
if set -q _flag_nofiles
11+
set --append options ($cmd -help-debug | string match -r '^ -.*' | string replace -r ' -' '-' | string replace -r '=.*' '=' | string match --invert -r '^.*=$')
12+
else
13+
set --append options ($cmd -help-debug | string match -r '^ -.*' | string replace -r ' -' '-' | string replace -r '=.*' '=' | string match -r '^.*=$')
14+
end
15+
16+
for option in $options
17+
echo $option
18+
end
19+
end
20+
21+
22+
# Add options with file completion
23+
complete \
24+
--command bitcoind \
25+
--arguments "(__fish_bitcoind_get_options)"
26+
# Enable file completions only if the commandline now contains a `*.=` style option
27+
complete --command bitcoind \
28+
--condition 'string match --regex -- ".*=" (commandline -pt)' \
29+
--force-files
30+
31+
# Add options without file completion
32+
complete \
33+
--command bitcoind \
34+
--arguments "(__fish_bitcoind_get_options --nofiles)"
35+

0 commit comments

Comments
 (0)