1 // Copyright (c) 2023 Google LLC.
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
15 #include "flags.h"
16
17 #include <algorithm>
18 #include <cerrno>
19 #include <cstdlib>
20 #include <cstring>
21 #include <iostream>
22 #include <regex>
23 #include <string>
24 #include <unordered_set>
25 #include <variant>
26 #include <vector>
27
28 namespace flags {
29
30 std::vector<std::string> positional_arguments;
31
32 namespace {
33
34 using token_t = const char*;
35 using token_iterator_t = token_t*;
36
37 // Extracts the flag name from a potential token.
38 // This function only looks for a '=', to split the flag name from the value for
39 // long-form flags. Returns the name of the flag, prefixed with the hyphen(s).
get_flag_name(const std::string & flag,bool is_short_flag)40 inline std::string get_flag_name(const std::string& flag, bool is_short_flag) {
41 if (is_short_flag) {
42 return flag;
43 }
44
45 size_t equal_index = flag.find('=');
46 if (equal_index == std::string::npos) {
47 return flag;
48 }
49 return flag.substr(0, equal_index);
50 }
51
52 // Parse a boolean flag. Returns `true` if the parsing succeeded, `false`
53 // otherwise.
parse_bool_flag(Flag<bool> & flag,bool is_short_flag,const std::string & token)54 bool parse_bool_flag(Flag<bool>& flag, bool is_short_flag,
55 const std::string& token) {
56 if (is_short_flag) {
57 flag.value() = true;
58 return true;
59 }
60
61 const std::string raw_flag(token);
62 size_t equal_index = raw_flag.find('=');
63 if (equal_index == std::string::npos) {
64 flag.value() = true;
65 return true;
66 }
67
68 const std::string value = raw_flag.substr(equal_index + 1);
69 if (value == "true") {
70 flag.value() = true;
71 return true;
72 }
73
74 if (value == "false") {
75 flag.value() = false;
76 return true;
77 }
78
79 return false;
80 }
81
82 // Parse a uint32_t flag value.
parse_flag_value(Flag<uint32_t> & flag,const std::string & value)83 bool parse_flag_value(Flag<uint32_t>& flag, const std::string& value) {
84 std::regex unsigned_pattern("^ *[0-9]+ *$");
85 if (!std::regex_match(value, unsigned_pattern)) {
86 std::cerr << "'" << value << "' is not a unsigned number." << std::endl;
87 return false;
88 }
89
90 errno = 0;
91 char* end_ptr = nullptr;
92 const uint64_t number = strtoull(value.c_str(), &end_ptr, 10);
93 if (end_ptr == nullptr || end_ptr != value.c_str() + value.size() ||
94 errno == EINVAL) {
95 std::cerr << "'" << value << "' is not a unsigned number." << std::endl;
96 return false;
97 }
98
99 if (errno == ERANGE || number > static_cast<size_t>(UINT32_MAX)) {
100 std::cerr << "'" << value << "' cannot be represented as a 32bit unsigned."
101 << std::endl;
102 return false;
103 }
104
105 flag.value() = static_cast<uint32_t>(number);
106 return true;
107 }
108
109 // "Parse" a string flag value (assigns it, cannot fail).
parse_flag_value(Flag<std::string> & flag,const std::string & value)110 bool parse_flag_value(Flag<std::string>& flag, const std::string& value) {
111 flag.value() = value;
112 return true;
113 }
114
115 // Parse a potential multi-token flag. Moves the iterator to the last flag's
116 // token if it's a multi-token flag. Returns `true` if the parsing succeeded.
117 // The iterator is moved to the last parsed token.
118 template <typename T>
parse_flag(Flag<T> & flag,bool is_short_flag,const char *** iterator)119 bool parse_flag(Flag<T>& flag, bool is_short_flag, const char*** iterator) {
120 const std::string raw_flag(**iterator);
121 std::string raw_value;
122 const size_t equal_index = raw_flag.find('=');
123
124 if (is_short_flag || equal_index == std::string::npos) {
125 if ((*iterator)[1] == nullptr) {
126 return false;
127 }
128
129 // This is a bi-token flag. Moving iterator to the last parsed token.
130 raw_value = (*iterator)[1];
131 *iterator += 1;
132 } else {
133 // This is a mono-token flag, no need to move the iterator.
134 raw_value = raw_flag.substr(equal_index + 1);
135 }
136
137 return parse_flag_value(flag, raw_value);
138 }
139
140 } // namespace
141
142 // This is the function to expand if you want to support a new type.
parse_flag_info(FlagInfo & info,token_iterator_t * iterator)143 bool FlagList::parse_flag_info(FlagInfo& info, token_iterator_t* iterator) {
144 bool success = false;
145
146 std::visit(
147 [&](auto&& item) {
148 using T = std::decay_t<decltype(item.get())>;
149 if constexpr (std::is_same_v<T, Flag<bool>>) {
150 success = parse_bool_flag(item.get(), info.is_short, **iterator);
151 } else if constexpr (std::is_same_v<T, Flag<std::string>>) {
152 success = parse_flag(item.get(), info.is_short, iterator);
153 } else if constexpr (std::is_same_v<T, Flag<uint32_t>>) {
154 success = parse_flag(item.get(), info.is_short, iterator);
155 } else {
156 static_assert(always_false_v<T>, "Unsupported flag type.");
157 }
158 },
159 info.flag);
160
161 return success;
162 }
163
parse(token_t * argv)164 bool FlagList::parse(token_t* argv) {
165 flags::positional_arguments.clear();
166 std::unordered_set<const FlagInfo*> parsed_flags;
167
168 bool ignore_flags = false;
169 for (const char** it = argv + 1; *it != nullptr; it++) {
170 if (ignore_flags) {
171 flags::positional_arguments.emplace_back(*it);
172 continue;
173 }
174
175 // '--' alone is used to mark the end of the flags.
176 if (std::strcmp(*it, "--") == 0) {
177 ignore_flags = true;
178 continue;
179 }
180
181 // '-' alone is not a flag, but often used to say 'stdin'.
182 if (std::strcmp(*it, "-") == 0) {
183 flags::positional_arguments.emplace_back(*it);
184 continue;
185 }
186
187 const std::string raw_flag(*it);
188 if (raw_flag.size() == 0) {
189 continue;
190 }
191
192 if (raw_flag[0] != '-') {
193 flags::positional_arguments.emplace_back(*it);
194 continue;
195 }
196
197 // Only case left: flags (long and shorts).
198 if (raw_flag.size() < 2) {
199 std::cerr << "Unknown flag " << raw_flag << std::endl;
200 return false;
201 }
202 const bool is_short_flag = std::strncmp(*it, "--", 2) != 0;
203 const std::string flag_name = get_flag_name(raw_flag, is_short_flag);
204
205 auto needle = std::find_if(
206 get_flags().begin(), get_flags().end(),
207 [&flag_name](const auto& item) { return item.name == flag_name; });
208 if (needle == get_flags().end()) {
209 std::cerr << "Unknown flag " << flag_name << std::endl;
210 return false;
211 }
212
213 if (parsed_flags.count(&*needle) != 0) {
214 std::cerr << "The flag " << flag_name << " was specified multiple times."
215 << std::endl;
216 return false;
217 }
218 parsed_flags.insert(&*needle);
219
220 if (!parse_flag_info(*needle, &it)) {
221 std::cerr << "Invalid usage for flag " << flag_name << std::endl;
222 return false;
223 }
224 }
225
226 // Check that we parsed all required flags.
227 for (const auto& flag : get_flags()) {
228 if (!flag.required) {
229 continue;
230 }
231
232 if (parsed_flags.count(&flag) == 0) {
233 std::cerr << "Missing required flag " << flag.name << std::endl;
234 return false;
235 }
236 }
237
238 return true;
239 }
240
241 // Just the public wrapper around the parse function.
Parse(const char ** argv)242 bool Parse(const char** argv) { return FlagList::parse(argv); }
243
244 } // namespace flags
245