Merge pull request #580 from josephfrazier/bash-command-substitution

bash: fix parsing of command substitution
This commit is contained in:
Vladimir Iakovlev
2016-11-30 15:49:47 +01:00
committed by GitHub
4 changed files with 32 additions and 3 deletions
+11
View File
@@ -1,4 +1,5 @@
import os
import bashlex
from ..conf import settings
from ..utils import memoize
from .generic import Generic
@@ -45,3 +46,13 @@ class Bash(Generic):
else:
config = 'bash config'
return 'eval $(thefuck --alias)', config
def split_command(self, command):
generic = Generic()
# If bashlex fails for some reason, fallback to shlex
# See https://github.com/idank/bashlex#limitations
try:
return generic.decode_utf8(list(bashlex.split(generic.encode_utf8(command))))
except:
return generic.split_command(command)
+10 -2
View File
@@ -65,9 +65,17 @@ class Generic(object):
def split_command(self, command):
"""Split the command using shell-like syntax."""
return self.decode_utf8(shlex.split(self.encode_utf8(command)))
def encode_utf8(self, command):
if six.PY2:
return [s.decode('utf8') for s in shlex.split(command.encode('utf8'))]
return shlex.split(command)
return command.encode('utf8')
return command
def decode_utf8(self, command_parts):
if six.PY2:
return [s.decode('utf8') for s in command_parts]
return command_parts
def quote(self, s):
"""Return a shell-escaped version of the string s."""