diff --git a/commitizen/cli.py b/commitizen/cli.py index 2f847ef8cd..45aa33f303 100644 --- a/commitizen/cli.py +++ b/commitizen/cli.py @@ -3,6 +3,7 @@ import sys from functools import partial +import argcomplete from decli import cli from commitizen import commands, config @@ -265,6 +266,7 @@ def main(): conf = config.read_cfg() parser = cli(data) + argcomplete.autocomplete(parser) # Show help if no arg provided if len(sys.argv) == 1: parser.print_help(sys.stderr) diff --git a/docs/README.md b/docs/README.md index 4b87077126..77205dfc1a 100644 --- a/docs/README.md +++ b/docs/README.md @@ -144,6 +144,32 @@ commands: current project (default: installed commitizen) ``` +## Setting up bash completion + +When using bash as your shell (limited support for zsh, fish, and tcsh is available), Commitizen can use [argcomplete](https://kislyuk.github.io/argcomplete/) for auto-completion. For this argcomplete needs to be enabled. + +argcomplete is installed when you install Commitizen since it's a dependency. + +If Commitizen is installed globally, global activation can be executed: + +```bash +sudo activate-global-python-argcomplete +``` + +For permanent (but not global) Commitizen activation, use: + +```bash +register-python-argcomplete cz >> ~/.bashrc +``` + +For one-time activation of argcomplete for Commitizen only, use: + +```bash +eval "$(register-python-argcomplete cz)" +``` + +For further information on activation, please visit the [argcomplete website](https://kislyuk.github.io/argcomplete/). + ## Third-Party Commitizen Templates See [Third-Party Commitizen Templates](third-party-commitizen.md). diff --git a/pyproject.toml b/pyproject.toml index 39f09b3f69..9eadaf7fa7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -53,6 +53,7 @@ packaging = ">=19,<21" tomlkit = "^0.5.3" jinja2 = "^2.10.3" pyyaml = ">=3.08" +argcomplete = "^1.12.1" [tool.poetry.dev-dependencies] ipython = "^7.2" diff --git a/tests/test_cli.py b/tests/test_cli.py index 7935806be2..2b73bbd5cf 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,3 +1,4 @@ +import subprocess import sys import pytest @@ -78,3 +79,16 @@ def test_commitizen_debug_excepthook(capsys): assert excinfo.type == SystemExit assert excinfo.value.code == NotAGitProjectError.exit_code assert "NotAGitProjectError" in str(excinfo.traceback[0]) + + +def test_argcomplete_activation(): + """ + This function is testing the one-time activation of argcomplete for + commitizen only. + + Equivalent to run: + $ eval "$(register-python-argcomplete pytest)" + """ + output = subprocess.run(["register-python-argcomplete", "cz"]) + + assert output.returncode == 0