Skip to content

Commit

Permalink
alternative approach
Browse files Browse the repository at this point in the history
  • Loading branch information
eseiler committed Feb 9, 2024
1 parent 37f9117 commit e9b5684
Showing 1 changed file with 30 additions and 20 deletions.
50 changes: 30 additions & 20 deletions include/sharg/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,11 @@ class parser
{
info.app_name = app_name;

add_subcommands(std::move(subcommands));
for (auto const & subcommand : subcommands)
std::ignore /* function is nodisard */ = add_subcommand(subcommand);

if (subcommands.empty())
init();
}

//!\overload
Expand Down Expand Up @@ -453,12 +457,18 @@ class parser
* \details
* \stableapi{Since version 1.0.}
*/
parser & get_sub_parser()
[[nodiscard]] parser & get_sub_parser()
{
// Technically not, as sub_parser is set via init(), but if if no subcommand is used in the command line,
// we do not process special formats (help/short-help/etc.) for the top-level parser.
if (!parse_was_called)
throw design_error("The function parse() must be called before get_sub_parser!");

if (subcommands.empty())
throw design_error("No subcommand was provided for the argument parser!");

if (sub_parser == nullptr)
{
throw design_error("No subcommand was provided at the construction of the argument parser!");
}
throw design_error("There is no subparser!");

return *sub_parser;
}
Expand Down Expand Up @@ -675,31 +685,31 @@ class parser
* \param[in] subcommands A list of subcommands.
* \throws sharg::design_error if the subcommand name contains illegal characters.
*/
void add_subcommands(std::vector<std::string> const & subcommands)
[[nodiscard]] parser * add_subcommand(std::string subcommand)
{
check_parse_not_called("add_subcommands");
for (auto const & sub : subcommands)
check_parse_not_called("add_subcommand");

if (!std::regex_match(subcommand, app_name_regex))
{
if (!std::regex_match(sub, app_name_regex))
{
std::string const error_message =
detail::to_string(std::quoted(info.app_name),
" contains an invalid subcommand name: ",
std::quoted(sub),
". The subcommand name must only contain alpha-numeric characters ",
"or '_' and '-' (regex: \"^[a-zA-Z0-9_-]+$\").");
throw design_error{error_message};
};
}
std::string const error_message =
detail::to_string(std::quoted(info.app_name),
" contains an invalid subcommand name: ",
std::quoted(subcommand),
". The subcommand name must only contain alpha-numeric characters ",
"or '_' and '-' (regex: \"^[a-zA-Z0-9_-]+$\").");
throw design_error{error_message};
};

auto & parser_subcommands = this->subcommands;
parser_subcommands.insert(parser_subcommands.end(), subcommands.cbegin(), subcommands.cend());
parser_subcommands.emplace_back(std::move(subcommand));

std::ranges::sort(parser_subcommands);
auto const [first, last] = std::ranges::unique(parser_subcommands);
parser_subcommands.erase(first, last);

init();

return sub_parser.get();
}

private:
Expand Down

0 comments on commit e9b5684

Please sign in to comment.