File: _example

package info (click to toggle)
crazy-complete 0.3.7-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,528 kB
  • sloc: python: 13,342; sh: 995; makefile: 68
file content (200 lines) | stat: -rw-r--r-- 5,897 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#compdef example

# 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__query() {

  __zsh_query_contains() {
    local arg='' key="$1"; shift
    for arg; do [[ "$key" == "$arg" ]] && return 0; done
    return 1
  }

  local cmd="$1"; shift

  case "$cmd" in
    get_positional)
      printf "%s" "${POSITIONALS[$1]}"
      return 0;;
    has_option)
      local option='' with_incomplete=0
      [[ "$1" == 'WITH_INCOMPLETE' ]] && { with_incomplete=1; shift; }

      for option in "${HAVING_OPTIONS[@]}"; do
        __zsh_query_contains "$option" "$@" && return 0
      done

      (( with_incomplete )) && \
        __zsh_query_contains "$INCOMPLETE_OPTION" "$@" && return 0

      return 1;;
    init)
      local IFS=','
      local -a options=(${=1})
      unset IFS
      shift;;
    *)
      echo "_example__query: argv[1]: invalid command" >&2
      return 1;;
  esac


  local  long_opts_with_arg=()  long_opts_with_optional_arg=()  long_opts_without_arg=()
  local short_opts_with_arg=() short_opts_with_optional_arg=() short_opts_without_arg=()

  local option=''
  for option in "${options[@]}"; do
    case "$option" in
      --?*=)    long_opts_with_arg+=("${option%=}");;
      --?*=\?)  long_opts_with_optional_arg+=("${option%=?}");;
      --?*)     long_opts_without_arg+=("$option");;
      -?=)      short_opts_with_arg+=("${option%=}");;
      -?=\?)    short_opts_with_optional_arg+=("${option%=?}");;
      -?)       short_opts_without_arg+=("$option");;
    esac
  done

  POSITIONALS=()
  HAVING_OPTIONS=()
  OPTION_VALUES=()
  INCOMPLETE_OPTION=''

  local argi=2 # argi[1] is program name
  for ((; argi <= $#; ++argi)); do
    local arg="${@[$argi]}"
    local have_trailing_arg=$(test $argi -lt $# && echo true || echo false)

    case "$arg" in
      --)
        POSITIONALS+=("${@:$((argi + 1))}")
        break;;
      --*=*)
        HAVING_OPTIONS+=("${arg%%=*}")
        OPTION_VALUES+=("${arg#*=}");;
      --*)
        if __zsh_query_contains "$arg" "${long_opts_with_arg[@]}"; then
          if $have_trailing_arg; then
            HAVING_OPTIONS+=("$arg")
            OPTION_VALUES+=("${@[$((++argi))]}")
          else
            INCOMPLETE_OPTION="$arg"
          fi
        else
          HAVING_OPTIONS+=("$arg")
          OPTION_VALUES+=("")
        fi
        ;;
      -?*) # ignore '-'

        local i=1 arg_length=${#arg}
        for ((; i < arg_length; ++i)); do
          local option="-${arg:$i:1}"
          local trailing_chars="${arg:$((i+1))}"

          if __zsh_query_contains "$option" "${short_opts_without_arg[@]}"; then
            HAVING_OPTIONS+=("$option")
            OPTION_VALUES+=("")
          elif __zsh_query_contains "$option" "${short_opts_with_arg[@]}"; then
            if [[ -n "$trailing_chars" ]]; then
              HAVING_OPTIONS+=("$option")
              OPTION_VALUES+=("$trailing_chars")
            elif $have_trailing_arg; then
              HAVING_OPTIONS+=("$option")
              OPTION_VALUES+=("${@[$((++argi))]}")
            else
              INCOMPLETE_OPTION="$option"
            fi

            continue 2
          elif __zsh_query_contains "$option" "${short_opts_with_optional_arg[@]}"; then
            HAVING_OPTIONS+=("$option")
            OPTION_VALUES+=("$trailing_chars") # may be empty
            continue 2
          fi
        done
        ;;
      *)
        POSITIONALS+=("$arg");;
    esac
  done
}

_example_start__--preserve-env() {
  _parameters -g '*export*'
}

_example_start__--priority() {
  local items=(
    low:'Run with lowest priority, background tasks only'
    normal:'Standard scheduling priority for regular tasks'
    high:'Higher priority for time-sensitive operations'
    realtime:'Real-time scheduling, may affect system responsiveness'
  )

  _describe -- '' items
}

_example_start__-o() {
  _values -s , -S = '' \
    lang'[set language]':::_locales \
    user'[set user]':::_users \
    group'[set group]':::_groups
}

_example() {
  local opts=-h,--help
  local HAVING_OPTIONS=() OPTION_VALUES=() POSITIONALS=() INCOMPLETE_OPTION=''
  _example__query init "$opts" "${words[@]}"

  case "$(_example__query get_positional 1)" in
    start|launch) _example_start; return $?;;
    view-log) _example_view_log; return $?;;
  esac

  local -a args=(
    '(- *)'{-h,--help}
    1:command1:'((start launch view-log))'
  )
  _arguments -S -s -w "${args[@]}"
}

_example_start() {
  local opts=-v,--verbose,--deprecated-option,--preserve-env=,--priority=,--foreground,--background,--pid-file=,-o=,--options=,-h,--help
  local HAVING_OPTIONS=() OPTION_VALUES=() POSITIONALS=() INCOMPLETE_OPTION=''
  _example__query init "$opts" "${words[@]}"

  local -a args=(
    '*'{-v,--verbose}
    '(--preserve-env)'--preserve-env=:' ':'_sequence _example_start__--preserve-env'
    '(--priority)'--priority=:' ':_example_start__--priority
    '(--background --foreground)'--foreground
    '(--foreground --background)'--background
    '(--options -o)'{-o+,--options=}:' ':'{_example_start__-o}'
    '(- *)'{-h,--help}
    1:command1:'((start launch view-log))'
    2:command:_command_names
    '*'::argument:_normal
  )
  _example__query has_option WITH_INCOMPLETE --deprecated-option &&\
    args+=('(--deprecated-option)'--deprecated-option)
  _example__query has_option --background &&\
    args+=('(--pid-file)'--pid-file=:' ':_files)
  _arguments -S -s -w "${args[@]}"
}

_example_view_log() {
  local -a args=(
    '(--pager)'--pager=-:command:_command_names
    '(- *)'{-h,--help}
    1:command1:'((start launch view-log))'
  )
  _arguments -S -s -w "${args[@]}"
}

zstyle ':completion:*:*:example:option--priority-*' sort false

_example "$@"

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