_stestr()
{
  local cur prev words
  COMPREPLY=()
  _get_comp_words_by_ref -n : cur prev words

  # Command data:
  cmds='complete failing help history init last list load run slowest'
  cmds_complete='-h --help --name --shell'
  cmds_failing='-h --help --subunit --list'
  cmds_help='-h --help'
  cmds_history='list remove show'
  cmds_history_list='-h --help -f --format -c --column --quote --noindent --max-width --fit-width --print-empty --sort-column --sort-ascending --sort-descending --show-metadata -m'
  cmds_history_remove='-h --help'
  cmds_history_show='-h --help --subunit --no-subunit-trace --force-subunit-trace --color --suppress-attachments --all-attachments --show-binary-attachments'
  cmds_init='-h --help'
  cmds_last='-h --help --subunit --no-subunit-trace --force-subunit-trace --color --suppress-attachments --all-attachments --show-binary-attachments'
  cmds_list='-h --help --exclude-list -e --include-list -i --exclude-regex -E'
  cmds_load='-h --help --force-init --subunit --id -i --subunit-trace --color --abbreviate --suppress-attachments --all-attachments --show-binary-attachments'
  cmds_run='-h --help --failing --serial --concurrency --load-list --subunit --until-failure --analyze-isolation --isolated --worker-file --exclude-list -e --include-list -i --exclude-regex -E --no-discover -n --pdb --random --combine --no-subunit-trace --force-subunit-trace --color --slowest --abbreviate --suppress-attachments --all-attachments --show-binary-attachments'
  cmds_slowest='-h --help --all'

  dash=-
  underscore=_
  cmd=""
  words[0]=""
  completed="${cmds}"
  for var in "${words[@]:1}"
  do
    if [[ ${var} == -* ]] ; then
      break
    fi
    if [ -z "${cmd}" ] ; then
      proposed="${var}"
    else
      proposed="${cmd}_${var}"
    fi
    local i="cmds_${proposed}"
    i=${i//$dash/$underscore}
    local comp="${!i}"
    if [ -z "${comp}" ] ; then
      break
    fi
    if [[ ${comp} == -* ]] ; then
      if [[ ${cur} != -* ]] ; then
        completed=""
        break
      fi
    fi
    cmd="${proposed}"
    completed="${comp}"
  done

  if [ -z "${completed}" ] ; then
    COMPREPLY=( $( compgen -f -- "$cur" ) $( compgen -d -- "$cur" ) )
  else
    COMPREPLY=( $(compgen -W "${completed}" -- ${cur}) )
  fi
  return 0
}
complete -F _stestr stestr
