Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions concore_cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,22 @@ Stops all running concore processes. Prompts for confirmation before proceeding.
concore stop
```

### `concore completion <shell>`

Prints a tab-completion script so `concore <TAB>` 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**
Expand Down
8 changes: 8 additions & 0 deletions concore_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__
Expand Down Expand Up @@ -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"""
Expand Down
13 changes: 13 additions & 0 deletions concore_cli/commands/completion.py
Original file line number Diff line number Diff line change
@@ -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()
40 changes: 40 additions & 0 deletions tests/test_completion_command.py
Original file line number Diff line number Diff line change
@@ -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"]
Loading