Android-cuttlefish cvd tool
selector_option_parser_utils.h
Go to the documentation of this file.
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 <cstddef>
20#include <optional>
21#include <string>
22#include <vector>
23
26
27namespace cuttlefish {
28namespace selector {
29
30/*
31 * @return any parsing successfully and actually happened
32 */
33template <typename T>
34Result<void> FilterSelectorFlag(std::vector<std::string>& args,
35 const std::string& flag_name,
36 std::optional<T>& value_opt) {
37 value_opt = std::nullopt;
38 const std::size_t args_initial_size = args.size();
39 if (args_initial_size == 0) {
40 return {};
41 }
42
43 T value;
44 CF_EXPECT(ConsumeFlags({GflagsCompatFlag(flag_name, value)}, args),
45 "Failed to parse --" << flag_name);
46 if (args.size() == args_initial_size) {
47 // not consumed
48 return {};
49 }
50 value_opt = value;
51 return {};
52}
53
54/*
55 * android::base::Split by delimiter but returns CF_ERR if any split token is
56 * empty
57 */
59 const std::string& input, const std::string& delimiter);
60
61} // namespace selector
62} // namespace cuttlefish
Definition: expected.h:86
#define CF_EXPECT(...)
Definition: result.h:414
Result< void > FilterSelectorFlag(std::vector< std::string > &args, const std::string &flag_name, std::optional< T > &value_opt)
Definition: selector_option_parser_utils.h:34
Result< std::vector< std::string > > SeparateButWithNoEmptyToken(const std::string &input, const std::string &delimiter)
Definition: selector_option_parser_utils.cpp:27
Definition: alloc_utils.cpp:23
Flag GflagsCompatFlag(const std::string &name)
Definition: flag_parser.cpp:583
Result< void > ConsumeFlags(const std::vector< Flag > &flags, std::vector< std::string > &args, const bool recognize_end_of_option_mark)
Definition: flag_parser.cpp:394