Skip to content
Myr

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

3 files+50 added−17 removed

modified:src/click/parser.py+31 added-17 removed
19 unmodified lines
20# 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 Foundation24import re24from collections import deque2526from .exceptions import BadArgumentUsage27from .exceptions import BadOptionUsage
73 unmodified lines
101 return f"{prefix}{ctx.token_normalize_func(opt)}"102103104def 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.108109 .. code-block:: python110111 split_arg_string("example 'my file")112 ["example", "my file"]113114 split_arg_string("example my\\")115 ["example", "my"]116117 :param string: String to split.118 """119 import shlex120121 lex = shlex.shlex(string, posix=True)122 lex.whitespace_split = True123 lex.commenters = ""124 out = []125126 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)134135 return out136137138class 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 lines
added:tests/test_parser.py+17 added
1import pytest23from click.parser import split_arg_string456@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)16def test_split_arg_string(value, expect):17 assert split_arg_string(value) == expect
modified:CHANGES.rst+2 added
105 unmodified lines
106 - 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.109110- 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