summaryrefslogtreecommitdiffstats
path: root/test/lib/ansible_test/_internal/cli/commands/__init__.py
blob: 2eb14abc7bac2a86145af801eda0be58c682a4f0 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
"""Command line parsing for all commands."""
from __future__ import annotations

import argparse
import functools
import sys

from ...util import (
    display,
)

from ..completers import (
    complete_target,
    register_completer,
)

from ..environments import (
    CompositeActionCompletionFinder,
)

from .coverage import (
    do_coverage,
)

from .env import (
    do_env,
)

from .integration import (
    do_integration,
)

from .sanity import (
    do_sanity,
)

from .shell import (
    do_shell,
)

from .units import (
    do_units,
)


def do_commands(
    parent: argparse.ArgumentParser,
    completer: CompositeActionCompletionFinder,
) -> None:
    """Command line parsing for all commands."""
    common = argparse.ArgumentParser(add_help=False)

    common.add_argument(
        '-e',
        '--explain',
        action='store_true',
        help='explain commands that would be executed',
    )

    common.add_argument(
        '-v',
        '--verbose',
        dest='verbosity',
        action='count',
        default=0,
        help='display more output',
    )

    common.add_argument(
        '--color',
        metavar='COLOR',
        nargs='?',
        help='generate color output: yes, no, auto',
        const='yes',
        default='auto',
        type=color,
    )

    common.add_argument(
        '--debug',
        action='store_true',
        help='run ansible commands in debug mode',
    )

    common.add_argument(
        '--truncate',
        dest='truncate',
        metavar='COLUMNS',
        type=int,
        default=display.columns,
        help='truncate some long output (0=disabled) (default: auto)',
    )

    common.add_argument(
        '--redact',
        dest='redact',
        action='store_true',
        default=True,
        help=argparse.SUPPRESS,  # kept for backwards compatibility, but no point in advertising since it's the default
    )

    common.add_argument(
        '--no-redact',
        dest='redact',
        action='store_false',
        default=False,
        help='show sensitive values in output',
    )

    test = argparse.ArgumentParser(add_help=False, parents=[common])

    testing = test.add_argument_group(title='common testing arguments')

    register_completer(testing.add_argument(
        'include',
        metavar='TARGET',
        nargs='*',
        help='test the specified target',
    ), functools.partial(complete_target, completer))

    register_completer(testing.add_argument(
        '--include',
        metavar='TARGET',
        action='append',
        help='include the specified target',
    ), functools.partial(complete_target, completer))

    register_completer(testing.add_argument(
        '--exclude',
        metavar='TARGET',
        action='append',
        help='exclude the specified target',
    ), functools.partial(complete_target, completer))

    register_completer(testing.add_argument(
        '--require',
        metavar='TARGET',
        action='append',
        help='require the specified target',
    ), functools.partial(complete_target, completer))

    testing.add_argument(
        '--coverage',
        action='store_true',
        help='analyze code coverage when running tests',
    )

    testing.add_argument(
        '--coverage-check',
        action='store_true',
        help='only verify code coverage can be enabled',
    )

    testing.add_argument(
        '--metadata',
        help=argparse.SUPPRESS,
    )

    testing.add_argument(
        '--base-branch',
        metavar='BRANCH',
        help='base branch used for change detection',
    )

    testing.add_argument(
        '--changed',
        action='store_true',
        help='limit targets based on changes',
    )

    changes = test.add_argument_group(title='change detection arguments')

    changes.add_argument(
        '--tracked',
        action='store_true',
        help=argparse.SUPPRESS,
    )

    changes.add_argument(
        '--untracked',
        action='store_true',
        help='include untracked files',
    )

    changes.add_argument(
        '--ignore-committed',
        dest='committed',
        action='store_false',
        help='exclude committed files',
    )

    changes.add_argument(
        '--ignore-staged',
        dest='staged',
        action='store_false',
        help='exclude staged files',
    )

    changes.add_argument(
        '--ignore-unstaged',
        dest='unstaged',
        action='store_false',
        help='exclude unstaged files',
    )

    changes.add_argument(
        '--changed-from',
        metavar='PATH',
        help=argparse.SUPPRESS,
    )

    changes.add_argument(
        '--changed-path',
        metavar='PATH',
        action='append',
        help=argparse.SUPPRESS,
    )

    subparsers = parent.add_subparsers(metavar='COMMAND', required=True)

    do_coverage(subparsers, common, completer)
    do_env(subparsers, common, completer)
    do_shell(subparsers, common, completer)

    do_integration(subparsers, test, completer)
    do_sanity(subparsers, test, completer)
    do_units(subparsers, test, completer)


def color(value: str) -> bool:
    """Strict converter for color option."""
    if value == 'yes':
        return True

    if value == 'no':
        return False

    if value == 'auto':
        return sys.stdout.isatty()

    raise argparse.ArgumentTypeError(f"invalid choice: '{value}' (choose from 'yes', 'no', 'auto')")