mysql-test-run 的 bash 补全

多年来我一直使用 tcsh,并进行了许多有用的自定义。现在我在笔记本电脑上使用了 bash,并正在慢慢添加我习惯的功能。

昨天我为 mysql-test-run 创建了命令行补全规则。它不是所有可能功能的完整集合,但就目前而言已经非常有用。现在我在调用 mysql-test-run 时(而且我调用它的频率很高)需要输入的字符少多了。

如果你想尝试,请将以下内容粘贴到你的 ~/.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