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