about summary refs log tree commit diff
path: root/Completion/Debian/_apt_arguments
blob: 70f17aa802ea48e344d1f86b1583144c39520f6b (plain) (blame)
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
#autoload

# usage: _apt_arguments funcname option-spec... -- rest-regex

local funcname
funcname="$1"
shift

typeset -A canonicalize options
local short_hasarg short_bool short_intlevel short_configfile short_arbitem
local long_hasarg long_bool long_intlevel long_configfile long_arbitem
local comp_hasarg=''
local opt opts type

while [[ 0 -lt $# && $1 != -- ]]; do
  opts="${1%%:*}"
  type="${1#*:}"

  case $type in
    bool) options[$opts]=1;;
    intlevel) options[$opts]=-1;;
    configfile) options[$opts]=1;;
    arbitem) options[$opts]=-1;;
    *) options[$opts]=1
       comp_hasarg="${comp_hasarg}$opts) $type;;
"
       type=hasarg;;
  esac

  for opt in ${(s:,:)=opts}; do
    canonicalize[$opt]="$opts"
    case $opt in
      --*) eval "long_$type=(\$long_$type ${opt#--})";;
      -?) eval "short_$type=(\$short_$type ${opt#-})";;
    esac
  done
  shift
done

# skip --
[[ 0 -lt $# ]] && shift

comp_hasarg="\
case \$current_option in
${comp_hasarg}esac"

local short_seq false true bool __bool_ intlevel word word1 nul
local comp_bool comp_intlevel comp_configfile comp_arbitem comp_long comp_opt
local regex_short regex_long regex_all

regex_all=("$@")

short_seq="(${(j:|:)short_bool}|${(j:|:)short_intlevel})#"

false=(no false without off disable)
true=(yes true with on enable)
bool=($false $true)

__bool_=(--${^bool}-)

intlevel='[0-9]##'

word=$'[^\0]#\0'
word1=$'[^\0]##\0'
nul=$'\0'

comp_bool='compadd "$expl_bool[@]" '"$bool"
comp_intlevel= #"_message 'intlevel'"
comp_configfile='_files "$expl_configfile[@]"'
comp_arbitem= #"_message 'Foo::Bar=bar'"

comp_short=\
'if [[ $PREFIX = -'"$short_seq"' ]]; then
  apt_consume_short ${PREFIX[2,-1]}
  tmp1=(${${(M)${(s:,:)${(kj:,:)options[(R)*~0]}}:#-?}#-})
  if [[ $PREFIX = - ]]; then
    compadd "$expl_opt[@]" - -${^tmp1}
  else
    tmp2=(-${^tmp1})
    compadd "$expl_opt[@]" -y "($tmp2)" - ${PREFIX}${^tmp1}
  fi
elif [[ -z "$PREFIX" ]]; then
  compadd "$expl_opt[@]" - ${(M)${(s:,:)${(kj:,:)options[(R)*~0]}}:#-?}
fi'

comp_long=\
'tmp1="${(j:|:)${(@)${(@M)${(@s:,:)${(@kj:,:)options[(R)*~0]}}:#--*}#--}}"
tmp2=(--${(M)^long_bool:#$~tmp1} --${(M)^long_intlevel:#$~tmp1})
compadd "$expl_opt[@]" - $tmp2
tmp2=(--${(M)^long_hasarg:#$~tmp1} --${(M)^long_configfile:#$~tmp1} --${(M)^long_arbitem:#$~tmp1})
compadd "$expl_opt[@]" -S= - $tmp2
compadd "$expl_opt[@]" -S "" - '"$__bool_"

comp_long_prefix=\
'tmp1="${(j:|:)${(@)${(@M)${(@s:,:)${(@kj:,:)options[(R)*~0]}}:#--*}#--}}"
tmp2=($_ra_left${(M)^long_bool:#$~tmp1} $_ra_left${(M)^long_intlevel:#$~tmp1})
compadd "$expl_opt[@]" - $tmp2
tmp2=($_ra_left${(M)^long_hasarg:#$~tmp1} $_ra_left${(M)^long_configfile:#$~tmp1} $_ra_left${(M)^long_arbitem:#$~tmp1})
compadd "$expl_opt[@]" -S= - $tmp2
tmp1="${(j:|:)${(@)${(@M)${(@s:,:)${(@kj:,:)options[(R)*~0]}}:#-?}#-}}"
tmp2=($_ra_left${(M)^short_bool:#$~tmp1} $_ra_left${(M)^short_intlevel:#$~tmp1})
compadd "$expl_opt[@]" - $tmp2
tmp2=($_ra_left${(M)^short_hasarg:#$~tmp1} $_ra_left${(M)^short_configfile:#$~tmp1} $_ra_left${(M)^short_arbitem:#$~tmp1})
compadd "$expl_opt[@]" -S= - $tmp2'

comp_opt='[[ -prefix - || -z "$compconfig[option_prefix]" ]]'" && { $comp_short; $comp_long }"

regex_short=()
regex_long=()
regex_long_prefix=()

if (( $#short_hasarg )); then
  regex_short=("$regex_short[@]"
    /"$short_seq(${(j:|:)short_hasarg})(=|)"
      -'apt_consume_short ${_ra_match%=}; current_option=${canonicalize[-${${_ra_match%=}[-1]}]}'
      \( /"$word1" !"$comp_hasarg" \| /"$nul" /"$word" !"$comp_hasarg" \) \|
  )
  regex_long_prefix=("$regex_long_prefix[@]"
    /"(${(j:|:)short_hasarg})$nul"
      -'apt_consume_short ${_ra_match[-2]}; current_option=${canonicalize[-${${_ra_match%$nul}[-1]}]}'
      /"$word" !"$comp_hasarg" \|
    /"(${(j:|:)short_hasarg})="
      -'apt_consume_short ${_ra_match[-2]}; current_option=${canonicalize[-${${_ra_match%=}[-1]}]}'
      \( /"$word1" !"$comp_hasarg" \| /"$nul" /"$word" !"$comp_hasarg" \) \|
  )
fi

if (( $#short_bool )); then
  regex_short=("$regex_short[@]"
    /"$short_seq(${(j:|:)short_bool})($nul(${(j:|:)bool})|(${(j:|:)bool})|)$nul"
      -"apt_consume_short \${_ra_match%%($nul(${(j:|:)bool})|(${(j:|:)bool})|)$nul}" \|
    /"$short_seq(${(j:|:)short_bool})="
      -"apt_consume_short \${_ra_match%=}"
      \( /"$word1" !"$comp_bool" \| /"$nul" /"$word" !"$comp_bool" \) \|
  )
  regex_long_prefix=("$regex_long_prefix[@]"
    /"(${(j:|:)short_bool})="
      -"apt_consume_short \${_ra_match[-2]}"
      \( /"$word1" !"$comp_bool" \| /"$nul" /"$word" !"$comp_bool" \) \|
    /"(${(j:|:)short_bool})$nul"
      -"apt_consume_short \${_ra_match[-2]}"
      /"((${(j:|:)bool})$nul|)" !"$comp_bool" \|
  )
fi

if (( $#short_intlevel )); then
  regex_short=("$regex_short[@]"
    /"$short_seq(${(j:|:)short_intlevel})($nul$intlevel|$intlevel|)$nul"
      -"apt_consume_short \${_ra_match%%($nul$intlevel|$intlevel|)$nul}" \|
    /"$short_seq(${(j:|:)short_intlevel})="
      -"apt_consume_short \${_ra_match%=}"
      \( /"$word1" !"$comp_intlevel" \| /"$nul" /"$word" !"$comp_intlevel" \) \|
  )
  regex_long_prefix=("$regex_long_prefix[@]"
    /"(${(j:|:)short_intlevel})="
      -"apt_consume_short \${_ra_match[-2]}"
      \( /"$word1" !"$comp_intlevel" \| /"$nul" /"$word" !"$comp_intlevel" \) \|
    /"(${(j:|:)short_intlevel})$nul"
      -"apt_consume_short \${_ra_match[-2]}"
      /"($intlevel$nul|)" !"$comp_intlevel" \|
  )
fi

if (( $#short_configfile )); then
  regex_short=("$regex_short[@]"
    /"$short_seq(${(j:|:)short_configfile})(=|)"
      -"apt_consume_short \${_ra_match%=}"
      \( /"$word1" !"$comp_configfile" \| /"$nul" /"$word" !"$comp_configfile" \) \|
  )
  regex_long_prefix=("$regex_long_prefix[@]"
    /"(${(j:|:)short_configfile})$nul"
      -"apt_consume_short \${_ra_match[-2]}"
      /"$word" !"$comp_configfile" \|
    /"(${(j:|:)short_configfile})="
      -"apt_consume_short \${_ra_match[-2]}"
      \( /"$word1" !"$comp_configfile" \| /"$nul" /"$word" !"$comp_configfile" \) \|
  )
fi

if (( $#short_arbitem )); then
  regex_short=("$regex_short[@]"
    /"$short_seq(${(j:|:)short_arbitem})(=|)"
      -"apt_consume_short \${_ra_match%=}"
      \( /"$word1" !"$comp_arbitem" \| /"$nul" /"$word" !"$comp_arbitem" \) \|
  )
  regex_long_prefix=("$regex_long_prefix[@]"
    /"(${(j:|:)short_arbitem})$nul"
      -"apt_consume_short \${_ra_match[-2]}"
      /"$word" !"$comp_arbitem" \|
    /"(${(j:|:)short_arbitem})="
      -"apt_consume_short \${_ra_match[-2]}"
      \( /"$word1" !"$comp_arbitem" \| /"$nul" /"$word" !"$comp_arbitem" \) \|
  )
fi

if (( $#long_hasarg )); then
  regex_long=("$regex_long[@]"
    /"(${(j:|:)long_hasarg})$nul"
      -"apt_consume_long \${_ra_match%$nul}; current_option=\${canonicalize[--\${_ra_match%$nul}]}"
      /"$word" !"$comp_hasarg" \|
    /"(${(j:|:)long_hasarg})="
      -"apt_consume_long \${_ra_match%=}; current_option=\${canonicalize[--\${_ra_match%=}]}"
      \( /"$word1" !"$comp_hasarg" \| /"$nul" /"$word" !"$comp_hasarg" \) \|
  )
  regex_long_prefix=("$regex_long_prefix[@]"
    /"(${(j:|:)long_hasarg})$nul"
      -"apt_consume_long \${_ra_match%$nul}; current_option=\${canonicalize[--\${_ra_match%$nul}]}"
      /"$word" !"$comp_hasarg" \|
    /"(${(j:|:)long_hasarg})="
      -"apt_consume_long \${_ra_match%=}; current_option=\${canonicalize[--\${_ra_match%=}]}"
      \( /"$word1" !"$comp_hasarg" \| /"$nul" /"$word" !"$comp_hasarg" \) \|
  )
fi

if (( $#long_bool )); then
  regex_long=("$regex_long[@]"
    /"(${(j:|:)long_bool})="
      -"apt_consume_long \${_ra_match%=}"
      \( /"$word1" !"$comp_bool" \| /"$nul" /"$word" !"$comp_bool" \) \|
    /"(${(j:|:)long_bool})$nul"
      -"apt_consume_long \${_ra_match%$nul}"
      /"((${(j:|:)bool})$nul|)" !"$comp_bool" \|
  )
  regex_long_prefix=("$regex_long_prefix[@]"
    /"(${(j:|:)long_bool})="
      -"apt_consume_long \${_ra_match%=}"
      \( /"$word1" !"$comp_bool" \| /"$nul" /"$word" !"$comp_bool" \) \|
    /"(${(j:|:)long_bool})$nul"
      -"apt_consume_long \${_ra_match%$nul}"
      /"((${(j:|:)bool})$nul|)" !"$comp_bool" \|
  )
fi

if (( $#long_intlevel )); then
  regex_long=("$regex_long[@]"
    /"(${(j:|:)long_intlevel})="
      -"apt_consume_long \${_ra_match%=}"
      \( /"$word1" !"$comp_intlevel" \| /"$nul" /"$word" !"$comp_intlevel" \) \|
    /"(${(j:|:)long_intlevel})$nul"
      -"apt_consume_long \${_ra_match%$nul}"
      /"($intlevel$nul|)" !"$comp_intlevel" \|
  )
  regex_long_prefix=("$regex_long_prefix[@]"
    /"(${(j:|:)long_intlevel})$nul"
      -"apt_consume_long \${_ra_match%$nul}"
      /"$intlevel" !"$comp_intlevel" /"$nul" \|
    /"(${(j:|:)long_intlevel})="
      -"apt_consume_long \${_ra_match%=}"
      \( /"$word1" !"$comp_intlevel" \| /"$nul" /"$word" !"$comp_intlevel" \) \|
    /"(${(j:|:)long_intlevel})$nul"
      -"apt_consume_long \${_ra_match%$nul}"
      /"($intlevel$nul|)" !"$comp_intlevel" \|
  )
fi

if (( $#long_configfile )); then
  regex_long=("$regex_long[@]"
    /"(${(j:|:)long_configfile})$nul"
      -"apt_consume_long \${_ra_match%$nul}"
      /"$word" !"$comp_configfile" \|
    /"(${(j:|:)long_configfile})="
      -"apt_consume_long \${_ra_match%=}"
      \( /"$word1" !"$comp_configfile" \| /"$nul" /"$word" !"$comp_configfile" \) \|
  )
  regex_long_prefix=("$regex_long_prefix[@]"
    /"(${(j:|:)long_configfile})$nul"
      -"apt_consume_long \${_ra_match%$nul}"
      /"$word" !"$comp_configfile" \|
    /"(${(j:|:)long_configfile})="
      -"apt_consume_long \${_ra_match%=}"
      \( /"$word1" !"$comp_configfile" \| /"$nul" /"$word" !"$comp_configfile" \) \|
  )
fi

if (( $#long_arbitem )); then
  regex_long=("$regex_long[@]"
    /"(${(j:|:)long_arbitem})$nul"
      -"apt_consume_long \${_ra_match%$nul}"
      /"$word" !"$comp_arbitem" \|
    /"(${(j:|:)long_arbitem})="
      -"apt_consume_long \${_ra_match%=}"
      \( /"$word1" !"$comp_arbitem" \| /"$nul" /"$word" !"$comp_arbitem" \) \|
  )
  regex_long_prefix=("$regex_long_prefix[@]"
    /"(${(j:|:)long_arbitem})$nul"
      -"apt_consume_long \${_ra_match%$nul}"
      /"$word" !"$comp_arbitem" \|
    /"(${(j:|:)long_arbitem})="
      -"apt_consume_long \${_ra_match%=}"
      \( /"$word1" !"$comp_arbitem" \| /"$nul" /"$word" !"$comp_arbitem" \) \|
  )
fi

regex_all=(
  /"$word"
  \( %-- \( "$regex_long[@]"
	    %"(${(j:|:)bool})-"
	    \( "$regex_long_prefix[@]" /"[]" !"$comp_long_prefix" \) \|
	    /"[]" !"$comp_long" \) \|
     %- \( "$regex_short[@]" /"[]" !"$comp_short; $comp_long" \) \|
     /"[]" !"$comp_opt" \) \#
  "$regex_all[@]"
)

apt_consume_short () {
  local short opt
  for short in ${(s::)1}; do
    opt="$canonicalize[-$short]"
    (( 0 < options[$opt] && options[$opt]-- ))
  done
}

apt_consume_long () {
  local long opt
  opt="$canonicalize[--$1]"
  (( 0 < options[$opt] && options[$opt]-- ))
}

_regex_arguments "${funcname}_sm" "$regex_all[@]"

eval "$funcname () {
  typeset -A canonicalize options
  canonicalize=(${(kv)canonicalize})
  options=(${(kv)options})

  local short_hasarg short_bool short_intlevel short_configfile short_arbitem
  local long_hasarg long_bool long_intlevel long_configfile long_arbitem
  short_hasarg=($short_hasarg)
  short_bool=($short_bool)
  short_intlevel=($short_intlevel)
  short_configfile=($short_configfile)
  short_arbitem=($short_arbitem)
  long_hasarg=($long_hasarg)
  long_bool=($long_bool)
  long_intlevel=($long_intlevel)
  long_configfile=($long_configfile)
  long_arbitem=($long_arbitem)

  local expl_opt expl_bool expl_configfile
  _description expl_opt option
  _description expl_bool 'bool value'
  _description expl_configfile 'config file'

  local current_option tmp1 tmp2

  ${funcname}_sm
}"