@@ -288,6 +288,7 @@ def bash_completions(
288288 paths = None ,
289289 command = None ,
290290 quote_paths = _bash_quote_paths ,
291+ arg_index = None ,
291292 ** kwargs
292293):
293294 """Completes based on results from BASH completion.
@@ -320,7 +321,9 @@ def bash_completions(
320321 this as the default is acceptable 99+% of the time. This function should
321322 return a set of the new paths and a boolean for whether the paths were
322323 quoted.
323-
324+ arg_index : int, optional
325+ The current prefix's index in the args.
326+
324327 Returns
325328 -------
326329 rtn : set of str
@@ -336,24 +339,27 @@ def bash_completions(
336339 splt = line .split ()
337340 cmd = splt [0 ]
338341 cmd = os .path .basename (cmd )
339- idx = n = 0
340342 prev = ""
341- for n , tok in enumerate (splt ):
342- if tok == prefix :
343- idx = line .find (prefix , idx )
344- if idx >= begidx :
345- break
346- prev = tok
347-
348- if len (prefix ) == 0 :
349- prefix_quoted = '""'
350- n += 1
343+ if arg_index is not None :
344+ n = arg_index
345+ if arg_index > 0 :
346+ prev = splt [arg_index - 1 ]
351347 else :
352- prefix_quoted = shlex .quote (prefix )
348+ # find `n` and `prev` by ourselves
349+ idx = n = 0
350+ for n , tok in enumerate (splt ): # noqa
351+ if tok == prefix :
352+ idx = line .find (prefix , idx )
353+ if idx >= begidx :
354+ break
355+ prev = tok
356+ if len (prefix ) == 0 :
357+ n += 1
358+ prefix_quoted = shlex .quote (prefix )
353359
354360 script = BASH_COMPLETE_SCRIPT .format (
355361 source = source ,
356- line = " " .join (shlex .quote (p ) for p in splt ),
362+ line = " " .join (shlex .quote (p ) for p in splt if p ),
357363 comp_line = shlex .quote (line ),
358364 n = n ,
359365 cmd = shlex .quote (cmd ),
0 commit comments