I am writing a bash completion script for a command-line tool:
_plink()
{
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
opts="--1 --23file --a1-allele --a2-allele --adjust --aec"
if [[ ${cur} == --*file ]] || [[ ${cur} == --out ]]; then
COMPREPLY=( $(compgen -W "$(ls)" -- ${cur}) )
elif [[ ${cur} == -* ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
fi
}
complete -F _plink plink1.9
The idea is if the after the option --*file and --out, bash should autocomplete the file path.
Right now I am using "$(ls)" which only completes the filenames in current working path. Any suggestions?
compgen -o filenames -G "$cur*"