Skip to content

Enable cli tab completion #317

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 15, 2025
Merged
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
1 change: 1 addition & 0 deletions .config/requirements-test.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
argcomplete
coverage[toml]
mypy
pip-tools
Expand Down
2 changes: 2 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ repos:
args:
- --output-format=colorized
additional_dependencies:
- argcomplete
- pytest
- pyyaml
- subprocess_tee
Expand All @@ -91,6 +92,7 @@ repos:
hooks:
- id: mypy
additional_dependencies:
- argcomplete
- pip
- pytest
- subprocess_tee
Expand Down
11 changes: 11 additions & 0 deletions src/ansible_dev_environment/arg_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
from .utils import str_to_bool


try:
import argcomplete

HAS_ARGCOMPLETE = True
except ImportError: # pragma: no cover
HAS_ARGCOMPLETE = False


logger = logging.getLogger(__name__)

if TYPE_CHECKING:
Expand Down Expand Up @@ -255,6 +263,9 @@ def parse() -> argparse.Namespace:
err = "The environment variable 'SKIP_UV' is deprecated, use 'ADE_UV' or '--no-uv' instead."
warnings.warn(err, DeprecationWarning, stacklevel=2)

if HAS_ARGCOMPLETE:
argcomplete.autocomplete(parser)

return apply_envvars(args=args, parser=parser)


Expand Down
1 change: 1 addition & 0 deletions src/ansible_dev_environment/cli.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# PYTHON_ARGCOMPLETE_OK
"""CLI entrypoint."""

from __future__ import annotations
Expand Down
53 changes: 52 additions & 1 deletion tests/unit/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
from pathlib import Path
from typing import TYPE_CHECKING

import argcomplete
import pytest

from ansible_dev_environment.arg_parser import ArgumentParser, apply_envvars
from ansible_dev_environment.arg_parser import ArgumentParser, apply_envvars, parse
from ansible_dev_environment.cli import Cli, main


Expand Down Expand Up @@ -385,3 +386,53 @@ def test_env_wrong_choice(
cli.parse_args()
captured = capsys.readouterr()
assert "choose from 'restrictive', 'cfg', 'none'" in captured.err


def test_arg_complete(monkeypatch: pytest.MonkeyPatch) -> None:
"""Test argument completion.

Args:
monkeypatch: Pytest fixture.
"""
inited_parser = None
orig_apply_envvars = apply_envvars

def _apply_envvars(
args: list[str],
parser: ArgumentParser,
) -> None:
"""Apply environment variables to the argument parser.

Args:
args: List of arguments.
parser: Argument parser.
"""
nonlocal inited_parser
inited_parser = parser
orig_apply_envvars(args, parser)

monkeypatch.setattr(
"ansible_dev_environment.arg_parser.apply_envvars",
_apply_envvars,
)

monkeypatch.setattr(
"sys.argv",
["ade", "install"],
)
parse()

cli = "ade ins"
monkeypatch.setenv("_ARGCOMPLETE", "1")
monkeypatch.setenv("_ARGCOMPLETE_IFS", "\013")
monkeypatch.setenv("COMP_LINE", cli)
monkeypatch.setenv("COMP_POINT", str(len(cli)))
import io

str_io = io.StringIO()

argcomplete.autocomplete(inited_parser, exit_method=print, output_stream=str_io) # type: ignore[arg-type]

output = str_io.getvalue()
assert "inspect" in output
assert "install" in output
Loading