Skip to content

Commit

Permalink
initial fix for click-contrib#32
Browse files Browse the repository at this point in the history
  • Loading branch information
mhils committed Oct 19, 2019
1 parent 6e08a5f commit 453042d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
8 changes: 2 additions & 6 deletions click_completion/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,8 @@ def do_fish_complete(cli, prog_name):
True if the completion was successful, False otherwise
"""
commandline = os.environ['COMMANDLINE']
args = split_args(commandline)[1:]
if args and not commandline.endswith(' '):
incomplete = args[-1]
args = args[:-1]
else:
incomplete = ''
args, incomplete = split_args(commandline)
args = args[1:]

for item, help in get_choices(cli, prog_name, args, incomplete):
if help:
Expand Down
26 changes: 19 additions & 7 deletions click_completion/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,23 +101,35 @@ def split_args(line):
Returns
-------
[str]
The line split in separated arguments
[str], str
The line split in separated arguments, plus the last incomplete argument (if any)
"""
lex = shlex.shlex(line, posix=True)
lex.whitespace_split = True
lex.commenters = ''
res = []
last_state = lex.state
try:
while True:
last_state = lex.state
res.append(next(lex))
except ValueError: # No closing quotation
pass
return res, lex.token
except StopIteration: # End of loop
pass
if lex.token:
res.append(lex.token)
return res
if last_state is None:
return res[:-1], res[-1]
else:
return res, ''


def test_split_args():
assert split_args("foo bar") == (["foo"], "bar")
assert split_args("foo bar ") == (["foo", "bar"], "")
assert split_args("foo 'bar") == (["foo"], "bar")
assert split_args("foo 'bar ") == (["foo"], "bar ")
assert split_args("foo 'bar baz'") == (["foo"], "bar baz")
assert split_args("foo 'bar baz' ") == (["foo", "bar baz"], "")
assert split_args("foo bar\\ ") == (["foo"], "bar ")


def get_auto_shell():
Expand Down

0 comments on commit 453042d

Please sign in to comment.