bash completion for mysql-test-run

For many years I was using tcsh, with lots of useful customizations, that were created during these years. Now I have bash on my laptop and slowly adding what I’ve got used to. Yesterday I’ve created command line completion rules for mysql-test-run. It’s not a complete set of everything that’s possible, still it’s quite useful as it is. I need to type much less now when invoking mysql-test-run (and I invoke it quite a lot). If you’d like to try it, paste the below in your ~/.bashrc:

_mtr_complete_testnames ()
{
  dir=$1
  [ -d $dir/t ] && dir=$dir/t
  testnames=`cd $dir && echo *.test | sed -e 's/.test>//g'`
}
_mtr_complete()
{
  [ -x ./mtr ] || return
  cur=${COMP_WORDS[COMP_CWORD]}
  case $cur in
    --*)
      opts=`./mtr --list`
      COMPREPLY=( $( compgen -W "$opts" -- $cur) )
      ;;
    main.*)
      _mtr_complete_testnames .
      COMPREPLY=( $( compgen -P ${cur%.*}. -W "$testnames" -- ${cur#*.}) )
      ;;
    ?*.*)
      _mtr_complete_testnames suite/${cur%.*}
      COMPREPLY=( $( compgen -P ${cur%.*}. -W "$testnames" -- ${cur#*.}) )
      ;;
    *)
      _mtr_complete_testnames .
      suites=`cd suite && echo main * | sed 's/>/./g'`
      COMPREPLY=( $( compgen -W "$testnames $suites" -- $cur) )
      ;;
  esac
}
complete -F _mtr_complete mtr
complete -F _mtr_complete mysql-test-run
complete -F _mtr_complete mysql-test-run.pl