Skip to content

Commit

Permalink
The add subcommand function was not exception safe. It added the subc…
Browse files Browse the repository at this point in the history
…ommand to the vector before checking the already added option. This would result in duplicate subcommands being in place in the subcommands_ vector. The modifications make it exception safe and remove what I think was an unnecessary check for pointer duplication, that as far as I can tell was always false since it was comparing a newly created pointer directly to previously created ones.
  • Loading branch information
phlptp authored and henryiii committed Jan 25, 2019
1 parent 478f582 commit 45496a8
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions include/CLI/App.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1080,11 +1080,11 @@ class App {

/// Add a subcommand. Inherits INHERITABLE and OptionDefaults, and help flag
App *add_subcommand(std::string subcommand_name, std::string description = "") {
subcommands_.emplace_back(new App(description, subcommand_name, this));
CLI::App_p subcom(new App(description, subcommand_name, this));
for(const auto &subc : subcommands_)
if(subc.get() != subcommands_.back().get())
if(subc->check_name(subcommands_.back()->name_) || subcommands_.back()->check_name(subc->name_))
throw OptionAlreadyAdded(subc->name_);
if(subc->check_name(subcommand_name) || subcom->check_name(subc->name_))
throw OptionAlreadyAdded(subc->name_);
subcommands_.push_back(std::move(subcom));
return subcommands_.back().get();
}

Expand Down

0 comments on commit 45496a8

Please sign in to comment.