From a73c09cc9bbb4b17c0bee9c3a12b3dcf0c8b7f6a Mon Sep 17 00:00:00 2001 From: Marco D'Aleo Date: Fri, 2 Oct 2026 15:41:10 +0100 Subject: [PATCH] Add static Bash completion with argcomplete --- README.md | 12 ++++++++++- docs/cli.md | 13 ++++++++++++ docs/security-model.md | 4 ++-- poetry.lock | 19 +++++++++++++++-- pyproject.toml | 2 +- src/schedls/cli.py | 4 ++++ tests/unit/test_cli.py | 47 ++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 95 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index b2e82fd..6165b11 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,17 @@ $ pipx install schedls $ pip install --user schedls ``` -`python -m schedls` works too. Zero runtime dependencies; Python 3.11+. +`python -m schedls` works too. Python 3.11+. + +### Bash completion + +Enable opt-in completion for the current Bash session: + +```bash +eval "$(register-python-argcomplete schedls)" +``` + +Add that line to your shell profile to enable it in future sessions. ## Examples diff --git a/docs/cli.md b/docs/cli.md index 3f4020a..6f1a6e5 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -33,6 +33,19 @@ Global options may appear before the command. `--user`/`--system`, `--json` is intended for scripts. It prints one document on stdout and sends diagnostics to stderr; no ANSI escapes are emitted. +## Bash completion + +Completion is opt-in. After installing schedls, enable it for the current Bash +session with: + +```bash +eval "$(register-python-argcomplete schedls)" +``` + +Add the command to your shell profile to enable it in future sessions. It +completes schedls subcommands, options, and option choices without discovering +schedules. + ## Commands ### `list` diff --git a/docs/security-model.md b/docs/security-model.md index 5db313f..763c9c4 100644 --- a/docs/security-model.md +++ b/docs/security-model.md @@ -108,8 +108,8 @@ removes whatever it had already written. ### Supply chain -Runtime dependencies are zero. Development dependencies are locked and audited -in CI. +The sole runtime dependency is `argcomplete`, which provides opt-in shell +completion. Development dependencies are locked and audited in CI. ## Availability, privacy diff --git a/poetry.lock b/poetry.lock index cd9b1e7..be26404 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1,4 +1,19 @@ -# This file is automatically @generated by Poetry 2.4.1 and should not be changed by hand. +# This file is automatically @generated by Poetry 2.5.0 and should not be changed by hand. + +[[package]] +name = "argcomplete" +version = "3.7.2" +description = "Bash tab completion for argparse" +optional = false +python-versions = ">=3.10" +groups = ["main"] +files = [ + {file = "argcomplete-3.7.2-py3-none-any.whl", hash = "sha256:6029205678bdd9c1c728a155f5f9ecf5812393f969eef58807641a2bc2aa5b19"}, + {file = "argcomplete-3.7.2.tar.gz", hash = "sha256:aad8b69a0b9969edb62db0d1752354c0d50717b10e0cbb00e2a958381b9fc6b9"}, +] + +[package.extras] +test = ["coverage", "mypy", "pexpect", "ruff", "wheel"] [[package]] name = "ast-serialize" @@ -820,4 +835,4 @@ python-discovery = ">=1.6" [metadata] lock-version = "2.1" python-versions = ">=3.11,<4.0" -content-hash = "25a3f2935d10e944921652fc64a2d0209fb0a0c98cf35d743c934cee62137cf3" +content-hash = "cbb2fef0a2daaa34415ed1a4505ca966650a621fb261a421ba4a46c8b62c8cba" diff --git a/pyproject.toml b/pyproject.toml index 23d77b5..98724d7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -8,7 +8,7 @@ authors = [ license = "GPL-3.0-or-later" readme = "README.md" requires-python = ">=3.11,<4.0" -dependencies = [] +dependencies = ["argcomplete>=3.3,<4.0"] keywords = ["cron", "crontab", "systemd", "timer", "scheduler", "cli", "sysadmin"] classifiers = [ "Development Status :: 3 - Alpha", diff --git a/src/schedls/cli.py b/src/schedls/cli.py index 240a755..b257ef7 100644 --- a/src/schedls/cli.py +++ b/src/schedls/cli.py @@ -7,6 +7,9 @@ import re import sys from dataclasses import dataclass +import argcomplete +from argcomplete.completers import SuppressCompleter + from . import __version__, convenience from .backends.base import SchedulerBackend from .backends.cron import CronBackend @@ -219,6 +222,7 @@ def main(argv: list[str] | None = None) -> int: arguments = list(sys.argv[1:] if argv is None else argv) head, tail = split_command(arguments) parser = build_parser() + argcomplete.autocomplete(parser, default_completer=SuppressCompleter()) args = parser.parse_args(head) output = Output(json_mode=args.json_mode, color=args.color, utc=args.utc) diff --git a/tests/unit/test_cli.py b/tests/unit/test_cli.py index 5bfb8d4..60dde60 100644 --- a/tests/unit/test_cli.py +++ b/tests/unit/test_cli.py @@ -1,5 +1,9 @@ from __future__ import annotations +import os +import subprocess +import sys + import pytest from schedls.cli import _parse_environment, _spec_for_new, build_parser, main, split_command @@ -16,6 +20,49 @@ def test_split_command() -> None: assert tail == [] +def test_main_activates_argcomplete(monkeypatch: pytest.MonkeyPatch) -> None: + calls = [] + monkeypatch.setattr("schedls.cli.argcomplete.autocomplete", lambda parser, **kwargs: calls.append((parser, kwargs))) + + with pytest.raises(SystemExit): + main(["--version"]) + + assert len(calls) == 1 + assert calls[0][1]["default_completer"].__class__.__name__ == "SuppressCompleter" + + +def _complete(tmp_path, line: str) -> list[str]: + completions = tmp_path / "completions" + environment = { + **os.environ, + "_ARGCOMPLETE": "1", + "_ARGCOMPLETE_STDOUT_FILENAME": str(completions), + "COMP_LINE": line, + "COMP_POINT": str(len(line)), + "COMP_WORDBREAKS": " \t\n\"'><=;|&(:,", + "PATH": "", + } + + completed = subprocess.run( + [sys.executable, "-m", "schedls"], + check=False, + cwd=tmp_path, + env=environment, + capture_output=True, + text=True, + ) + + assert completed.returncode == 0 + return [item.strip() for item in completions.read_text().split("\v") if item.strip()] + + +def test_argcomplete_only_completes_static_parser_info(tmp_path) -> None: + (tmp_path / "candidate").touch() + + assert "show" in _complete(tmp_path, "schedls sh") + assert _complete(tmp_path, "schedls show c") == [] + + def test_edit_command_dest_does_not_shadow_subcommand() -> None: args = build_parser().parse_args(["edit", "backup", "--command"]) assert args.command == "edit"