diff --git a/include/CLI/App.hpp b/include/CLI/App.hpp index 397879c9..649b49ff 100644 --- a/include/CLI/App.hpp +++ b/include/CLI/App.hpp @@ -331,10 +331,10 @@ class App { return opt; } - /// Add a help flag, currently throws an error if already set + /// Add a help flag, replaced the existing one if present Option *add_help_flag(std::string name, std::string description = "") { - if(help_ptr_) - throw IncorrectConstruction("Help flag already initialized"); + if(help_ptr_ != nullptr) + remove_option(help_ptr_); help_ptr_ = add_flag(name, description); return help_ptr_; } diff --git a/tests/HelpTest.cpp b/tests/HelpTest.cpp index f8e5e4fa..73e4d37a 100644 --- a/tests/HelpTest.cpp +++ b/tests/HelpTest.cpp @@ -286,8 +286,13 @@ TEST(THelp, SetLower) { TEST(THelp, OnlyOneHelp) { CLI::App app{"My prog"}; - /* It is not supported to add more than one help flag. */ - EXPECT_THROW(app.add_help_flag("--yelp", "Alias for help"), CLI::IncorrectConstruction); + // It is not supported to have more than one help flag, last one wins + app.add_help_flag("--help", "No short name allowed"); + app.add_help_flag("--yelp", "Alias for help"); + + std::vector input{"--help"}; + EXPECT_THROW(app.parse(input), CLI::ExtrasError); + } TEST(THelp, NoHelp) { @@ -313,7 +318,6 @@ TEST(THelp, CustomHelp) { CLI::Option *help_option = app.add_help_flag("--yelp", "display help and exit"); EXPECT_EQ(app.get_help_ptr(), help_option); - EXPECT_THROW(app.add_help_flag("--help", "Alias for yelp"), CLI::IncorrectConstruction); std::string help = app.help();