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 (467 lines) | stat: -rw-r--r-- 11,806 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
# 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