Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion core/rint/src/TRint.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
#include "TTabCom.h"
#include <cstdlib>
#include <algorithm>

#include <iostream>
#include "Getline.h"
#include "strlcpy.h"
#include "snprintf.h"
Expand Down Expand Up @@ -146,6 +146,16 @@ TRint::TRint(const char *appClassName, Int_t *argc, char **argv, void *options,
TApplication(appClassName, argc, argv, options, numOptions),
fCaughtSignal(-1)
{

if (*argc > 1) {
// Early exit if there are remaining unrecognized options
for (auto n = 1; n < *argc; n++) {
std::cerr << "root: unrecognized option '" << argv[n] << "'\n";
}
std::cerr << "Try 'root --help' for more information.\n";
TApplication::Terminate(0);
}

fNcmd = 0;
fDefaultPrompt = "root [%d] ";
fInterrupt = kFALSE;
Expand Down
2 changes: 2 additions & 0 deletions core/rint/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
# For the list of contributors see $ROOTSYS/README/CREDITS.

ROOT_ADD_GTEST(TTabComTests TTabComTests.cxx LIBRARIES Rint)
ROOT_ADD_GTEST(TRintTests TRintTests.cxx LIBRARIES Rint)

43 changes: 43 additions & 0 deletions core/rint/test/TRintTests.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <iostream>
#include <string>

#include "gtest/gtest.h"

#include "TRint.h"

using testing::internal::CaptureStderr;
using testing::internal::GetCapturedStderr;

TEST(TRint, UnrecognizedOptions)
{
// Create array of options.
// We need to create it as a dynamic array for the following reasons:
// - TRint constructor accepts a char** so we construct directly that type
// - TRint will modify this array, removing recognized options and leaving
// only unrecognized ones, so we can't create an std::vector and pass its
// data to TRint directly.
int argc{4};
char **argv = new char *[argc];
argv[0] = const_cast<char *>("-q");
argv[1] = const_cast<char *>("-z");
argv[2] = const_cast<char *>("--nonexistingoption");
argv[3] = const_cast<char *>("-b");

CaptureStderr();
// Unrecognized options will be printed to stderr
TRint app{"App", &argc, argv};
std::string trinterr = GetCapturedStderr();

const std::string expected{"root: unrecognized option '-z'\n"
"root: unrecognized option '--nonexistingoption'\n"
"Try 'root --help' for more information.\n"};

EXPECT_EQ(trinterr, expected);

// Properly delete the array
for (int i = 0; i < argc; i++) {
delete[] argv[i];
}
delete[] argv;
argv = nullptr;
}