diff --git a/concore_cli/README.md b/concore_cli/README.md index 78f98b1..edf9734 100644 --- a/concore_cli/README.md +++ b/concore_cli/README.md @@ -126,6 +126,22 @@ Stops all running concore processes. Prompts for confirmation before proceeding. concore stop ``` +### `concore completion ` + +Prints a tab-completion script so `concore ` completes commands, options and option values (for example `--type`). Supported shells: `bash`, `zsh`, `fish`. + +**Setup (add one line to your shell startup file):** +```bash +# ~/.bashrc +eval "$(concore completion bash)" + +# ~/.zshrc +eval "$(concore completion zsh)" + +# ~/.config/fish/config.fish +concore completion fish | source +``` + ## Development Workflow 1. **Create a new project** diff --git a/concore_cli/cli.py b/concore_cli/cli.py index dc8fca3..7e6a489 100644 --- a/concore_cli/cli.py +++ b/concore_cli/cli.py @@ -10,6 +10,7 @@ from .commands.stop import stop_all from .commands.inspect import inspect_workflow from .commands.watch import watch_study +from .commands.completion import SHELLS, completion_script from .commands.doctor import doctor_check from .commands.setup import setup_concore from . import __version__ @@ -181,6 +182,13 @@ def watch(study_dir, interval, once): sys.exit(1) +@cli.command() +@click.argument("shell", type=click.Choice(SHELLS)) +def completion(shell): + """Print the tab-completion script for bash, zsh or fish""" + click.echo(completion_script(cli, shell), nl=False) + + @cli.command() def doctor(): """Check system readiness for running concore studies""" diff --git a/concore_cli/commands/completion.py b/concore_cli/commands/completion.py new file mode 100644 index 0000000..90ac541 --- /dev/null +++ b/concore_cli/commands/completion.py @@ -0,0 +1,13 @@ +from click.shell_completion import get_completion_class + +SHELLS = ("bash", "zsh", "fish") +PROG_NAME = "concore" +COMPLETE_VAR = "_CONCORE_COMPLETE" + + +def completion_script(cli, shell): + """Return the shell completion script for the concore command.""" + completion_class = get_completion_class(shell) + if completion_class is None: + raise ValueError(f"Unsupported shell '{shell}' (use bash, zsh or fish)") + return completion_class(cli, {}, PROG_NAME, COMPLETE_VAR).source() diff --git a/tests/test_completion_command.py b/tests/test_completion_command.py new file mode 100644 index 0000000..df1cc38 --- /dev/null +++ b/tests/test_completion_command.py @@ -0,0 +1,40 @@ +import pytest +from click.shell_completion import get_completion_class +from click.testing import CliRunner + +from concore_cli.cli import cli + + +@pytest.mark.parametrize("shell", ["bash", "zsh", "fish"]) +def test_completion_prints_script_for_each_shell(shell): + result = CliRunner().invoke(cli, ["completion", shell]) + assert result.exit_code == 0 + assert "_CONCORE_COMPLETE" in result.output + assert "concore" in result.output + + +def test_completion_rejects_unknown_shell(): + result = CliRunner().invoke(cli, ["completion", "powershell"]) + assert result.exit_code != 0 + + +def _complete(args, incomplete): + comp = get_completion_class("bash")(cli, {}, "concore", "_CONCORE_COMPLETE") + return [item.value for item in comp.get_completions(args, incomplete)] + + +def test_commands_are_completed(): + assert _complete([], "va") == ["validate"] + assert "completion" in _complete([], "") + + +def test_option_values_are_completed(): + assert _complete(["build", "wf.graphml", "--type"], "") == [ + "windows", + "posix", + "docker", + ] + + +def test_completion_command_completes_shell_names(): + assert _complete(["completion"], "") == ["bash", "zsh", "fish"]