summaryrefslogtreecommitdiffstats
path: root/test/lib/ansible_test/_internal/cli/parsers/base_argument_parsers.py
blob: aac7a6946885bcc2670ddbeaa3b478e5180838ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"""Base classes for the primary parsers for composite command line arguments."""
from __future__ import annotations

import abc
import typing as t

from ..argparsing.parsers import (
    CompletionError,
    NamespaceParser,
    ParserState,
)


class ControllerNamespaceParser(NamespaceParser, metaclass=abc.ABCMeta):
    """Base class for controller namespace parsers."""
    @property
    def dest(self) -> str:
        """The name of the attribute where the value should be stored."""
        return 'controller'

    def parse(self, state: ParserState) -> t.Any:
        """Parse the input from the given state and return the result."""
        if state.root_namespace.targets:
            raise ControllerRequiredFirstError()

        return super().parse(state)


class TargetNamespaceParser(NamespaceParser, metaclass=abc.ABCMeta):
    """Base class for target namespace parsers involving a single target."""
    @property
    def option_name(self) -> str:
        """The option name used for this parser."""
        return '--target'

    @property
    def dest(self) -> str:
        """The name of the attribute where the value should be stored."""
        return 'targets'

    @property
    def use_list(self) -> bool:
        """True if the destination is a list, otherwise False."""
        return True

    @property
    def limit_one(self) -> bool:
        """True if only one target is allowed, otherwise False."""
        return True


class TargetsNamespaceParser(NamespaceParser, metaclass=abc.ABCMeta):
    """Base class for controller namespace parsers involving multiple targets."""
    @property
    def option_name(self) -> str:
        """The option name used for this parser."""
        return '--target'

    @property
    def dest(self) -> str:
        """The name of the attribute where the value should be stored."""
        return 'targets'

    @property
    def use_list(self) -> bool:
        """True if the destination is a list, otherwise False."""
        return True


class ControllerRequiredFirstError(CompletionError):
    """Exception raised when controller and target options are specified out-of-order."""
    def __init__(self) -> None:
        super().__init__('The `--controller` option must be specified before `--target` option(s).')