Debian Codegraph @

test/lib/ansible_test/_internal/cli/completers.py

raw
1 """Completers for use with argcomplete."""
2
3 from __future__ import annotations
4
5 import argparse
6
7 from ..target import (
8 find_target_completion,
9 )
10
11 from .argparsing.argcompletion import (
12 OptionCompletionFinder,
13 )
14
15
16 def complete_target(completer: OptionCompletionFinder, prefix: str, parsed_args: argparse.Namespace, **_) -> list[str]:
17 """Perform completion for the targets configured for the command being parsed."""
18 matches = find_target_completion(parsed_args.targets_func, prefix, completer.list_mode)
19 completer.disable_completion_mangling = completer.list_mode and len(matches) > 1
20 return matches
21
22
23 def complete_choices(choices: list[str], prefix: str, **_) -> list[str]:
24 """Perform completion using the provided choices."""
25 matches = [choice for choice in choices if choice.startswith(prefix)]
26 return matches
27
28
29 def register_completer(action: argparse.Action, completer) -> None:
30 """Register the given completer with the specified action."""
31 action.completer = completer # type: ignore[attr-defined] # intentionally using an attribute that does not exist