From dae58211ba00f3d38fc7e690589512641108dd17 Mon Sep 17 00:00:00 2001 From: mcarton Date: Wed, 28 Oct 2015 14:01:14 +0100 Subject: [PATCH] Parse command line with shlex I put that in shells so that weird shells might try to parse it differently. --- thefuck/shells.py | 14 ++++++++++++-- thefuck/types.py | 33 +++++++++++++++++++++++---------- thefuck/utils.py | 8 ++++---- 3 files changed, 39 insertions(+), 16 deletions(-) diff --git a/thefuck/shells.py b/thefuck/shells.py index 0bbe0117..cdded259 100644 --- a/thefuck/shells.py +++ b/thefuck/shells.py @@ -1,6 +1,6 @@ """Module with shell specific actions, each shell class should -implement `from_shell`, `to_shell`, `app_alias`, `put_to_history` and `get_aliases` -methods. +implement `from_shell`, `to_shell`, `app_alias`, `put_to_history` and +`get_aliases` methods. """ from collections import defaultdict @@ -9,6 +9,7 @@ from subprocess import Popen, PIPE from time import time import io import os +import shlex from .utils import DEVNULL, memoize, cache @@ -75,6 +76,10 @@ class Generic(object): def how_to_configure(self): return + def split_command(self, command): + """Split the command using shell-like syntax.""" + return shlex.split(command) + class Bash(Generic): def app_alias(self, fuck): @@ -285,9 +290,14 @@ def get_aliases(): return list(_get_shell().get_aliases().keys()) +def split_command(command): + return _get_shell().split_command(command) + + @memoize def get_history(): return list(_get_shell().get_history()) + def how_to_configure(): return _get_shell().how_to_configure() diff --git a/thefuck/types.py b/thefuck/types.py index 44f6aeeb..779829bd 100644 --- a/thefuck/types.py +++ b/thefuck/types.py @@ -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 .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): @@ -21,10 +21,23 @@ class Command(object): :type stderr: basestring """ - self.script = script + self._script = script self.stdout = stdout 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): if isinstance(other, Command): return (self.script, self.stdout, self.stderr) \ diff --git a/thefuck/utils.py b/thefuck/utils.py index 51b9a113..c9c4e04a 100644 --- a/thefuck/utils.py +++ b/thefuck/utils.py @@ -146,10 +146,10 @@ def replace_command(command, broken, matched): @memoize def is_app(command, *app_names): """Returns `True` if command is call to one of passed app names.""" - for name in app_names: - if command.script == name \ - or command.script.startswith(u'{} '.format(name)): - return True + if command.split_script is not None and len(command.split_script) > 0: + app = command.split_script[0] + return app in app_names + return False