Add static Bash completion with argcomplete

This commit is contained in:
mdaleo404 committed 2026-10-02 15:41:10 +01:00
1 parent beb16e5be3
commit a73c09cc9b
7 files changed
+95 -6

No files matched your search

+11 -1
View File
@@ -75,7 +75,17 @@ $ pipx install schedls
$ pip install --user 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 ## Examples
+13
View File
@@ -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 `--json` is intended for scripts. It prints one document on stdout and sends
diagnostics to stderr; no ANSI escapes are emitted. 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 ## Commands
### `list` ### `list`
+2 -2
View File
@@ -108,8 +108,8 @@ removes whatever it had already written.
### Supply chain ### Supply chain
Runtime dependencies are zero. Development dependencies are locked and audited The sole runtime dependency is `argcomplete`, which provides opt-in shell
in CI. completion. Development dependencies are locked and audited in CI.
## Availability, privacy ## Availability, privacy
Generated
+17 -2
View File
@@ -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]] [[package]]
name = "ast-serialize" name = "ast-serialize"
@@ -820,4 +835,4 @@ python-discovery = ">=1.6"
[metadata] [metadata]
lock-version = "2.1" lock-version = "2.1"
python-versions = ">=3.11,<4.0" python-versions = ">=3.11,<4.0"
content-hash = "25a3f2935d10e944921652fc64a2d0209fb0a0c98cf35d743c934cee62137cf3" content-hash = "cbb2fef0a2daaa34415ed1a4505ca966650a621fb261a421ba4a46c8b62c8cba"
+1 -1
View File
@@ -8,7 +8,7 @@ authors = [
license = "GPL-3.0-or-later" license = "GPL-3.0-or-later"
readme = "README.md" readme = "README.md"
requires-python = ">=3.11,<4.0" requires-python = ">=3.11,<4.0"
dependencies = [] dependencies = ["argcomplete>=3.3,<4.0"]
keywords = ["cron", "crontab", "systemd", "timer", "scheduler", "cli", "sysadmin"] keywords = ["cron", "crontab", "systemd", "timer", "scheduler", "cli", "sysadmin"]
classifiers = [ classifiers = [
"Development Status :: 3 - Alpha", "Development Status :: 3 - Alpha",
+4
View File
@@ -7,6 +7,9 @@ import re
import sys import sys
from dataclasses import dataclass from dataclasses import dataclass
import argcomplete
from argcomplete.completers import SuppressCompleter
from . import __version__, convenience from . import __version__, convenience
from .backends.base import SchedulerBackend from .backends.base import SchedulerBackend
from .backends.cron import CronBackend 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) arguments = list(sys.argv[1:] if argv is None else argv)
head, tail = split_command(arguments) head, tail = split_command(arguments)
parser = build_parser() parser = build_parser()
argcomplete.autocomplete(parser, default_completer=SuppressCompleter())
args = parser.parse_args(head) args = parser.parse_args(head)
output = Output(json_mode=args.json_mode, color=args.color, utc=args.utc) output = Output(json_mode=args.json_mode, color=args.color, utc=args.utc)
+47
View File
@@ -1,5 +1,9 @@
from __future__ import annotations from __future__ import annotations
import os
import subprocess
import sys
import pytest import pytest
from schedls.cli import _parse_environment, _spec_for_new, build_parser, main, split_command 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 == [] 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: def test_edit_command_dest_does_not_shadow_subcommand() -> None:
args = build_parser().parse_args(["edit", "backup", "--command"]) args = build_parser().parse_args(["edit", "backup", "--command"])
assert args.command == "edit" assert args.command == "edit"