function _wpg() {
    local optforcolorschemes
    _get_comp_words_by_ref cur
    # themes
    optforcolorschemes="-s -e -d -z -m -LA -A --brt --sat -R"
    # themes, pywal themes
	optforvariabletheme="-Ti"
    # themes, filenames
    optvariable="-i -o"
	# filenames
    optfordefault="--link -aL -La -a -ta -at --update"
	# templates
    optfortemplates="-td -dt"
	# pywal backends
	optforbackends="--backends"
	# pywal themes
	optforthemes="--theme"
	# pywal light themes
	optforlightthemes="-L --light"

    if [[ $COMP_CWORD = 1 ]]; then
        COMPREPLY=($(compgen -W "$(_parse_usage wpg)" -- "$cur"))
    elif [[ $COMP_CWORD < 4 || ${COMP_WORDS[1]} != "-s" ]]; then
        for opt in $optforcolorschemes; do
            if [[ $opt = ${COMP_WORDS[1]} ]]; then
                COMPREPLY=($(compgen -W "$(wpg -l)" -- "$cur"))
            fi
        done
        for opt in $optbackends; do
            if [[ $opt = ${COMP_WORDS[1]} ]]; then
                COMPREPLY=($(compgen -W "$(wpg --backends)" -- "$cur"))
            fi
        done
        for opt in $optforthemes; do
            if [[ $opt = ${COMP_WORDS[1]} ]]; then
                COMPREPLY=($(compgen -W "$(wpg --theme)" -- "$cur"))
            fi
        done
        for opt in $optvariable; do
            if [[ $opt = ${COMP_WORDS[1]} && $COMP_CWORD < 3 ]]; then
                COMPREPLY=($(compgen -W "$(wpg -l)" -- "$cur"))
            elif [[ $opt = ${COMP_WORDS[1]} ]]; then
                compopt -o default; COMPREPLY=()
            fi
        done
        for opt in $optforvariabletheme; do
            if [[ $opt = ${COMP_WORDS[1]} && $COMP_CWORD < 3 ]]; then
                COMPREPLY=($(compgen -W "$(wpg -l)" -- "$cur"))
            elif [[ $opt = ${COMP_WORDS[1]} ]]; then
                COMPREPLY=($(compgen -W "$(wpg --theme)" -- "$cur"))
            fi
        done
        for opt in $optfordefault; do
            if [[ $opt = ${COMP_WORDS[1]} ]]; then
                compopt -o default; COMPREPLY=()
            fi
        done
        for opt in $optfortemplates; do
            if [[ $opt = ${COMP_WORDS[1]} ]]; then
                COMPREPLY=($(compgen -W "$(wpg -tl)" -- "$cur"))
            fi
        done
		for opt in $optforlightthemes; do
			if [[ $opt = ${COMP_WORDS[1]} && "--theme" = ${COMP_WORDS[2]} ]]; then
				COMPREPLY=($(compgen -W "$(wpg -L --theme)" -- "$cur"))
			fi
		done
	fi
}

complete -F _wpg wpg
