Git Product home page Git Product logo

Comments (1)

mhils avatar mhils commented on June 17, 2024

Ok, turns out my initial patching attempt was incomplete. This works and fixes the issue:

diff --git a/click_completion/core.py b/click_completion/core.py
index 867085e..4f83e48 100644
--- a/click_completion/core.py
+++ b/click_completion/core.py
@@ -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:
diff --git a/click_completion/lib.py b/click_completion/lib.py
index fc195cd..ddee5da 100644
--- a/click_completion/lib.py
+++ b/click_completion/lib.py
@@ -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 ")

There's the obvious question if the same sort of fix should be applied to the other shells as well. Any thoughts?

from click-completion.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.