1# Copyright (C) 2022 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15def get_arg_value(args_list, arg_name):
16    """
17    Fetches the value of a named argument from the list of args provided by a
18    Bazel action. If there are multiple instances of the arg present, this
19    function will return the first. This function makes all the same assumptions
20    as get_arg_values() below.
21
22    Arguments:
23        args_list (string[]): The list of arguments provided by the Bazel action.
24                           i.e., bazel_action.argv
25        arg_name (string): The name of the argument to fetch the value of
26    Return:
27        The value corresponding to the specified argument name
28    """
29
30    values = get_arg_values(args_list, arg_name)
31    if len(values) == 0:
32        return None
33
34    if len(values) != 1:
35        fail("More than one args found `%s`" % values)
36
37    return values[0]
38
39def get_arg_values(args_list, arg_name):
40    """
41    Fetches all the values of a named argument from the list of args provided
42    by a Bazel action, the argument and its values can repeat multiple times
43    and all the values will be returned.
44
45    This function assumes that the only
46    one argument is added per call to args.add() or per string passed to
47    args.add_all(). It still works when two values are passed to
48    args.add() as separate strings, however.
49
50    The above assumption implies that this function does not handle cases where
51    an argument name is separated from its value by an =, or any character
52    other than a space, in the final command.
53
54    Arguments:
55        args_list (string[]): The list of arguments provided by the Bazel action.
56                           i.e., bazel_action.argv
57        arg_name (string): The name of the argument to fetch the value of
58    Return:
59        All the values corresponding to the specified argument name
60    """
61
62    values = []
63    for i in range(1, len(args_list) - 1):
64        if args_list[i] == arg_name:
65            values.append(args_list[i + 1])
66
67    return values
68
69def get_all_args_with_prefix(input_args, arg_prefix):
70    """returns all arguments that start with arg_prefix
71
72    Args:
73        input_args (list[str]): list of arguments
74        arg_prefix (str): prefix of argument to search for
75    Returns:
76        args (list[str]): value in args that start with arg_prefix
77    """
78    args = []
79    for a in input_args:
80        if a.startswith(arg_prefix):
81            args.append(a[len(arg_prefix):])
82    return args
83
84def get_single_arg_with_prefix(input_args, arg_prefix):
85    """returns all arguments that start with arg_prefix
86
87    Fails if more than one argument exists.
88
89    Args:
90        input_args (list[str]): list of arguments
91        arg_prefix (str): prefix of argument to search for
92    Returns:
93        args (str): value in args that starts with arg_prefix
94    """
95    args = get_all_args_with_prefix(input_args, arg_prefix)
96    if len(args) != 1:
97        fail("expected single argument with prefix `%s`, got %d; args = `%s`" % (arg_prefix, len(args), args))
98    return args[0]
99