1
0
mirror of https://github.com/CLIUtils/CLI11.git synced 2025-05-02 21:53:51 +00:00

Deregister help if added previously

This commit is contained in:
Henry Fredrick Schreiner 2017-11-20 10:43:53 -05:00 committed by Henry Schreiner
parent 1b9aac85ca
commit 1c5c8391f7
2 changed files with 10 additions and 6 deletions

View File

@ -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_;
}

View File

@ -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<std::string> 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();