1
0
mirror of https://github.com/nvbn/thefuck.git synced 2025-02-07 13:41:21 +00:00

Parse command line with shlex

I put that in shells so that weird shells might try to parse it
differently.
This commit is contained in:
mcarton 2015-10-28 14:01:14 +01:00
parent 346cb99217
commit dae58211ba
3 changed files with 39 additions and 16 deletions

View File

@ -1,6 +1,6 @@
"""Module with shell specific actions, each shell class should """Module with shell specific actions, each shell class should
implement `from_shell`, `to_shell`, `app_alias`, `put_to_history` and `get_aliases` implement `from_shell`, `to_shell`, `app_alias`, `put_to_history` and
methods. `get_aliases` methods.
""" """
from collections import defaultdict from collections import defaultdict
@ -9,6 +9,7 @@ from subprocess import Popen, PIPE
from time import time from time import time
import io import io
import os import os
import shlex
from .utils import DEVNULL, memoize, cache from .utils import DEVNULL, memoize, cache
@ -75,6 +76,10 @@ class Generic(object):
def how_to_configure(self): def how_to_configure(self):
return return
def split_command(self, command):
"""Split the command using shell-like syntax."""
return shlex.split(command)
class Bash(Generic): class Bash(Generic):
def app_alias(self, fuck): def app_alias(self, fuck):
@ -285,9 +290,14 @@ def get_aliases():
return list(_get_shell().get_aliases().keys()) return list(_get_shell().get_aliases().keys())
def split_command(command):
return _get_shell().split_command(command)
@memoize @memoize
def get_history(): def get_history():
return list(_get_shell().get_history()) return list(_get_shell().get_history())
def how_to_configure(): def how_to_configure():
return _get_shell().how_to_configure() return _get_shell().how_to_configure()

View File

@ -1,13 +1,13 @@
from imp import load_source
import os
from subprocess import Popen, PIPE
import sys
from psutil import Process, TimeoutExpired
import six
from .conf import settings, DEFAULT_PRIORITY, ALL_ENABLED
from .utils import compatibility_call
from .exceptions import EmptyCommand
from . import logs, shells from . import logs, shells
from .conf import settings, DEFAULT_PRIORITY, ALL_ENABLED
from .exceptions import EmptyCommand
from .utils import compatibility_call
from imp import load_source
from psutil import Process, TimeoutExpired
from subprocess import Popen, PIPE
import os
import six
import sys
class Command(object): class Command(object):
@ -21,10 +21,23 @@ class Command(object):
:type stderr: basestring :type stderr: basestring
""" """
self.script = script self._script = script
self.stdout = stdout self.stdout = stdout
self.stderr = stderr self.stderr = stderr
try:
self._split_script = shells.split_command(script)
except:
self._split_script = None
@property
def script(self):
return self._script
@property
def split_script(self):
return self._split_script
def __eq__(self, other): def __eq__(self, other):
if isinstance(other, Command): if isinstance(other, Command):
return (self.script, self.stdout, self.stderr) \ return (self.script, self.stdout, self.stderr) \

View File

@ -146,10 +146,10 @@ def replace_command(command, broken, matched):
@memoize @memoize
def is_app(command, *app_names): def is_app(command, *app_names):
"""Returns `True` if command is call to one of passed app names.""" """Returns `True` if command is call to one of passed app names."""
for name in app_names: if command.split_script is not None and len(command.split_script) > 0:
if command.script == name \ app = command.split_script[0]
or command.script.startswith(u'{} '.format(name)): return app in app_names
return True
return False return False