blob: b5c2487da577d5d849f16d1c106f3e3bc42b9d5f [file] [log] [blame]
Austin Schuh36244a12019-09-21 17:52:38 -07001//
2// Copyright 2019 The Abseil Authors.
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// https://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#include "absl/flags/internal/usage.h"
17
Austin Schuhb4691e92020-12-31 12:37:18 -080018#include <stdint.h>
19
Austin Schuh36244a12019-09-21 17:52:38 -070020#include <sstream>
Austin Schuhb4691e92020-12-31 12:37:18 -080021#include <string>
Austin Schuh36244a12019-09-21 17:52:38 -070022
23#include "gtest/gtest.h"
24#include "absl/flags/flag.h"
Austin Schuhb4691e92020-12-31 12:37:18 -080025#include "absl/flags/internal/parse.h"
Austin Schuh36244a12019-09-21 17:52:38 -070026#include "absl/flags/internal/path_util.h"
27#include "absl/flags/internal/program_name.h"
Austin Schuhb4691e92020-12-31 12:37:18 -080028#include "absl/flags/reflection.h"
Austin Schuh36244a12019-09-21 17:52:38 -070029#include "absl/flags/usage.h"
30#include "absl/flags/usage_config.h"
Austin Schuh36244a12019-09-21 17:52:38 -070031#include "absl/strings/match.h"
Austin Schuhb4691e92020-12-31 12:37:18 -080032#include "absl/strings/string_view.h"
Austin Schuh36244a12019-09-21 17:52:38 -070033
34ABSL_FLAG(int, usage_reporting_test_flag_01, 101,
35 "usage_reporting_test_flag_01 help message");
36ABSL_FLAG(bool, usage_reporting_test_flag_02, false,
37 "usage_reporting_test_flag_02 help message");
38ABSL_FLAG(double, usage_reporting_test_flag_03, 1.03,
39 "usage_reporting_test_flag_03 help message");
40ABSL_FLAG(int64_t, usage_reporting_test_flag_04, 1000000000000004L,
41 "usage_reporting_test_flag_04 help message");
42
43static const char kTestUsageMessage[] = "Custom usage message";
44
45struct UDT {
46 UDT() = default;
47 UDT(const UDT&) = default;
48};
49bool AbslParseFlag(absl::string_view, UDT*, std::string*) { return true; }
50std::string AbslUnparseFlag(const UDT&) { return "UDT{}"; }
51
52ABSL_FLAG(UDT, usage_reporting_test_flag_05, {},
53 "usage_reporting_test_flag_05 help message");
54
55ABSL_FLAG(
56 std::string, usage_reporting_test_flag_06, {},
57 "usage_reporting_test_flag_06 help message.\n"
58 "\n"
59 "Some more help.\n"
60 "Even more long long long long long long long long long long long long "
61 "help message.");
62
63namespace {
64
65namespace flags = absl::flags_internal;
66
67static std::string NormalizeFileName(absl::string_view fname) {
68#ifdef _WIN32
69 std::string normalized(fname);
70 std::replace(normalized.begin(), normalized.end(), '\\', '/');
71 fname = normalized;
72#endif
73
74 auto absl_pos = fname.rfind("absl/");
75 if (absl_pos != absl::string_view::npos) {
76 fname = fname.substr(absl_pos);
77 }
78 return std::string(fname);
79}
80
81class UsageReportingTest : public testing::Test {
82 protected:
83 UsageReportingTest() {
84 // Install default config for the use on this unit test.
85 // Binary may install a custom config before tests are run.
86 absl::FlagsUsageConfig default_config;
87 default_config.normalize_filename = &NormalizeFileName;
88 absl::SetFlagsUsageConfig(default_config);
89 }
Austin Schuhb4691e92020-12-31 12:37:18 -080090 ~UsageReportingTest() override {
91 flags::SetFlagsHelpMode(flags::HelpMode::kNone);
92 flags::SetFlagsHelpMatchSubstr("");
93 flags::SetFlagsHelpFormat(flags::HelpFormat::kHumanReadable);
94 }
Austin Schuh36244a12019-09-21 17:52:38 -070095
96 private:
Austin Schuhb4691e92020-12-31 12:37:18 -080097 absl::FlagSaver flag_saver_;
Austin Schuh36244a12019-09-21 17:52:38 -070098};
99
100// --------------------------------------------------------------------
101
102using UsageReportingDeathTest = UsageReportingTest;
103
104TEST_F(UsageReportingDeathTest, TestSetProgramUsageMessage) {
105 EXPECT_EQ(absl::ProgramUsageMessage(), kTestUsageMessage);
106
107#ifndef _WIN32
108 // TODO(rogeeff): figure out why this does not work on Windows.
Austin Schuhb4691e92020-12-31 12:37:18 -0800109 EXPECT_DEATH_IF_SUPPORTED(
110 absl::SetProgramUsageMessage("custom usage message"),
111 ".*SetProgramUsageMessage\\(\\) called twice.*");
Austin Schuh36244a12019-09-21 17:52:38 -0700112#endif
113}
114
115// --------------------------------------------------------------------
116
117TEST_F(UsageReportingTest, TestFlagHelpHRF_on_flag_01) {
Austin Schuhb4691e92020-12-31 12:37:18 -0800118 const auto* flag = absl::FindCommandLineFlag("usage_reporting_test_flag_01");
Austin Schuh36244a12019-09-21 17:52:38 -0700119 std::stringstream test_buf;
120
121 flags::FlagHelp(test_buf, *flag, flags::HelpFormat::kHumanReadable);
122 EXPECT_EQ(
123 test_buf.str(),
Austin Schuhb4691e92020-12-31 12:37:18 -0800124 R"( --usage_reporting_test_flag_01 (usage_reporting_test_flag_01 help message);
Austin Schuh36244a12019-09-21 17:52:38 -0700125 default: 101;
126)");
127}
128
129TEST_F(UsageReportingTest, TestFlagHelpHRF_on_flag_02) {
Austin Schuhb4691e92020-12-31 12:37:18 -0800130 const auto* flag = absl::FindCommandLineFlag("usage_reporting_test_flag_02");
Austin Schuh36244a12019-09-21 17:52:38 -0700131 std::stringstream test_buf;
132
133 flags::FlagHelp(test_buf, *flag, flags::HelpFormat::kHumanReadable);
134 EXPECT_EQ(
135 test_buf.str(),
Austin Schuhb4691e92020-12-31 12:37:18 -0800136 R"( --usage_reporting_test_flag_02 (usage_reporting_test_flag_02 help message);
Austin Schuh36244a12019-09-21 17:52:38 -0700137 default: false;
138)");
139}
140
141TEST_F(UsageReportingTest, TestFlagHelpHRF_on_flag_03) {
Austin Schuhb4691e92020-12-31 12:37:18 -0800142 const auto* flag = absl::FindCommandLineFlag("usage_reporting_test_flag_03");
Austin Schuh36244a12019-09-21 17:52:38 -0700143 std::stringstream test_buf;
144
145 flags::FlagHelp(test_buf, *flag, flags::HelpFormat::kHumanReadable);
146 EXPECT_EQ(
147 test_buf.str(),
Austin Schuhb4691e92020-12-31 12:37:18 -0800148 R"( --usage_reporting_test_flag_03 (usage_reporting_test_flag_03 help message);
Austin Schuh36244a12019-09-21 17:52:38 -0700149 default: 1.03;
150)");
151}
152
153TEST_F(UsageReportingTest, TestFlagHelpHRF_on_flag_04) {
Austin Schuhb4691e92020-12-31 12:37:18 -0800154 const auto* flag = absl::FindCommandLineFlag("usage_reporting_test_flag_04");
Austin Schuh36244a12019-09-21 17:52:38 -0700155 std::stringstream test_buf;
156
157 flags::FlagHelp(test_buf, *flag, flags::HelpFormat::kHumanReadable);
158 EXPECT_EQ(
159 test_buf.str(),
Austin Schuhb4691e92020-12-31 12:37:18 -0800160 R"( --usage_reporting_test_flag_04 (usage_reporting_test_flag_04 help message);
Austin Schuh36244a12019-09-21 17:52:38 -0700161 default: 1000000000000004;
162)");
163}
164
165TEST_F(UsageReportingTest, TestFlagHelpHRF_on_flag_05) {
Austin Schuhb4691e92020-12-31 12:37:18 -0800166 const auto* flag = absl::FindCommandLineFlag("usage_reporting_test_flag_05");
Austin Schuh36244a12019-09-21 17:52:38 -0700167 std::stringstream test_buf;
168
169 flags::FlagHelp(test_buf, *flag, flags::HelpFormat::kHumanReadable);
170 EXPECT_EQ(
171 test_buf.str(),
Austin Schuhb4691e92020-12-31 12:37:18 -0800172 R"( --usage_reporting_test_flag_05 (usage_reporting_test_flag_05 help message);
Austin Schuh36244a12019-09-21 17:52:38 -0700173 default: UDT{};
174)");
175}
176
177// --------------------------------------------------------------------
178
179TEST_F(UsageReportingTest, TestFlagsHelpHRF) {
180 std::string usage_test_flags_out =
181 R"(usage_test: Custom usage message
182
183 Flags from absl/flags/internal/usage_test.cc:
Austin Schuhb4691e92020-12-31 12:37:18 -0800184 --usage_reporting_test_flag_01 (usage_reporting_test_flag_01 help message);
Austin Schuh36244a12019-09-21 17:52:38 -0700185 default: 101;
Austin Schuhb4691e92020-12-31 12:37:18 -0800186 --usage_reporting_test_flag_02 (usage_reporting_test_flag_02 help message);
Austin Schuh36244a12019-09-21 17:52:38 -0700187 default: false;
Austin Schuhb4691e92020-12-31 12:37:18 -0800188 --usage_reporting_test_flag_03 (usage_reporting_test_flag_03 help message);
Austin Schuh36244a12019-09-21 17:52:38 -0700189 default: 1.03;
Austin Schuhb4691e92020-12-31 12:37:18 -0800190 --usage_reporting_test_flag_04 (usage_reporting_test_flag_04 help message);
Austin Schuh36244a12019-09-21 17:52:38 -0700191 default: 1000000000000004;
Austin Schuhb4691e92020-12-31 12:37:18 -0800192 --usage_reporting_test_flag_05 (usage_reporting_test_flag_05 help message);
Austin Schuh36244a12019-09-21 17:52:38 -0700193 default: UDT{};
Austin Schuhb4691e92020-12-31 12:37:18 -0800194 --usage_reporting_test_flag_06 (usage_reporting_test_flag_06 help message.
Austin Schuh36244a12019-09-21 17:52:38 -0700195
196 Some more help.
197 Even more long long long long long long long long long long long long help
198 message.); default: "";
Austin Schuhb4691e92020-12-31 12:37:18 -0800199
200Try --helpfull to get a list of all flags or --help=substring shows help for
201flags which include specified substring in either in the name, or description or
202path.
Austin Schuh36244a12019-09-21 17:52:38 -0700203)";
204
205 std::stringstream test_buf_01;
206 flags::FlagsHelp(test_buf_01, "usage_test.cc",
207 flags::HelpFormat::kHumanReadable, kTestUsageMessage);
208 EXPECT_EQ(test_buf_01.str(), usage_test_flags_out);
209
210 std::stringstream test_buf_02;
211 flags::FlagsHelp(test_buf_02, "flags/internal/usage_test.cc",
212 flags::HelpFormat::kHumanReadable, kTestUsageMessage);
213 EXPECT_EQ(test_buf_02.str(), usage_test_flags_out);
214
215 std::stringstream test_buf_03;
216 flags::FlagsHelp(test_buf_03, "usage_test", flags::HelpFormat::kHumanReadable,
217 kTestUsageMessage);
218 EXPECT_EQ(test_buf_03.str(), usage_test_flags_out);
219
220 std::stringstream test_buf_04;
221 flags::FlagsHelp(test_buf_04, "flags/invalid_file_name.cc",
222 flags::HelpFormat::kHumanReadable, kTestUsageMessage);
223 EXPECT_EQ(test_buf_04.str(),
224 R"(usage_test: Custom usage message
225
Austin Schuhb4691e92020-12-31 12:37:18 -0800226No flags matched.
227
228Try --helpfull to get a list of all flags or --help=substring shows help for
229flags which include specified substring in either in the name, or description or
230path.
Austin Schuh36244a12019-09-21 17:52:38 -0700231)");
232
233 std::stringstream test_buf_05;
234 flags::FlagsHelp(test_buf_05, "", flags::HelpFormat::kHumanReadable,
235 kTestUsageMessage);
236 std::string test_out = test_buf_05.str();
237 absl::string_view test_out_str(test_out);
238 EXPECT_TRUE(
239 absl::StartsWith(test_out_str, "usage_test: Custom usage message"));
240 EXPECT_TRUE(absl::StrContains(
241 test_out_str, "Flags from absl/flags/internal/usage_test.cc:"));
Austin Schuh36244a12019-09-21 17:52:38 -0700242 EXPECT_TRUE(
243 absl::StrContains(test_out_str, "-usage_reporting_test_flag_01 "));
Austin Schuh36244a12019-09-21 17:52:38 -0700244}
245
246// --------------------------------------------------------------------
247
248TEST_F(UsageReportingTest, TestNoUsageFlags) {
249 std::stringstream test_buf;
250 EXPECT_EQ(flags::HandleUsageFlags(test_buf, kTestUsageMessage), -1);
251}
252
253// --------------------------------------------------------------------
254
255TEST_F(UsageReportingTest, TestUsageFlag_helpshort) {
Austin Schuhb4691e92020-12-31 12:37:18 -0800256 flags::SetFlagsHelpMode(flags::HelpMode::kShort);
Austin Schuh36244a12019-09-21 17:52:38 -0700257
258 std::stringstream test_buf;
259 EXPECT_EQ(flags::HandleUsageFlags(test_buf, kTestUsageMessage), 1);
260 EXPECT_EQ(test_buf.str(),
261 R"(usage_test: Custom usage message
262
263 Flags from absl/flags/internal/usage_test.cc:
Austin Schuhb4691e92020-12-31 12:37:18 -0800264 --usage_reporting_test_flag_01 (usage_reporting_test_flag_01 help message);
Austin Schuh36244a12019-09-21 17:52:38 -0700265 default: 101;
Austin Schuhb4691e92020-12-31 12:37:18 -0800266 --usage_reporting_test_flag_02 (usage_reporting_test_flag_02 help message);
Austin Schuh36244a12019-09-21 17:52:38 -0700267 default: false;
Austin Schuhb4691e92020-12-31 12:37:18 -0800268 --usage_reporting_test_flag_03 (usage_reporting_test_flag_03 help message);
Austin Schuh36244a12019-09-21 17:52:38 -0700269 default: 1.03;
Austin Schuhb4691e92020-12-31 12:37:18 -0800270 --usage_reporting_test_flag_04 (usage_reporting_test_flag_04 help message);
Austin Schuh36244a12019-09-21 17:52:38 -0700271 default: 1000000000000004;
Austin Schuhb4691e92020-12-31 12:37:18 -0800272 --usage_reporting_test_flag_05 (usage_reporting_test_flag_05 help message);
Austin Schuh36244a12019-09-21 17:52:38 -0700273 default: UDT{};
Austin Schuhb4691e92020-12-31 12:37:18 -0800274 --usage_reporting_test_flag_06 (usage_reporting_test_flag_06 help message.
Austin Schuh36244a12019-09-21 17:52:38 -0700275
276 Some more help.
277 Even more long long long long long long long long long long long long help
278 message.); default: "";
Austin Schuhb4691e92020-12-31 12:37:18 -0800279
280Try --helpfull to get a list of all flags or --help=substring shows help for
281flags which include specified substring in either in the name, or description or
282path.
Austin Schuh36244a12019-09-21 17:52:38 -0700283)");
284}
285
286// --------------------------------------------------------------------
287
Austin Schuhb4691e92020-12-31 12:37:18 -0800288TEST_F(UsageReportingTest, TestUsageFlag_help_simple) {
289 flags::SetFlagsHelpMode(flags::HelpMode::kImportant);
Austin Schuh36244a12019-09-21 17:52:38 -0700290
291 std::stringstream test_buf;
292 EXPECT_EQ(flags::HandleUsageFlags(test_buf, kTestUsageMessage), 1);
293 EXPECT_EQ(test_buf.str(),
294 R"(usage_test: Custom usage message
295
296 Flags from absl/flags/internal/usage_test.cc:
Austin Schuhb4691e92020-12-31 12:37:18 -0800297 --usage_reporting_test_flag_01 (usage_reporting_test_flag_01 help message);
Austin Schuh36244a12019-09-21 17:52:38 -0700298 default: 101;
Austin Schuhb4691e92020-12-31 12:37:18 -0800299 --usage_reporting_test_flag_02 (usage_reporting_test_flag_02 help message);
Austin Schuh36244a12019-09-21 17:52:38 -0700300 default: false;
Austin Schuhb4691e92020-12-31 12:37:18 -0800301 --usage_reporting_test_flag_03 (usage_reporting_test_flag_03 help message);
Austin Schuh36244a12019-09-21 17:52:38 -0700302 default: 1.03;
Austin Schuhb4691e92020-12-31 12:37:18 -0800303 --usage_reporting_test_flag_04 (usage_reporting_test_flag_04 help message);
Austin Schuh36244a12019-09-21 17:52:38 -0700304 default: 1000000000000004;
Austin Schuhb4691e92020-12-31 12:37:18 -0800305 --usage_reporting_test_flag_05 (usage_reporting_test_flag_05 help message);
Austin Schuh36244a12019-09-21 17:52:38 -0700306 default: UDT{};
Austin Schuhb4691e92020-12-31 12:37:18 -0800307 --usage_reporting_test_flag_06 (usage_reporting_test_flag_06 help message.
Austin Schuh36244a12019-09-21 17:52:38 -0700308
309 Some more help.
310 Even more long long long long long long long long long long long long help
311 message.); default: "";
312
Austin Schuhb4691e92020-12-31 12:37:18 -0800313Try --helpfull to get a list of all flags or --help=substring shows help for
314flags which include specified substring in either in the name, or description or
315path.
316)");
317}
318
319// --------------------------------------------------------------------
320
321TEST_F(UsageReportingTest, TestUsageFlag_help_one_flag) {
322 flags::SetFlagsHelpMode(flags::HelpMode::kMatch);
323 flags::SetFlagsHelpMatchSubstr("usage_reporting_test_flag_06");
324
325 std::stringstream test_buf;
326 EXPECT_EQ(flags::HandleUsageFlags(test_buf, kTestUsageMessage), 1);
327 EXPECT_EQ(test_buf.str(),
328 R"(usage_test: Custom usage message
329
330 Flags from absl/flags/internal/usage_test.cc:
331 --usage_reporting_test_flag_06 (usage_reporting_test_flag_06 help message.
332
333 Some more help.
334 Even more long long long long long long long long long long long long help
335 message.); default: "";
336
337Try --helpfull to get a list of all flags or --help=substring shows help for
338flags which include specified substring in either in the name, or description or
339path.
340)");
341}
342
343// --------------------------------------------------------------------
344
345TEST_F(UsageReportingTest, TestUsageFlag_help_multiple_flag) {
346 flags::SetFlagsHelpMode(flags::HelpMode::kMatch);
347 flags::SetFlagsHelpMatchSubstr("test_flag");
348
349 std::stringstream test_buf;
350 EXPECT_EQ(flags::HandleUsageFlags(test_buf, kTestUsageMessage), 1);
351 EXPECT_EQ(test_buf.str(),
352 R"(usage_test: Custom usage message
353
354 Flags from absl/flags/internal/usage_test.cc:
355 --usage_reporting_test_flag_01 (usage_reporting_test_flag_01 help message);
356 default: 101;
357 --usage_reporting_test_flag_02 (usage_reporting_test_flag_02 help message);
358 default: false;
359 --usage_reporting_test_flag_03 (usage_reporting_test_flag_03 help message);
360 default: 1.03;
361 --usage_reporting_test_flag_04 (usage_reporting_test_flag_04 help message);
362 default: 1000000000000004;
363 --usage_reporting_test_flag_05 (usage_reporting_test_flag_05 help message);
364 default: UDT{};
365 --usage_reporting_test_flag_06 (usage_reporting_test_flag_06 help message.
366
367 Some more help.
368 Even more long long long long long long long long long long long long help
369 message.); default: "";
370
371Try --helpfull to get a list of all flags or --help=substring shows help for
372flags which include specified substring in either in the name, or description or
373path.
Austin Schuh36244a12019-09-21 17:52:38 -0700374)");
375}
376
377// --------------------------------------------------------------------
378
379TEST_F(UsageReportingTest, TestUsageFlag_helppackage) {
Austin Schuhb4691e92020-12-31 12:37:18 -0800380 flags::SetFlagsHelpMode(flags::HelpMode::kPackage);
Austin Schuh36244a12019-09-21 17:52:38 -0700381
382 std::stringstream test_buf;
383 EXPECT_EQ(flags::HandleUsageFlags(test_buf, kTestUsageMessage), 1);
384 EXPECT_EQ(test_buf.str(),
385 R"(usage_test: Custom usage message
386
387 Flags from absl/flags/internal/usage_test.cc:
Austin Schuhb4691e92020-12-31 12:37:18 -0800388 --usage_reporting_test_flag_01 (usage_reporting_test_flag_01 help message);
Austin Schuh36244a12019-09-21 17:52:38 -0700389 default: 101;
Austin Schuhb4691e92020-12-31 12:37:18 -0800390 --usage_reporting_test_flag_02 (usage_reporting_test_flag_02 help message);
Austin Schuh36244a12019-09-21 17:52:38 -0700391 default: false;
Austin Schuhb4691e92020-12-31 12:37:18 -0800392 --usage_reporting_test_flag_03 (usage_reporting_test_flag_03 help message);
Austin Schuh36244a12019-09-21 17:52:38 -0700393 default: 1.03;
Austin Schuhb4691e92020-12-31 12:37:18 -0800394 --usage_reporting_test_flag_04 (usage_reporting_test_flag_04 help message);
Austin Schuh36244a12019-09-21 17:52:38 -0700395 default: 1000000000000004;
Austin Schuhb4691e92020-12-31 12:37:18 -0800396 --usage_reporting_test_flag_05 (usage_reporting_test_flag_05 help message);
Austin Schuh36244a12019-09-21 17:52:38 -0700397 default: UDT{};
Austin Schuhb4691e92020-12-31 12:37:18 -0800398 --usage_reporting_test_flag_06 (usage_reporting_test_flag_06 help message.
Austin Schuh36244a12019-09-21 17:52:38 -0700399
400 Some more help.
401 Even more long long long long long long long long long long long long help
402 message.); default: "";
403
Austin Schuhb4691e92020-12-31 12:37:18 -0800404Try --helpfull to get a list of all flags or --help=substring shows help for
405flags which include specified substring in either in the name, or description or
406path.
Austin Schuh36244a12019-09-21 17:52:38 -0700407)");
408}
409
410// --------------------------------------------------------------------
411
412TEST_F(UsageReportingTest, TestUsageFlag_version) {
Austin Schuhb4691e92020-12-31 12:37:18 -0800413 flags::SetFlagsHelpMode(flags::HelpMode::kVersion);
Austin Schuh36244a12019-09-21 17:52:38 -0700414
415 std::stringstream test_buf;
416 EXPECT_EQ(flags::HandleUsageFlags(test_buf, kTestUsageMessage), 0);
417#ifndef NDEBUG
418 EXPECT_EQ(test_buf.str(), "usage_test\nDebug build (NDEBUG not #defined)\n");
419#else
420 EXPECT_EQ(test_buf.str(), "usage_test\n");
421#endif
422}
423
424// --------------------------------------------------------------------
425
426TEST_F(UsageReportingTest, TestUsageFlag_only_check_args) {
Austin Schuhb4691e92020-12-31 12:37:18 -0800427 flags::SetFlagsHelpMode(flags::HelpMode::kOnlyCheckArgs);
Austin Schuh36244a12019-09-21 17:52:38 -0700428
429 std::stringstream test_buf;
430 EXPECT_EQ(flags::HandleUsageFlags(test_buf, kTestUsageMessage), 0);
431 EXPECT_EQ(test_buf.str(), "");
432}
433
434// --------------------------------------------------------------------
435
436TEST_F(UsageReportingTest, TestUsageFlag_helpon) {
Austin Schuhb4691e92020-12-31 12:37:18 -0800437 flags::SetFlagsHelpMode(flags::HelpMode::kMatch);
438 flags::SetFlagsHelpMatchSubstr("/bla-bla.");
Austin Schuh36244a12019-09-21 17:52:38 -0700439
440 std::stringstream test_buf_01;
441 EXPECT_EQ(flags::HandleUsageFlags(test_buf_01, kTestUsageMessage), 1);
442 EXPECT_EQ(test_buf_01.str(),
443 R"(usage_test: Custom usage message
444
Austin Schuhb4691e92020-12-31 12:37:18 -0800445No flags matched.
446
447Try --helpfull to get a list of all flags or --help=substring shows help for
448flags which include specified substring in either in the name, or description or
449path.
Austin Schuh36244a12019-09-21 17:52:38 -0700450)");
451
Austin Schuhb4691e92020-12-31 12:37:18 -0800452 flags::SetFlagsHelpMatchSubstr("/usage_test.");
Austin Schuh36244a12019-09-21 17:52:38 -0700453
454 std::stringstream test_buf_02;
455 EXPECT_EQ(flags::HandleUsageFlags(test_buf_02, kTestUsageMessage), 1);
456 EXPECT_EQ(test_buf_02.str(),
457 R"(usage_test: Custom usage message
458
459 Flags from absl/flags/internal/usage_test.cc:
Austin Schuhb4691e92020-12-31 12:37:18 -0800460 --usage_reporting_test_flag_01 (usage_reporting_test_flag_01 help message);
Austin Schuh36244a12019-09-21 17:52:38 -0700461 default: 101;
Austin Schuhb4691e92020-12-31 12:37:18 -0800462 --usage_reporting_test_flag_02 (usage_reporting_test_flag_02 help message);
Austin Schuh36244a12019-09-21 17:52:38 -0700463 default: false;
Austin Schuhb4691e92020-12-31 12:37:18 -0800464 --usage_reporting_test_flag_03 (usage_reporting_test_flag_03 help message);
Austin Schuh36244a12019-09-21 17:52:38 -0700465 default: 1.03;
Austin Schuhb4691e92020-12-31 12:37:18 -0800466 --usage_reporting_test_flag_04 (usage_reporting_test_flag_04 help message);
Austin Schuh36244a12019-09-21 17:52:38 -0700467 default: 1000000000000004;
Austin Schuhb4691e92020-12-31 12:37:18 -0800468 --usage_reporting_test_flag_05 (usage_reporting_test_flag_05 help message);
Austin Schuh36244a12019-09-21 17:52:38 -0700469 default: UDT{};
Austin Schuhb4691e92020-12-31 12:37:18 -0800470 --usage_reporting_test_flag_06 (usage_reporting_test_flag_06 help message.
Austin Schuh36244a12019-09-21 17:52:38 -0700471
472 Some more help.
473 Even more long long long long long long long long long long long long help
474 message.); default: "";
Austin Schuhb4691e92020-12-31 12:37:18 -0800475
476Try --helpfull to get a list of all flags or --help=substring shows help for
477flags which include specified substring in either in the name, or description or
478path.
Austin Schuh36244a12019-09-21 17:52:38 -0700479)");
480}
481
482// --------------------------------------------------------------------
483
484} // namespace
485
486int main(int argc, char* argv[]) {
487 (void)absl::GetFlag(FLAGS_undefok); // Force linking of parse.cc
488 flags::SetProgramInvocationName("usage_test");
489 absl::SetProgramUsageMessage(kTestUsageMessage);
490 ::testing::InitGoogleTest(&argc, argv);
491
492 return RUN_ALL_TESTS();
493}