Skip to content

Commit

Permalink
Reword help message to include help_all flag (#197)
Browse files Browse the repository at this point in the history
* Reword help message to include help_all flag

* Adding test for combined simple message
  • Loading branch information
nurelin authored and henryiii committed Jan 18, 2019
1 parent c65d9fd commit 1933a21
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
23 changes: 21 additions & 2 deletions include/CLI/App.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2023,9 +2023,28 @@ namespace FailureMessage {

/// Printout a clean, simple message on error (the default in CLI11 1.5+)
inline std::string simple(const App *app, const Error &e) {
const bool has_help = app->get_help_ptr() != nullptr;
const bool has_help_all = app->get_help_all_ptr() != nullptr;

std::string header = std::string(e.what()) + "\n";
if(app->get_help_ptr() != nullptr)
header += "Run with " + app->get_help_ptr()->get_name() + " for more information.\n";

if(has_help || has_help_all) {
header += "Run with ";

if(has_help) {
header += app->get_help_ptr()->get_name();
}

if(has_help_all) {
if(has_help) {
header += " or ";
}
header += app->get_help_all_ptr()->get_name();
}

header += " for more information.\n";
}

return header;
}

Expand Down
28 changes: 28 additions & 0 deletions tests/HelpTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,34 @@ TEST_F(CapturedHelp, NormalError) {
EXPECT_THAT(err.str(), HasSubstr("for more information"));
EXPECT_THAT(err.str(), Not(HasSubstr("ExtrasError")));
EXPECT_THAT(err.str(), HasSubstr("Thing"));
EXPECT_THAT(err.str(), Not(HasSubstr(" or ")));
EXPECT_THAT(err.str(), Not(HasSubstr("Usage")));
}

TEST_F(CapturedHelp, DoubleError) {
app.set_help_all_flag("--help-all");
EXPECT_EQ(run(CLI::ExtrasError({"Thing"})), static_cast<int>(CLI::ExitCodes::ExtrasError));
EXPECT_EQ(out.str(), "");
EXPECT_THAT(err.str(), HasSubstr("for more information"));
EXPECT_THAT(err.str(), HasSubstr(" --help "));
EXPECT_THAT(err.str(), HasSubstr(" --help-all "));
EXPECT_THAT(err.str(), HasSubstr(" or "));
EXPECT_THAT(err.str(), Not(HasSubstr("ExtrasError")));
EXPECT_THAT(err.str(), HasSubstr("Thing"));
EXPECT_THAT(err.str(), Not(HasSubstr("Usage")));
}

TEST_F(CapturedHelp, AllOnlyError) {
app.set_help_all_flag("--help-all");
app.set_help_flag();
EXPECT_EQ(run(CLI::ExtrasError({"Thing"})), static_cast<int>(CLI::ExitCodes::ExtrasError));
EXPECT_EQ(out.str(), "");
EXPECT_THAT(err.str(), HasSubstr("for more information"));
EXPECT_THAT(err.str(), Not(HasSubstr(" --help ")));
EXPECT_THAT(err.str(), HasSubstr(" --help-all "));
EXPECT_THAT(err.str(), Not(HasSubstr(" or ")));
EXPECT_THAT(err.str(), Not(HasSubstr("ExtrasError")));
EXPECT_THAT(err.str(), HasSubstr("Thing"));
EXPECT_THAT(err.str(), Not(HasSubstr("Usage")));
}

Expand Down

0 comments on commit 1933a21

Please sign in to comment.