Skip to content

Commit

Permalink
short-circuit if sub_parser set
Browse files Browse the repository at this point in the history
  • Loading branch information
eseiler committed Feb 9, 2024
1 parent fc4c414 commit 37f9117
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions include/sharg/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -793,19 +793,26 @@ class parser
{
assert(!original_arguments.empty());

// If init() is called multiple times (via add_subcommands):
// Start: If init() is called multiple times (via add_subcommands).

// * If sub_parser is set, nothing needs to be done. There can only ever be one subparser.
if (sub_parser)
return;

// * We need to clear cmd_arguments. They will be parsed again.
cmd_arguments.clear();

// * We need to handle executable_name:
// * If it is empty:
// * We are in the top level parser, or
// * We are constructing a subparser: make_unique<parser> -> constructor -> init
// * If it is not empty, we arrived here through a call to add_subcommands, in which case we already
// appended the subcommand to the executable_name.
cmd_arguments.clear();

if (executable_name.empty())
executable_name.emplace_back(original_arguments[0]);

// End: If init() is called multiple times (via add_subcommands).

bool special_format_was_set{false};

// Helper function for going to the next argument. This makes it more obvious that we are
Expand Down

0 comments on commit 37f9117

Please sign in to comment.