0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-03-05 14:06:27 -05:00

Merge bitcoin/bitcoin#24611: Add fish completions

ccba4fe7e3 doc: Add completion subdir to contrib/README.md (willcl-ark)
7075848f96 script: Add fish completions (willcl-ark)
a27a445b71 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 ccba4fe7e3
  josibake:
    ACK ccba4fe7e3

Tree-SHA512: fe6ed899ea1fe90f82970bde7739db11dd0c845ccd70b65f28ad5212f75b57d9105a3a7f70ccdff552d5b21fa3fe9c697d128fb10740bae31fe1854e716b4b8b
This commit is contained in:
Andrew Chow 2022-12-07 18:29:04 -05:00
commit bbfcbcfa0c
No known key found for this signature in database
GPG key ID: 17565732E08E5E41
10 changed files with 313 additions and 0 deletions

View file

@ -40,3 +40,9 @@ Utilities to generate test vectors for the data-driven Bitcoin tests.
### [Verify Binaries](/contrib/verifybinaries) ###
This script attempts to download and verify the signature file SHA256SUMS.asc from bitcoin.org.
Command Line Tools
---------------------
### [Completions](/contrib/completions) ###
Shell completions for bash and fish.

View file

@ -0,0 +1,99 @@
# Disable files from being included in completions by default
complete --command bitcoin-cli --no-files
function __fish_bitcoin_cli_get_commands_helper
set --local cmd (commandline -oc)
# Don't return commands if '-help or -?' in commandline
if string match --quiet --regex -- '^-help$|^-\?$' $cmd
return
end
# Strip help cmd from token to avoid duplication errors
set --local cmd (string match --invert --regex -- '^help$' $cmd)
# Strip -stdin* options to avoid waiting for input while we fetch completions
# TODO: this appears to be broken when run as tab completion (requires ctrl+c to exit)
set --local cmd (string match --invert --regex -- '^-stdin.*$' $cmd)
# Match, format and return commands
for command in ($cmd help 2>&1 | string match --invert -r '^\=\=.*' | string match --invert -r '^\\s*$')
echo $command
end
end
function __fish_bitcoin_cli_get_commands
argparse 'nohelp' 'commandsonly' -- $argv
set --local commands
# Exclude description, exclude help
if set -q _flag_nohelp; and set -q _flag_commandsonly
set --append commands (__fish_bitcoin_cli_get_commands_helper | string replace -r ' .*$' '' | string match --invert -r 'help')
# Include description, exclude help
else if set -q _flag_nohelp
set --append commands (__fish_bitcoin_cli_get_commands_helper | string replace ' ' \t | string match --invert -r 'help')
# Exclude description, include help
else if set -q _flag_commandsonly
set --append commands (__fish_bitcoin_cli_get_commands_helper | string replace -r ' .*$' '')
# Include description, include help
else
set --append commands (__fish_bitcoin_cli_get_commands_helper | string replace ' ' \t)
end
if string match -q -r '^.*error.*$' $commands[1]
# RPC offline or RPC wallet not loaded
return
else
for command in $commands
echo $command
end
end
end
function __fish_bitcoin_cli_get_options
argparse 'nofiles' -- $argv
set --local cmd (commandline -oc)
# Don't return options if '-help or -?' in commandline
if string match --quiet --regex -- '^-help$|-\?$' $cmd
return
end
set --local options
if set -q _flag_nofiles
set --append options ($cmd -help 2>&1 | string match -r '^ -.*' | string replace -r ' -' '-' | string replace -r '=.*' '=' | string match --invert -r '^.*=$')
else
set --append options ($cmd -help 2>&1 | string match -r '^ -.*' | string replace -r ' -' '-' | string replace -r '=.*' '=' | string match -r '^.*=$')
end
for option in $options
echo $option
end
end
# Add options with file completion
# Don't offer after a command is given
complete \
--command bitcoin-cli \
--no-files \
--condition "not __fish_seen_subcommand_from (__fish_bitcoin_cli_get_commands --commandsonly)" \
--arguments "(__fish_bitcoin_cli_get_options)"
# Enable file completions only if the commandline now contains a `*.=` style option
complete --command bitcoin-cli \
--condition 'string match --regex -- ".*=" (commandline -pt)' \
--force-files
# Add options without file completion
# Don't offer after a command is given
complete \
--command bitcoin-cli \
--no-files \
--condition "not __fish_seen_subcommand_from (__fish_bitcoin_cli_get_commands --commandsonly)" \
--arguments "(__fish_bitcoin_cli_get_options --nofiles)"
# Add commands
# Permit command completions after `bitcoin-cli help` but not after other commands
complete \
--command bitcoin-cli \
--no-files \
--condition "not __fish_seen_subcommand_from (__fish_bitcoin_cli_get_commands --commandsonly --nohelp)" \
--arguments "(__fish_bitcoin_cli_get_commands)"

View file

@ -0,0 +1,35 @@
# Disable files from being included in completions by default
complete --command bitcoin-qt --no-files
# Extract options
function __fish_bitcoinqt_get_options
argparse 'nofiles' -- $argv
set --local cmd (commandline -opc)[1]
set --local options
if set -q _flag_nofiles
set --append options ($cmd -help-debug | string match -r '^ -.*' | string replace -r ' -' '-' | string replace -r '=.*' '=' | string match --invert -r '^.*=$')
else
set --append options ($cmd -help-debug | string match -r '^ -.*' | string replace -r ' -' '-' | string replace -r '=.*' '=' | string match -r '^.*=$')
end
for option in $options
echo $option
end
end
# Add options with file completion
complete \
--command bitcoin-qt \
--arguments "(__fish_bitcoinqt_get_options)"
# Enable file completions only if the commandline now contains a `*.=` style option
complete -c bitcoin-qt \
--condition 'string match --regex -- ".*=" (commandline -pt)' \
--force-files
# Add options without file completion
complete \
--command bitcoin-qt \
--arguments "(__fish_bitcoinqt_get_options --nofiles)"

View file

@ -0,0 +1,65 @@
# Disable files from being included in completions by default
complete --command bitcoin-tx --no-files
# Modified version of __fish_seen_subcommand_from
# Uses regex to detect cmd= syntax
function __fish_bitcoin_seen_cmd
set -l cmd (commandline -oc)
set -e cmd[1]
for i in $cmd
for j in $argv
if string match --quiet --regex -- "^$j.*" $i
return 0
end
end
end
return 1
end
# Extract options
function __fish_bitcoin_tx_get_options
set --local cmd (commandline -oc)[1]
if string match --quiet --regex -- '^-help$|-\?$' $cmd
return
end
for option in ($cmd -help 2>&1 | string match -r '^ -.*' | string replace -r ' -' '-' | string replace -r '=.*' '=')
echo $option
end
end
# Extract commands
function __fish_bitcoin_tx_get_commands
argparse 'commandsonly' -- $argv
set --local cmd (commandline -oc)[1]
set --local commands
if set -q _flag_commandsonly
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 '=.*' '')
else
set --append commands ($cmd -help | sed -e '1,/Commands:/d' -e 's/=/=\t/' -e 's/(=/=/' -e '/^ [a-z]/ p' -e d | string replace -r '\ \ ' '')
end
for command in $commands
echo $command
end
end
# Add options
complete \
--command bitcoin-tx \
--condition "not __fish_bitcoin_seen_cmd (__fish_bitcoin_tx_get_commands --commandsonly)" \
--arguments "(__fish_bitcoin_tx_get_options)" \
--no-files
# Add commands
complete \
--command bitcoin-tx \
--arguments "(__fish_bitcoin_tx_get_commands)" \
--no-files
# Add file completions for load and set commands
complete \
--command bitcoin-tx \
--condition 'string match --regex -- "(load|set)=" (commandline -pt)' \
--force-files

View file

@ -0,0 +1,38 @@
# Disable files from being included in completions by default
complete --command bitcoin-util --no-files
# Extract options
function __fish_bitcoin_util_get_options
set --local cmd (commandline -opc)[1]
set --local options
set --append options ($cmd -help 2>&1 | string match -r '^ -.*' | string replace -r ' -' '-' | string replace -r '=.*' '=')
for option in $options
echo $option
end
end
# Extract commands
function __fish_bitcoin_util_get_commands
set --local cmd (commandline -opc)[1]
set --local commands
set --append commands ($cmd -help | sed -e '1,/Commands:/d' -e 's/=/=\t/' -e 's/(=/=/' -e '/^ [a-z]/ p' -e d | string replace -r '\ \ ' '')
for command in $commands
echo $command
end
end
# Add options
complete \
--command bitcoin-util \
--condition "not __fish_seen_subcommand_from (__fish_bitcoin_util_get_commands)" \
--arguments "(__fish_bitcoin_util_get_options)"
# Add commands
complete \
--command bitcoin-util \
--condition "not __fish_seen_subcommand_from (__fish_bitcoin_util_get_commands)" \
--arguments "(__fish_bitcoin_util_get_commands)"

View file

@ -0,0 +1,35 @@
# Disable files from being included in completions by default
complete --command bitcoin-wallet --no-files
# Extract options
function __fish_bitcoin_wallet_get_options
set --local cmd (commandline -opc)[1]
for option in ($cmd -help 2>&1 | string match -r '^ -.*' | string replace -r ' -' '-' | string replace -r '=.*' '=')
echo $option
end
end
# Extract commands
function __fish_bitcoin_wallet_get_commands
set --local cmd (commandline -opc)[1]
for command in ($cmd -help | sed -e '1,/Commands:/d' -e 's/=/=\t/' -e 's/(=/=/' -e '/^ [a-z]/ p' -e d | string replace -r '\ \ ' '')
echo $command
end
end
# Add options
complete \
--command bitcoin-wallet \
--condition "not __fish_seen_subcommand_from (__fish_bitcoin_wallet_get_commands)" \
--arguments "(__fish_bitcoin_wallet_get_options)"
# Add commands
complete \
--command bitcoin-wallet \
--condition "not __fish_seen_subcommand_from (__fish_bitcoin_wallet_get_commands)" \
--arguments "(__fish_bitcoin_wallet_get_commands)"
# Add file completions for load and set commands
complete --command bitcoin-wallet \
--condition "string match -r -- '(dumpfile|datadir)*=' (commandline -pt)" \
--force-files

View file

@ -0,0 +1,35 @@
# Disable files from being included in completions by default
complete --command bitcoind --no-files
# Extract options
function __fish_bitcoind_get_options
argparse 'nofiles' -- $argv
set --local cmd (commandline -opc)[1]
set --local options
if set -q _flag_nofiles
set --append options ($cmd -help-debug | string match -r '^ -.*' | string replace -r ' -' '-' | string replace -r '=.*' '=' | string match --invert -r '^.*=$')
else
set --append options ($cmd -help-debug | string match -r '^ -.*' | string replace -r ' -' '-' | string replace -r '=.*' '=' | string match -r '^.*=$')
end
for option in $options
echo $option
end
end
# Add options with file completion
complete \
--command bitcoind \
--arguments "(__fish_bitcoind_get_options)"
# Enable file completions only if the commandline now contains a `*.=` style option
complete --command bitcoind \
--condition 'string match --regex -- ".*=" (commandline -pt)' \
--force-files
# Add options without file completion
complete \
--command bitcoind \
--arguments "(__fish_bitcoind_get_options --nofiles)"