Skip to content

Commit e73b913

Browse files
rogeeffcopybara-github
authored andcommitted
Use stdout for help output even in case of errors.
PiperOrigin-RevId: 522393331 Change-Id: Ia5f4ad6a2d16c033ea97f3c7e27e8eb7ee429242
1 parent 0bc6509 commit e73b913

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

absl/flags/internal/parse.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#ifndef ABSL_FLAGS_INTERNAL_PARSE_H_
1717
#define ABSL_FLAGS_INTERNAL_PARSE_H_
1818

19+
#include <iostream>
20+
#include <ostream>
1921
#include <string>
2022
#include <vector>
2123

@@ -40,9 +42,15 @@ enum class OnUndefinedFlag {
4042
kAbortIfUndefined
4143
};
4244

43-
std::vector<char*> ParseCommandLineImpl(int argc, char* argv[],
44-
UsageFlagsAction usage_flag_action,
45-
OnUndefinedFlag undef_flag_action);
45+
// This is not a public interface. This interface exists to expose the ability
46+
// to change help output stream in case of parsing errors. This is used by
47+
// internal unit tests to validate expected outputs.
48+
// When this was written, `EXPECT_EXIT` only supported matchers on stderr,
49+
// but not on stdout.
50+
std::vector<char*> ParseCommandLineImpl(
51+
int argc, char* argv[], UsageFlagsAction usage_flag_action,
52+
OnUndefinedFlag undef_flag_action,
53+
std::ostream& error_help_output = std::cout);
4654

4755
// --------------------------------------------------------------------
4856
// Inspect original command line

absl/flags/parse.cc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <cstdlib>
2323
#include <fstream>
2424
#include <iostream>
25+
#include <ostream>
2526
#include <string>
2627
#include <tuple>
2728
#include <utility>
@@ -693,7 +694,8 @@ std::vector<std::string> GetMisspellingHints(const absl::string_view flag) {
693694

694695
std::vector<char*> ParseCommandLineImpl(int argc, char* argv[],
695696
UsageFlagsAction usage_flag_action,
696-
OnUndefinedFlag undef_flag_action) {
697+
OnUndefinedFlag undef_flag_action,
698+
std::ostream& error_help_output) {
697699
std::vector<char*> positional_args;
698700
std::vector<UnrecognizedFlag> unrecognized_flags;
699701

@@ -707,8 +709,8 @@ std::vector<char*> ParseCommandLineImpl(int argc, char* argv[],
707709

708710
if (undef_flag_action == OnUndefinedFlag::kAbortIfUndefined) {
709711
if (!unrecognized_flags.empty()) {
710-
flags_internal::HandleUsageFlags(std::cerr, ProgramUsageMessage());
711-
std::exit(1);
712+
flags_internal::HandleUsageFlags(error_help_output,
713+
ProgramUsageMessage()); std::exit(1);
712714
}
713715
}
714716
}

absl/flags/parse_test.cc

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <stdlib.h>
1919

2020
#include <fstream>
21+
#include <iostream>
2122
#include <string>
2223
#include <vector>
2324

@@ -235,7 +236,9 @@ ABSL_RETIRED_FLAG(std::string, legacy_str, "l", "");
235236
namespace {
236237

237238
namespace flags = absl::flags_internal;
239+
using testing::AllOf;
238240
using testing::ElementsAreArray;
241+
using testing::HasSubstr;
239242

240243
class ParseTest : public testing::Test {
241244
public:
@@ -270,6 +273,15 @@ void InvokeParseAbslOnly(const char* (&in_argv)[N]) {
270273

271274
// --------------------------------------------------------------------
272275

276+
template <int N>
277+
std::vector<char*> InvokeParseCommandLineImpl(const char* (&in_argv)[N]) {
278+
return flags::ParseCommandLineImpl(
279+
N, const_cast<char**>(in_argv), flags::UsageFlagsAction::kHandleUsage,
280+
flags::OnUndefinedFlag::kAbortIfUndefined, std::cerr);
281+
}
282+
283+
// --------------------------------------------------------------------
284+
273285
template <int N>
274286
std::vector<char*> InvokeParse(const char* (&in_argv)[N]) {
275287
return absl::ParseCommandLine(N, const_cast<char**>(in_argv));
@@ -1066,8 +1078,9 @@ TEST_F(ParseDeathTest, ExitOnUnrecognizedFlagPrintsHelp) {
10661078
"--help=int_flag",
10671079
};
10681080

1069-
EXPECT_DEATH_IF_SUPPORTED(InvokeParse(in_args),
1070-
"Try --helpfull to get a list of all flags");
1081+
EXPECT_EXIT(InvokeParseCommandLineImpl(in_args), testing::ExitedWithCode(1),
1082+
AllOf(HasSubstr("Unknown command line flag 'undef_flag1'"),
1083+
HasSubstr("Try --helpfull to get a list of all flags")));
10711084
}
10721085

10731086
// --------------------------------------------------------------------

0 commit comments

Comments
 (0)