__main_options="
    -n -s
    --no-section
"

__get_options="
    -a -i -e -S -v -q
    --all
    --sort-collected
    --shadowing
    --verbose
    --ignore-empty-values
    --ignore-file
    --get-filename
"

__is_set_options="
    -a -e -S -v
    --all
    --sort-collected
    --shadowing
    --verbose
    --ignore-file
"

__set_options="
    -c --create --no-indent --view-comments -S --semicolon-comment -q -Q
"

__add_options="
    -c --create --no-indent --view-comments -S --semicolon-comment -p -q
"

__comment_options="
    --semicolon-comment
"

__del_options="
    -f --flush --semicolon-comment -q
"

__is_empty_options="
    -s
"



__commands="comment del get is_set set add uncomment del_section get_commented is_empty rename check -V --version -h --help"

function _get_first_command()
{
    local cmd i

    for (( i=1; i < ${#COMP_WORDS[@]}; i++ )); do
        if [[ "$1"  == *${COMP_WORDS[i]}* ]]; then
            cmd=${COMP_WORDS[i]}
            break
        fi
    done

    echo "$cmd"
}

function _commands()
{
  latest="${COMP_WORDS[$COMP_CWORD]}"
  prev="${COMP_WORDS[$COMP_CWORD - 1]}"

  command="$( _get_first_command "$__commands" )"

  words=""

  case "${prev}" in
    py-ini-config)
      words="comment del get is_set set add uncomment del_section get_commented is_empty rename check -V --version"
      ;;
    *)
      ;;
  esac

  if [ -z "${words}" ]; then
    case "${command}" in
    get)
      words="$__main_options $__get_options"
      ;;
    is_set)
      words="$__main_options $__is_set_options"
      ;;
    set)
      words="$__main_options $__set_options"
      ;;
    add)
      words="$__main_options $__add_options"
      ;;
    del)
      words="$__main_options $__del_options"
      ;;
    del_section)
      words="-s"
      ;;
    check)
      words="-n --no-section"
      ;;
    comment|uncomment|get_commented)
      words="$__main_options $__comment_options"
      ;;
    is_empty)
      words="$__is_empty_options"
      ;;
    -V|--version)
      words=""
      ;;
    *)
      words="$__main_options"
    esac
  fi

  COMPREPLY=($(compgen -W "$words" -- $latest))
  return 0
}

complete -F _commands py-ini-config


