# This script was generated by crazy-complete.
# crazy-complete: A tool that creates robust and reliable autocompletion scripts for Bash, Fish and Zsh.
# For more information, visit: https://github.com/crazy-complete/crazy-complete

_example__parse_commandline() {
  POSITIONALS=()
  END_OF_OPTIONS=0

  local cmd="root" argi arg i char trailing_chars VAR ARGS

  __find_option() {
    case "$1" in root:view-log*)
      case "$2" in
        --pager) VAR=OPT_pager; ARGS='?'; return;;
      esac
    esac
    case "$1" in root:start*)
      case "$2" in
        -v|--verbose) VAR=OPT_verbose; ARGS=0; return;;
        --deprecated-option) VAR=OPT_deprecated_option; ARGS=0; return;;
        --preserve-env) VAR=OPT_preserve_env; ARGS=1; return;;
        --priority) VAR=OPT_priority; ARGS=1; return;;
        --foreground) VAR=OPT_foreground; ARGS=0; return;;
        --background) VAR=OPT_background; ARGS=0; return;;
        --pid-file) VAR=OPT_pid_file; ARGS=1; return;;
        -o|--options) VAR=OPT_options; ARGS=1; return;;
      esac
    esac
    case "$1" in root*)
      case "$2" in
        -h|--help) VAR=OPT_help; ARGS=0; return;;
      esac
    esac
    return 1
  }

  __append_to_array() {
    local -n arr=$1
    arr+=("$2")
  }

  for ((argi=1; argi < cword; ++argi)); do
    arg="${words[argi]}"

    case "$arg" in
      --)
        END_OF_OPTIONS=1
        POSITIONALS+=("${words[@]:$((++argi))}")
        return;;
      --*=*)
        if __find_option "$cmd" "${arg%%=*}"
        then __append_to_array "$VAR" "${arg#*=}"
        fi;;
      --*)
        if __find_option "$cmd" "$arg"; then
          if [[ "$ARGS" == 1 ]]
          then __append_to_array "$VAR" "${words[++argi]}"
          else __append_to_array "$VAR" "_OPT_ISSET_"
          fi
        fi;;
      -?*) # ignore '-'

        for ((i=1; i < ${#arg}; ++i)); do
          char="${arg:$i:1}"
          trailing_chars="${arg:$((i + 1))}"

          if __find_option "$cmd" "-$char"; then
            if [[ "$ARGS" == 1 ]]; then
              if [[ -n "$trailing_chars" ]]
              then __append_to_array "$VAR" "$trailing_chars"
              else __append_to_array "$VAR" "${words[++argi]}"
              fi
              break;
            else
              __append_to_array "$VAR" "_OPT_ISSET_"
            fi
          fi
        done
        ;;
      *)
        POSITIONALS+=("$arg")
        case "$cmd|${#POSITIONALS[@]}" in
          'root|1')
            case "$arg" in
              launch) cmd+=":start";;
              *) cmd+=":$arg";;
            esac;;
        esac
        ;;
    esac
  done

  for ((; argi <= cword; ++argi)); do
    case "${words[argi]}" in
      -?*);;
      *) POSITIONALS+=("${words[argi]}");;
    esac
  done
}

_example__value_list() {
  local duplicates=0
  [[ "$1" == '-d' ]] && { duplicates=1; shift; }
  local separator="$1"; shift

  compopt -o nospace

  local cur_unquoted break_pos in_quotes
  _example__dequote "$cur" cur_unquoted break_pos in_quotes

  if [[ -z "$cur_unquoted" ]]; then
    COMPREPLY=("$@")
    return
  fi

  local value having_value having_values=() remaining_values=()

  IFS="$separator" read -r -a having_values <<< "$cur_unquoted"

  if (( duplicates )); then
    remaining_values=("$@")
  else
    for value; do
      if ! _example__array_contains "$value" "${having_values[@]}"; then
        remaining_values+=("$value")
      fi
    done
  fi

  COMPREPLY=()

  local cur_stripped="$cur_unquoted"
  if (( break_pos > -1 )); then
    cur_stripped="${cur_stripped:break_pos}"
  fi

  if [[ "${cur_unquoted: -1}" == "$separator" ]]; then
    for value in "${remaining_values[@]}"; do
      COMPREPLY+=("$cur_stripped$value")
    done
  elif (( ${#remaining_values[@]} )); then
    if _example__array_contains "${having_values[-1]}" "$@"; then
      COMPREPLY+=("$cur_stripped$separator")
    elif (( ${#having_values[@]} )); then
      local cur_last_value=${having_values[-1]}
      cur_stripped="${cur_stripped%"$cur_last_value"}"

      for value in "${remaining_values[@]}"; do
        if [[ "$value" == "$cur_last_value"* ]]; then
          COMPREPLY+=("$cur_stripped$value")
        fi
      done
    fi
  fi
}

_example__dequote() {
  local in="$1" len=${#1} i=0 result='' ___break_pos=-1 ___in_quotes=0

  for ((; i < len; ++i)); do
    case "${in:i:1}" in
      "'")
        ___in_quotes=1
        for ((++i; i < len; ++i)); do
          [[ "${in:i:1}" == "'" ]] && { ___in_quotes=0; break; }
          result+="${in:i:1}"
        done;;
      '"')
        ___in_quotes=1
        for ((++i; i < len; ++i)); do
          [[ "${in:i:1}" == '"' ]] && { ___in_quotes=0; break; }

          if [[ "${in:i:1}" == '\' ]]; then
            result+="${in:$((++i)):1}"
          else
            result+="${in:i:1}"
          fi
        done;;
      '\')
        result+="${in:$((++i)):1}";;
      [$COMP_WORDBREAKS])
        result+="${in:i:1}"
        ___break_pos=${#result};;
      *)
        result+="${in:i:1}";;
    esac
  done

  local -n ___RESULT=$2
  local -n ___BREAK_POS=$3
  local -n ___IN_QUOTES=$4
  ___RESULT="$result"
  ___BREAK_POS=$___break_pos
  ___IN_QUOTES=$___in_quotes
}

_example__array_contains() {
  local w='' search="$1"; shift;
  for w; do [[ "$search" == "$w" ]] && return 0; done
  return 1
}

_example__locales() {
  COMPREPLY=($(compgen -W "$(command locale -a)" -- "$cur"))
}

_example__key_value_list() {
  local sep1="$1"; shift
  local sep2="$1"; shift
  local -A keys=()
  local i

  for ((i=1; i <= $#; i += 2)); do
    keys["${@:i:1}"]="${@:i + 1:1}"
  done

  local strip_chars=''
  [[ "$COMP_WORDBREAKS" == *"$sep1"* ]] && strip_chars+="$sep1"
  [[ "$COMP_WORDBREAKS" == *"$sep2"* ]] && strip_chars+="$sep2"
  [[ "${cur:0:1}" == '"' ]] && strip_chars=''
  [[ "${cur:0:1}" == "'" ]] && strip_chars=''
  local cur="$cur" break_pos in_quotes
  _example__dequote "$cur" cur break_pos in_quotes

  if [[ -z "$cur" ]]; then
    COMPREPLY=("${!keys[@]}")
    return
  fi

  local pair key value found_key cur_stripped="$cur"
  local -a having_pairs having_keys remaining_keys

  IFS="$sep1" read -r -a having_pairs <<< "$cur"

  for pair in "${having_pairs[@]}"; do
    having_keys+=("${pair%%"$sep2"*}")
  done

  for key in "${!keys[@]}"; do
    found_key=0

    for having_key in "${having_keys[@]}"; do
      if [[ "$key" == "$having_key" ]]; then
        found_key=1
        break
      fi
    done

    if (( ! found_key )); then
      remaining_keys+=("$key")
    fi
  done

  COMPREPLY=()

  if [[ "${cur: -1}" == "$sep1" ]]; then
    [[ -n "$strip_chars" ]] && cur_stripped="${cur_stripped##*[$strip_chars]}"

    for key in "${remaining_keys[@]}"; do
      COMPREPLY+=("$cur_stripped$key")
    done
  else
    pair="${cur##*"$sep1"}"
    if [[ "$pair" == *"$sep2"* ]]; then
      key="${pair%%"$sep2"*}"
      value="${pair##*"$sep2"}"
      cur="$value"
      ${keys[$key]}

      cur_stripped="${cur_stripped:0:$(( ${#cur_stripped} - ${#value} ))}"
      if [[ -n "$strip_chars" ]]; then
          cur_stripped="${cur_stripped##*[$strip_chars]}"
      fi

      for i in "${!COMPREPLY[@]}"; do
        COMPREPLY[i]="$cur_stripped${COMPREPLY[i]}"
      done
    else
      [[ -n "$strip_chars" ]] && cur_stripped="${cur_stripped##*[$strip_chars]}"
      cur_stripped="${cur_stripped%"$pair"}"

      for key in "${remaining_keys[@]}"; do
        if [[ "$key" == "$pair"* ]]; then
          COMPREPLY+=("$cur_stripped$key")
        fi
      done
    fi
  fi
}

_example__prefix_compreply() {
  [[ "$cur" == *[$COMP_WORDBREAKS]* ]] && return

  local i prefix="$1"
  for ((i=0; i < ${#COMPREPLY[@]}; ++i)); do
    COMPREPLY[i]="$prefix${COMPREPLY[i]}"
  done
}

_example_start__-o() {
  COMPREPLY=($(compgen -A user -- "$cur"))
}

_example_start__-o0() {
  COMPREPLY=($(compgen -A group -- "$cur"))
}

_example() {
  local cur prev words cword split
  _init_completion -n =: || return

  local END_OF_OPTIONS POSITIONALS
  local -a OPT_help OPT_verbose OPT_deprecated_option OPT_preserve_env OPT_priority OPT_foreground OPT_background OPT_pid_file OPT_options OPT_pager

  _example__parse_commandline

  if (( 0 < ${#POSITIONALS[@]} )); then
    case "${POSITIONALS[0]}" in
      start|launch) _example_start && return 0;;
      view-log) _example_view_log && return 0;;
    esac
  fi

  if (( ! END_OF_OPTIONS )) && [[ "$cur" = -* ]]; then
    local -a opts
    if (( ! ${#OPT_help[@]} )); then
      opts+=(-h --help)
    fi
    COMPREPLY+=($(compgen -W "${opts[*]}" -- "$cur"))
    [[ ${COMPREPLY-} == *= ]] && compopt -o nospace
    return 1
  fi

  (( ${#POSITIONALS[@]} == 1 )) && {
    COMPREPLY=($(compgen -W 'start launch view-log' -- "$cur"))
    return 0;
  }

  return 1
}

_example_start() {
  if (( ${#POSITIONALS[@]} >= 3 )); then
    local realpos
    for realpos in "${!COMP_WORDS[@]}"; do
      [[ "${COMP_WORDS[realpos]}" == "${POSITIONALS[1]}" ]] && {
        _command_offset $realpos
        return 0;
      }
    done
  fi

  __complete_option() {
    local opt="$1" cur="$2"

    case "$opt" in --pid-file)
      if (( ${#OPT_background[@]} )); then
        _filedir
        return 0
      fi;;
    esac

    case "$opt" in
      --preserve-env)
        local cur_old="$cur"
        cur="${cur##*,}"
        COMPREPLY=($(compgen -A export -- "$cur"))
        cur="$cur_old"
        _example__value_list , "${COMPREPLY[@]}"
        return 0;;
      --priority)
        COMPREPLY=($(compgen -W 'low normal high realtime' -- "$cur"))
        compopt -o nosort
        return 0;;
      --options|-o)
        _example__key_value_list , = \
          lang _example__locales \
          user _example_start__-o \
          group _example_start__-o0
        return 0;;
    esac

    return 1
  }

  case "$prev" in
    --*) __complete_option "$prev" "$cur" && return 0;;
    -*([vh])[o])
         __complete_option "-${prev: -1}" "$cur" && return 0;;
  esac

  case "$cur" in
    --*=*)
      __complete_option "${cur%%=*}" "${cur#*=}" && return 0;;
    --*);;
    -*([vh])[o]*)
      local i
      for ((i=2; i <= ${#cur}; ++i)); do
        local pre="${cur:0:$i}" value="${cur:$i}"
        __complete_option "-${pre: -1}" "$value" && {
          _example__prefix_compreply "$pre"
          return 0
        }
      done;;
  esac

  if (( ! END_OF_OPTIONS )) && [[ "$cur" = -* ]]; then
    local -a opts
    if (( ! ${#OPT_help[@]} )); then
      opts+=(-v --verbose)
      (( ! ${#OPT_preserve_env[@]} )) && opts+=(--preserve-env=)
      (( ! ${#OPT_priority[@]} )) && opts+=(--priority=)
      (( ! ${#OPT_background[@]} && ! ${#OPT_foreground[@]} )) && opts+=(--foreground --background)
      (( ! ${#OPT_pid_file[@]} )) && (( ${#OPT_background[@]} )) && opts+=(--pid-file=)
      (( ! ${#OPT_options[@]} )) && opts+=(-o --options=)
    fi
    COMPREPLY+=($(compgen -W "${opts[*]}" -- "$cur"))
    [[ ${COMPREPLY-} == *= ]] && compopt -o nospace
    return 1
  fi

  (( ${#POSITIONALS[@]} == 2 )) && {
    COMPREPLY=($(compgen -A command -- "$cur"))
    return 0;
  }

  return 1
}

_example_view_log() {
  __complete_option() {
    local opt="$1" cur="$2" mode="$3" ret=0

    (( ! ret )) && return 0
    [[ "$mode" == WITH_OPTIONAL ]] || return 1
    ret=0

    case "$opt" in
      --pager)
        COMPREPLY=($(compgen -A command -- "$cur"));;
      *) ret=1;;
    esac

    return $ret
  }

  case "$cur" in
    --*=*)
      __complete_option "${cur%%=*}" "${cur#*=}" WITH_OPTIONAL && return 0;;
  esac

  if (( ! END_OF_OPTIONS )) && [[ "$cur" = -* ]]; then
    local -a opts
    if (( ! ${#OPT_help[@]} )); then
      (( ! ${#OPT_pager[@]} )) && opts+=(--pager=)
    fi
    COMPREPLY+=($(compgen -W "${opts[*]}" -- "$cur"))
    [[ ${COMPREPLY-} == *= ]] && compopt -o nospace
    return 1
  fi

  return 1
}

complete -F _example example

# vim: ft=sh ts=2 sts=2 sw=2 et