0

I tried this other questions's accepted answer but it doesn't work for me. So please don't vote this as duplicate.

My script is named "tracker" and it accepts the following switches: --dummy --audit_sessiones --user_count --detailed_user_count --parfile

The --parfile switcj should be followed by a filename.

I have this autocompletion script:

_tracker() { local cur prev opts COMPREPLY=() cur="${COMP_WORDS[COMP_CWORD]}" prev="${COMP_WORDS[COMP_CWORD-1]}" opts="--dummy --audit_sessiones --user_count --detailed_user_count --parfile" if [[ ${cur} == -* ]] ; then COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) return 0 fi opts="ls *" if [[ ${prev} == --parfile ]]; then COMPREPLY=( "${files[@]##*/}" ) return 0 fi } complete -F _tracker tracker 

Autocompletion of switches works fine.

But I also want the user to be able to use filename autocompletion right after the parameter --parfile but I haven't been able to make it work.

2 Answers 2

1

complete has a -o default option so you can remove the opts="ls *"; if ... fi part and just do complete -F _tracker -o default tracker.

According to bash manual:

If the -o default option was supplied to complete when the compspec was defined, readline's default completion will be performed if the compspec (and, if attempted, the default bash completions) generate no matches.

Sign up to request clarification or add additional context in comments.

Comments

1

Try replacing COMPREPLY=( "${files[@]##*/}" ) with COMPREPLY=( $(compgen -f ${cur}) )

More information about auto completion can be found in the following links

An introduction to bash completion: part 1

An introduction to bash completion: part 2

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.