1 /*
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #pragma once
18
19 #include <optional>
20 #include <string>
21 #include <vector>
22
23 #include "common/libs/utils/result.h"
24
25 namespace cuttlefish {
26 namespace selector {
27
28 /*
29 * @return any parsing successfully and actually happened
30 */
31 template <typename T>
FilterSelectorFlag(std::vector<std::string> & args,const std::string & flag_name,std::optional<T> & value_opt)32 Result<void> FilterSelectorFlag(std::vector<std::string>& args,
33 const std::string& flag_name,
34 std::optional<T>& value_opt) {
35 value_opt = std::nullopt;
36 const int args_initial_size = args.size();
37 if (args_initial_size == 0) {
38 return {};
39 }
40
41 T value;
42 CF_EXPECT(ParseFlags({GflagsCompatFlag(flag_name, value)}, args),
43 "Failed to parse --" << flag_name);
44 if (args.size() == args_initial_size) {
45 // not consumed
46 return {};
47 }
48 value_opt = value;
49 return {};
50 }
51
52 /*
53 * android::base::Split by delimeter but returns CF_ERR if any split token is
54 * empty
55 */
56 Result<std::vector<std::string>> SeparateButWithNoEmptyToken(
57 const std::string& input, const std::string& delimiter);
58
59 } // namespace selector
60 } // namespace cuttlefish
61