Revision d4b94fe8

b/autotools/build-bash-completion
182 182
    sw.DecIndent()
183 183
  sw.Write("}")
184 184

  
185
  # Params: <long options with equal sign> <all options>
186
  # Result variable: $optcur
187
  sw.Write("_ganeti_checkopt() {")
188
  sw.IncIndent()
189
  try:
190
    sw.Write("""if [[ -n "$1" && "$cur" == @($1) ]]; then""")
191
    sw.IncIndent()
192
    try:
193
      sw.Write("optcur=\"${cur#--*=}\"")
194
      sw.Write("return 0")
195
    finally:
196
      sw.DecIndent()
197
    sw.Write("""elif [[ -n "$2" && "$prev" == @($2) ]]; then""")
198
    sw.IncIndent()
199
    try:
200
      sw.Write("optcur=\"$cur\"")
201
      sw.Write("return 0")
202
    finally:
203
      sw.DecIndent()
204
    sw.Write("fi")
205

  
206
    sw.Write("return 1")
207
  finally:
208
    sw.DecIndent()
209
  sw.Write("}")
210

  
185 211

  
186 212
def WriteCompReply(sw, args, cur="\"$cur\""):
187 213
  sw.Write("""COMPREPLY=( $(compgen %s -- %s) )""", args, cur)
......
254 280
    if not values:
255 281
      return
256 282

  
257
    sw.Write("if [[ $COMP_CWORD -gt %s ]]; then", self.arg_offset + 1)
258
    sw.IncIndent()
259
    try:
260
      # --foo value
261
      sw.Write("""case "$prev" in""")
262
      for (choices, names) in values.iteritems():
263
        sw.Write("%s)", "|".join([utils.ShellQuote(i) for i in names]))
264
        sw.IncIndent()
265
        try:
266
          WriteCompReply(sw, "-W %s" % utils.ShellQuote(choices))
267
        finally:
268
          sw.DecIndent()
269
        sw.Write(";;")
270
      sw.Write("""esac""")
271
    finally:
272
      sw.DecIndent()
273
    sw.Write("""fi""")
283
    cur = "\"$optcur\""
274 284

  
275
    # --foo=value
276
    values_longopts = {}
285
    wrote_opt = False
277 286

  
278
    for (choices, names) in values.iteritems():
279
      longnames = [i for i in names if i.startswith("--")]
280
      if longnames:
281
        values_longopts[choices] = longnames
287
    for (suggest, allnames) in values.iteritems():
288
      longnames = [i for i in allnames if i.startswith("--")]
282 289

  
283
    if values_longopts:
284
      sw.Write("""case "$cur" in""")
285
      for (choices, names) in values_longopts.iteritems():
286
        sw.Write("%s)", "|".join([utils.ShellQuote(i) + "=*" for i in names]))
287
        sw.IncIndent()
288
        try:
289
          # Shell expression to get option value
290
          cur="\"${cur#--*=}\""
291
          WriteCompReply(sw, "-W %s" % utils.ShellQuote(choices), cur=cur)
292
        finally:
293
          sw.DecIndent()
294
        sw.Write(";;")
295
      sw.Write("""esac""")
290
      if wrote_opt:
291
        condcmd = "elif"
292
      else:
293
        condcmd = "if"
294

  
295
      sw.Write("%s _ganeti_checkopt %s %s; then", condcmd,
296
               utils.ShellQuote("|".join(["%s=*" % i for i in longnames])),
297
               utils.ShellQuote("|".join(allnames)))
298
      sw.IncIndent()
299
      try:
300
        WriteCompReply(sw, "-W %s" % utils.ShellQuote(suggest), cur=cur)
301
      finally:
302
        sw.DecIndent()
303

  
304
      wrote_opt = True
305

  
306
    if wrote_opt:
307
      sw.Write("fi")
308

  
309
    return
296 310

  
297 311
  def _CompleteArguments(self, sw):
298 312
    if not (self.opts or self.args):
......
418 432
    sw.Write("local "
419 433
             ' cur="${COMP_WORDS[COMP_CWORD]}"'
420 434
             ' prev="${COMP_WORDS[COMP_CWORD-1]}"'
421
             ' i first_arg_idx choices compgenargs arg_idx')
435
             ' i first_arg_idx choices compgenargs arg_idx optcur')
422 436

  
423 437
    # Useful for debugging:
424 438
    #sw.Write("echo cur=\"$cur\" prev=\"$prev\"")

Also available in: Unified diff