-
Notifications
You must be signed in to change notification settings - Fork 4
Open
Description
The following code provides bash completion for helmsman commands whose sub-commands are "reasonable" (e.g. use commander):
# Parse helmsman top-level help output
# @param $1 command; if "-", read from stdin and ignore rest of args
# @param $2 command options (default: --help)
#
_parse_helmsman_subcommands ()
{
eval local cmd=$( quote "$1" )
local line IFS=''
LC_ALL=C "$( dequote "$cmd" )" ${2:---help} 2>&1 \
| while read -r line; do
[[ $line == @( )* ]] || continue
if [[ $line =~ ([A-Za-z][-A-Za-z0-9]*) ]]; then
printf '%s ' "${BASH_REMATCH[1]}"
fi
done
}
# Parse helmsman help output
# Assumes that subcommands accept --help and can be parsed with _parse_help
# @param $1 command; if "-", read from stdin and ignore rest of args
# @param $2 command options (default: --help)
#
_parse_helmsman ()
{
eval local cmd=$( quote "$1" )
local cur dashoptions prev i
COMPREPLY=()
_get_comp_words_by_ref cur prev
dashoptions='--help'
commands=( $(_parse_helmsman_subcommands "$( dequote "$cmd" )") )
if [[ "$prev" == "$( dequote "$cmd" )" ]]; then
# Complete subcommand or top-level option
if [[ "$cur" == -* ]]; then
COMPREPLY=( $( compgen -W "$dashoptions" -- "$cur" ) )
else
COMPREPLY=( $( for command in "${commands[@]}"; do
[[ ${command:0:${#cur}} == "$cur" ]] && printf '%s\n' $command
done ) )
fi
else # Complete subcommand option or argument
for command in "${commands[@]}"; do
if [[ "$command" == "$prev" ]]; then
COMPREPLY=( $( compgen -W '$( _parse_help "$( dequote "$cmd" )" "$prev --help")' -- "$cur" ) )
fi
done
# If no option found, complete filenames
[[ ${#COMPREPLY[@]} -eq 0 ]] && _filedir
fi
return 0
}
With this in place, one can say, e.g.: complete -F _parse_helmsman foo, where foo is a command written with helmsman, to get completion on subcommand names and options.
I'm not quite sure what the best thing to do with this code is. I just put it in my ~/.bash_completion, but I guess ideally it would go as a file in /usr/share/bash-completion/helpers. It's hard to see how helmsman itself could install that though…You could simply provide it as a file which can be copied (or whose contents can be copied and pasted) at will.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels