Android-cuttlefish cvd tool
flag.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2021 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 <functional>
20#include <ostream>
21#include <span>
22#include <string>
23#include <string_view>
24#include <vector>
25
27
28namespace cuttlefish {
29
30struct ConsumeFlagsOpts;
31
32class Flag {
33 public:
34 static Flag StringFlag(std::string name);
35 static Flag BoolFlag(std::string name);
36
37 /* Add an alias that triggers matches and calls to the `Setter` function. */
38 Flag& Alias(std::string alias) &;
39 Flag Alias(std::string alias) &&;
40 /* Set help text, visible in the class ostream writer method. Optional. */
41 Flag& Help(std::string) &;
42 Flag Help(std::string) &&;
43 /* Set the value name hint used in the synopsis. Optional. */
44 Flag& ValueNameHint(std::string) &;
45 Flag ValueNameHint(std::string) &&;
46 /* Set a loader that displays the current value in help text. Optional. */
47 Flag& Getter(std::function<std::string()>) &;
48 Flag Getter(std::function<std::string()>) &&;
49 /* Set the callback for matches. The callback may be invoked multiple times.
50 */
51 Flag& Setter(std::function<Result<void>(std::string_view)>) &;
52 Flag Setter(std::function<Result<void>(std::string_view)>) &&;
53 /* Add a callback to validate the parsed flag value. These callbacks are
54 * guaranteed to be called after Setter succeeds in the same order they are
55 * added. Validation stops when one validator callback fails, remaining
56 * callbacks are not executed. */
57 Flag& AddValidator(std::function<Result<void>()>) &;
58 Flag AddValidator(std::function<Result<void>()>) &&;
59
60 std::string Name() const;
61 bool operator<(const Flag& other) const { return Name() < other.Name(); }
62 std::string Synopsis() const;
63 const std::string& Description() const;
64 std::string CurrentValue() const;
65 const std::string& ValueNameHint() const;
66
67 private:
68 enum class Style {
69 String,
70 Bool,
71 };
72 Flag(std::string name, Style style);
73
74 void ValidateAlias(const std::string&);
75
76 /* Calls the flag's setter with the appropriate value if it matches the
77 * current argument. Returns the number of arguments consumed by the flag
78 * (typically between 0 and 2), with 0 indicating the flag doesn't match. */
79 Result<size_t> Match(std::string_view current_arg,
80 std::span<const std::string> following_args) const;
81 Result<size_t> MatchStringStyleFlag(std::string_view,
82 std::span<const std::string>) const;
83 Result<size_t> MatchBoolStyleFlag(std::string_view) const;
84 Result<void> SetAndValidate(std::string_view) const;
85
86 friend void WriteGflagsCompatXml(const Flag&, std::ostream&);
87 friend Result<void> ConsumeFlags(const std::vector<Flag>&,
88 std::vector<std::string>&, ConsumeFlagsOpts);
89
90 std::vector<std::string> aliases_;
92 std::string help_;
93 std::string value_name_hint_ = "VAL";
94 std::function<std::string()> getter_ = []() { return ""; };
95 std::function<Result<void>(std::string_view value)> setter_;
96 std::vector<std::function<Result<void>()>> validators_;
97};
98
99std::ostream& operator<<(std::ostream&, const Flag&);
100
102 /* Stop matching flags when the "--" separator is found. The other options
103 * only apply to the arguments up to but exluding the "--", if present. */
105 /* Fail if there are unconsumed arguments left. */
107};
108/* Matches flags against a list of arguments, removing all matches and leaving
109 * only unmatched arguments.
110 */
111Result<void> ConsumeFlags(const std::vector<Flag>& flags,
112 std::vector<std::string>& args,
113 ConsumeFlagsOpts opts = {});
114Result<void> ConsumeFlags(const std::vector<Flag>& flags,
115 std::vector<std::string>&& args,
116 ConsumeFlagsOpts opts = {});
117
118} // namespace cuttlefish
Definition: flag.h:32
std::string CurrentValue() const
Definition: flag.cpp:145
Result< void > SetAndValidate(std::string_view) const
Definition: flag.cpp:218
std::vector< std::string > aliases_
Definition: flag.h:90
std::string Synopsis() const
Definition: flag.cpp:128
Style
Definition: flag.h:68
Flag & Setter(std::function< Result< void >(std::string_view)>) &
Definition: flag.cpp:110
const std::string & Description() const
Definition: flag.cpp:143
Style style_
Definition: flag.h:91
std::function< std::string()> getter_
Definition: flag.h:94
Flag & AddValidator(std::function< Result< void >()>) &
Definition: flag.cpp:118
bool operator<(const Flag &other) const
Definition: flag.h:61
friend void WriteGflagsCompatXml(const Flag &, std::ostream &)
Definition: gflags_compat.cpp:214
static Flag BoolFlag(std::string name)
Definition: flag.cpp:77
std::string value_name_hint_
Definition: flag.h:93
std::vector< std::function< Result< void >()> > validators_
Definition: flag.h:96
Result< size_t > MatchStringStyleFlag(std::string_view, std::span< const std::string >) const
Definition: flag.cpp:170
Flag & Help(std::string) &
Definition: flag.cpp:88
const std::string & ValueNameHint() const
Definition: flag.cpp:147
void ValidateAlias(const std::string &)
Definition: flag.cpp:155
Flag(std::string name, Style style)
Definition: flag.cpp:149
Flag & Alias(std::string alias) &
Definition: flag.cpp:81
Flag & Getter(std::function< std::string()>) &
Definition: flag.cpp:102
Result< size_t > Match(std::string_view current_arg, std::span< const std::string > following_args) const
Definition: flag.cpp:160
std::string Name() const
Definition: flag.cpp:126
static Flag StringFlag(std::string name)
Definition: flag.cpp:73
Result< size_t > MatchBoolStyleFlag(std::string_view) const
Definition: flag.cpp:196
std::function< Result< void >(std::string_view value)> setter_
Definition: flag.h:95
friend Result< void > ConsumeFlags(const std::vector< Flag > &, std::vector< std::string > &, ConsumeFlagsOpts)
Definition: flag.cpp:237
std::string help_
Definition: flag.h:92
char * name
Definition: libf2fs.c:1403
Definition: alloc_driver.h:20
Result< void > ConsumeFlags(const std::vector< Flag > &flags, std::vector< std::string > &args, ConsumeFlagsOpts opts)
Definition: flag.cpp:237
tl::expected< T, StackTraceError > Result
Definition: result_type.h:30
std::ostream & operator<<(std::ostream &out, DeviceType device_type)
Definition: device_type.cpp:72
Definition: flag.h:101
bool fail_on_unexpected_argument
Definition: flag.h:106
bool stop_at_double_dashes
Definition: flag.h:104