From 81042514c8204d159283e85651d35c20f20c7863 Mon Sep 17 00:00:00 2001 From: Vladimir Iakovlev Date: Tue, 22 May 2018 19:01:51 +0200 Subject: [PATCH] Squashed commit of the following: commit 6919161e77a39b9bd59ca54eac44b956cd3ae1dc Author: Vladimir Iakovlev Date: Tue May 22 19:01:33 2018 +0200 #810: Fix code style commit ebbb31a3227ce32ba5288e96c0c16a3d334c45d6 Merge: 2df1a5a a2799ad Author: Vladimir Iakovlev Date: Tue May 22 18:59:56 2018 +0200 Merge branch 'feature/long-form-help' of https://github.com/jakewarren/thefuck into jakewarren-feature/long-form-help commit a2799ad09894808fc23ef1a99475ca30c3d4d67c Author: Jake Warren Date: Mon May 7 14:12:57 2018 -0500 Add new `long_form_help` rule --- README.md | 1 + tests/rules/test_long_form_help.py | 22 ++++++++++++++++++++++ thefuck/rules/long_form_help.py | 27 +++++++++++++++++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 tests/rules/test_long_form_help.py create mode 100644 thefuck/rules/long_form_help.py diff --git a/README.md b/README.md index a0c8671..e9c419b 100644 --- a/README.md +++ b/README.md @@ -244,6 +244,7 @@ following rules are enabled by default: * `java` – removes `.java` extension when running Java programs; * `javac` – appends missing `.java` when compiling Java files; * `lein_not_task` – fixes wrong `lein` tasks like `lein rpl`; +* `long_form_help` – changes `-h` to `--help` when the short form version is not supported * `ln_no_hard_link` – catches hard link creation on directories, suggest symbolic link; * `ln_s_order` – fixes `ln -s` arguments order; * `ls_all` – adds `-A` to `ls` when output is empty; diff --git a/tests/rules/test_long_form_help.py b/tests/rules/test_long_form_help.py new file mode 100644 index 0000000..e3a12be --- /dev/null +++ b/tests/rules/test_long_form_help.py @@ -0,0 +1,22 @@ +import pytest +from thefuck.rules.long_form_help import match, get_new_command +from thefuck.types import Command + + +@pytest.mark.parametrize('output', [ + 'Try \'grep --help\' for more information.']) +def test_match(output): + assert match(Command('grep -h', output)) + + +def test_not_match(): + assert not match(Command('', '')) + + +@pytest.mark.parametrize('before, after', [ + ('grep -h', 'grep --help'), + ('tar -h', 'tar --help'), + ('docker run -h', 'docker run --help'), + ('cut -h', 'cut --help')]) +def test_get_new_command(before, after): + assert get_new_command(Command(before, '')) == after diff --git a/thefuck/rules/long_form_help.py b/thefuck/rules/long_form_help.py new file mode 100644 index 0000000..3ff7a23 --- /dev/null +++ b/thefuck/rules/long_form_help.py @@ -0,0 +1,27 @@ +from thefuck.utils import replace_argument +import re + +# regex to match a suggested help command from the tool output +help_regex = r"(?:Run|Try) '([^']+)'(?: or '[^']+')? for (?:details|more information)." + + +def match(command): + if re.search(help_regex, command.output, re.I) is not None: + return True + + if '--help' in command.output: + return True + + return False + + +def get_new_command(command): + if re.search(help_regex, command.output) is not None: + match_obj = re.search(help_regex, command.output, re.I) + return match_obj.group(1) + + return replace_argument(command.script, '-h', '--help') + + +enabled_by_default = True +priority = 5000