Fix escaping

This commit is contained in:
Joscha 2019-04-11 23:33:06 +00:00
parent 147ea92102
commit 40cb7917c8

View file

@ -38,6 +38,20 @@ class ArgumentData:
self._fancy_escaped: Optional[FancyArgs] = None self._fancy_escaped: Optional[FancyArgs] = None
def _split_escaped(self, text: str) -> List[str]: def _split_escaped(self, text: str) -> List[str]:
"""
Splits the string into individual arguments, while allowing
bash-inspired quoting/escaping.
A single backslash escapes the immediately following character.
Double quotes allow backslash escapes, but escape all other characters.
Single quotes escape all characters.
The remaining string is split at all unescaped while space characters
(using str.isspace), similar to str.split without any arguments.
"""
words: List[str] = [] words: List[str] = []
word: List[str] = [] word: List[str] = []
@ -49,10 +63,16 @@ class ArgumentData:
backslash = False backslash = False
word.append(char) word.append(char)
elif quotes is not None: elif quotes is not None:
if char == quotes: if quotes == "\"" and char == "\\":
backslash = True
elif char == quotes:
quotes = None quotes = None
else: else:
word.append(char) word.append(char)
elif char == "\\":
backslash = True
elif char in ["\"", "'"]:
quotes = char
elif char.isspace(): elif char.isspace():
if word: if word:
words.append("".join(word)) words.append("".join(word))