Merge pull request #1709 from SamSchott/shell-completion-espace-handling
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Shell completion escape handling
authored by David Lord · committed by GitHub · parents f0cc87d, f7cd0ff · Browse files · 3 files·+50 added−17 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+50 added−17 removed
- tests:+17 added−0 removed
- other:+33 added−17 removed
Provenance
- Commit
9d8ad23- Tree
8d7fb17- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
f0cc87d- Parent 2
f7cd0ff
3 files changed+50 added−17 removed
modified:src/click/parser.py+31 added-17 removed
19 unmodified lines20 # This code uses parts of optparse written by Gregory P. Ward and21 # maintained by the Python Software Foundation.22 # Copyright 2001-2006 Gregory P. Ward23 # Copyright 2002-2006 Python Software Foundation24-import re24 from collections import deque25 26 from .exceptions import BadArgumentUsage27 from .exceptions import BadOptionUsage73 unmodified lines101 return f"{prefix}{ctx.token_normalize_func(opt)}"102 103 104 def split_arg_string(string):106- """Given an argument string this attempts to split it into small parts."""107- rv = []108- for match in re.finditer(109- r"('([^'\\]*(?:\\.[^'\\]*)*)'|\"([^\"\\]*(?:\\.[^\"\\]*)*)\"|\S+)\s*",110- string,111- re.S,112- ):113- arg = match.group().strip()114- if arg[:1] == arg[-1:] and arg[:1] in "\"'":115- arg = arg[1:-1].encode("ascii", "backslashreplace").decode("unicode-escape")116- try:117- arg = type(string)(arg)118- except UnicodeError:119- pass120- rv.append(arg)121- return rv105+ """Split an argument string as with :func:`shlex.split`, but don't106+ fail if the string is incomplete. Ignores a missing closing quote or107+ incomplete escape sequence and uses the partial token as-is.108 109+ .. code-block:: python110 111+ split_arg_string("example 'my file")112+ ["example", "my file"]113+114+ split_arg_string("example my\\")115+ ["example", "my"]116+117+ :param string: String to split.118+ """119+ import shlex120+121+ lex = shlex.shlex(string, posix=True)122+ lex.whitespace_split = True123+ lex.commenters = ""124+ out = []125+126+ try:127+ for token in lex:128+ out.append(token)129+ except ValueError:130+ # Raised when end-of-string is reached in an invalid state. Use131+ # the partial token as-is. The quote or escape character is in132+ # lex.state, not lex.token.133+ out.append(lex.token)134+135+ return out136+137+138 class Option:139 def __init__(self, opts, dest, action=None, nargs=1, const=None, obj=None):140 self._short_opts = []141 self._long_opts = []327 unmodified linesadded:tests/test_parser.py+17 added
1+import pytest2+3+from click.parser import split_arg_string4+5+6+@pytest.mark.parametrize(7+ ("value", "expect"),8+ [9+ ("cli a b c", ["cli", "a", "b", "c"]),10+ ("cli 'my file", ["cli", "my file"]),11+ ("cli 'my file'", ["cli", "my file"]),12+ ("cli my\\", ["cli", "my"]),13+ ("cli my\\ file", ["cli", "my file"]),14+ ],15+)16+def test_split_arg_string(value, expect):17+ assert split_arg_string(value) == expectmodified:CHANGES.rst+2 added
105 unmodified lines106 - The env var values used to start completion have changed order.107 The shell now comes first, such as ``{shell}_source`` rather108 than ``source_{shell}``, and is always required.109 110+- Completion correctly parses command line strings with incomplete111+ quoting or escape sequences. :issue:`1708`112 - Extra context settings (``obj=...``, etc.) are passed on to the113 completion system. :issue:`942`114 - Include ``--help`` option in completion. :pr:`1504`115 - ``ParameterSource`` is an ``enum.Enum`` subclass. :issue:`1530`681 unmodified lines