Repository: VirtusLab/git-machete
Pull request #704 (2022-10-20 14:08:32)

Issue #653: Accept command aliases with `help` command


Patch:
diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md
index 63bb018..4189fe5 100644
--- a/RELEASE_NOTES.md
+++ b/RELEASE_NOTES.md
@@ -3,6 +3,7 @@
 ## New in git-machete 3.12.6
 
 - improved: modify formatting of command line documentation
+- added: `git machete help <command>` now accepts command aliases
 
 ## New in git-machete 3.12.5
 
diff --git a/git_machete/cli.py b/git_machete/cli.py
index 373675b..f33ffc0 100644
--- a/git_machete/cli.py
+++ b/git_machete/cli.py
@@ -29,6 +29,8 @@ alias_by_command: Dict[str, str] = {
     "traverse": "t"
 }
 
+command_by_alias: Dict[str, str] = {v: k for k, v in alias_by_command.items()}
+
 command_groups: List[Tuple[str, List[str]]] = [
     ("General topics",
      ["config", "file", "format", "help", "hooks", "version"]),
@@ -44,11 +46,16 @@ command_groups: List[Tuple[str, List[str]]] = [
      ["github"])
 ]
 
+commands_and_aliases = list(long_docs.keys()) + list(alias_by_command.keys())
+
 
 def get_help_description(command: str = None) -> str:
     usage_str = ''
-    if command and command in long_docs:
-        usage_str += fmt(textwrap.dedent(long_docs[command]))
+    if command and command in commands_and_aliases:
+        if command in long_docs:
+            usage_str += fmt(textwrap.dedent(long_docs[command]))
+        elif command in command_by_alias:
+            usage_str += fmt(textwrap.dedent(long_docs[command_by_alias[command]]))
     else:
         usage_str += get_short_general_usage() + '\n'
         if command:
@@ -239,7 +246,7 @@ def create_cli_parser() -> argparse.ArgumentParser:
 
     help_parser = subparsers.add_parser(
         'help', add_help=False, usage=argparse.SUPPRESS, parents=[common_args_parser])
-    help_parser.add_argument('topic_or_cmd', nargs='?', choices=list(short_docs.keys()))
+    help_parser.add_argument('topic_or_cmd', nargs='?', choices=commands_and_aliases)
 
     is_managed_parser = subparsers.add_parser(
         'is-managed',


Test patch:
diff --git a/tests/test_cli.py b/tests/test_cli.py
new file mode 100644
index 0000000..02cc3eb
--- /dev/null
+++ b/tests/test_cli.py
@@ -0,0 +1,9 @@
+from git_machete.cli import alias_by_command
+
+
+class TestCLI:
+    def test_aliases(self) -> None:
+        """
+        Verify that each command alias is unique
+        """
+        assert len(alias_by_command.values()) == len(set(alias_by_command.values()))
diff --git a/tests/test_help.py b/tests/test_help.py
index 51ae13a..df4dcf9 100644
--- a/tests/test_help.py
+++ b/tests/test_help.py
@@ -2,7 +2,7 @@ from typing import Any, List
 
 import pytest
 
-from git_machete.generated_docs import long_docs
+from git_machete.cli import commands_and_aliases
 
 from unittest import mock
 
@@ -28,39 +28,39 @@ class TestHelp:
     def test_help(self, mocker: Any) -> None:
         mocker.patch('git_machete.utils.run_cmd', mock_run_cmd)  # to hide git outputs in tests
 
-        expected_exit_code = None
-        expected_exit_code_when_failed = 2
+        exit_when_success = None
+        exit_code_when_failed = 2
         help_topics: List[str] = ['config', 'format', 'hooks']
 
         with pytest.raises(SystemExit) as e:
             launch_command("help")
-        assert expected_exit_code == e.value.code, \
+        assert exit_when_success == e.value.code, \
             "Verify that `git machete help` causes SystemExit with " \
-            f"{expected_exit_code} exit code."
+            f"{exit_when_success} exit code."
 
-        for command in long_docs:
+        for command in commands_and_aliases:
 
             with pytest.raises(SystemExit) as e:
                 launch_command("help", command)
-            assert expected_exit_code == e.value.code, \
+            assert exit_when_success == e.value.code, \
                 f"Verify that `git machete help {command}` causes SystemExit" \
-                f" with {expected_exit_code} exit code."
+                f" with {exit_when_success} exit code."
 
             if command not in help_topics:
                 with pytest.raises(SystemExit) as e:
                     launch_command(command, "--help")
-                assert expected_exit_code == e.value.code, \
+                assert exit_when_success == e.value.code, \
                     f"Verify that `git machete {command} --help` causes " \
-                    f"SystemExit with {expected_exit_code} exit code."
+                    f"SystemExit with {exit_when_success} exit code."
             else:
                 with pytest.raises(SystemExit) as e:
                     launch_command(command, "--help")
-                assert expected_exit_code_when_failed == e.value.code, \
+                assert exit_code_when_failed == e.value.code, \
                     f"Verify that `git machete {command} --help` causes " \
-                    f"SystemExit with {expected_exit_code} exit code."
+                    f"SystemExit with {exit_when_success} exit code."
 
     @mock.patch('git_machete.cli.exit_script', mock_exit_script_no_exit)
     def test_help_output_has_no_ansi_codes(self) -> None:
-        for command in long_docs.keys():
+        for command in commands_and_aliases:
             help_output = launch_command('help', command)
             assert '\033' not in help_output