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)"
100