shell - Bash Completion script to complete file path after certain arguments options -


i writing bash completion script 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 ]];         compreply=( $(compgen -w "$(ls)" -- ${cur}) )     elif [[ ${cur} == -* ]] ;         compreply=( $(compgen -w "${opts}" -- ${cur}) )         return 0     fi } complete -f _plink plink1.9 

the idea if after option --*file , --out, bash should autocomplete file path. right using "$(ls)" completes filenames in current working path. suggestions?

you can use compgen -f complete filenames, this:

if [[ ${prev} == --*file ]] || [[ ${prev} == --out ]];     compreply=( $(compgen -f -- ${cur}) ) elif ... 

however, compgen -f isn't great @ completing filenames because doesn't honour spaces in filenames.

a better way use _filedir function available in bash-completion-lib. might available on system (type: declare -f _filedir check).

using _filedir, completion function becomes:

if [[ ${prev} == --*file ]] || [[ ${prev} == --out ]];     _filedir elif ... 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -