Skip to content

Commit e9b5684

Browse files
committed
alternative approach
1 parent 37f9117 commit e9b5684

File tree

1 file changed

+30
-20
lines changed

1 file changed

+30
-20
lines changed

include/sharg/parser.hpp

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,11 @@ class parser
189189
{
190190
info.app_name = app_name;
191191

192-
add_subcommands(std::move(subcommands));
192+
for (auto const & subcommand : subcommands)
193+
std::ignore /* function is nodisard */ = add_subcommand(subcommand);
194+
195+
if (subcommands.empty())
196+
init();
193197
}
194198

195199
//!\overload
@@ -453,12 +457,18 @@ class parser
453457
* \details
454458
* \stableapi{Since version 1.0.}
455459
*/
456-
parser & get_sub_parser()
460+
[[nodiscard]] parser & get_sub_parser()
457461
{
462+
// Technically not, as sub_parser is set via init(), but if if no subcommand is used in the command line,
463+
// we do not process special formats (help/short-help/etc.) for the top-level parser.
464+
if (!parse_was_called)
465+
throw design_error("The function parse() must be called before get_sub_parser!");
466+
467+
if (subcommands.empty())
468+
throw design_error("No subcommand was provided for the argument parser!");
469+
458470
if (sub_parser == nullptr)
459-
{
460-
throw design_error("No subcommand was provided at the construction of the argument parser!");
461-
}
471+
throw design_error("There is no subparser!");
462472

463473
return *sub_parser;
464474
}
@@ -675,31 +685,31 @@ class parser
675685
* \param[in] subcommands A list of subcommands.
676686
* \throws sharg::design_error if the subcommand name contains illegal characters.
677687
*/
678-
void add_subcommands(std::vector<std::string> const & subcommands)
688+
[[nodiscard]] parser * add_subcommand(std::string subcommand)
679689
{
680-
check_parse_not_called("add_subcommands");
681-
for (auto const & sub : subcommands)
690+
check_parse_not_called("add_subcommand");
691+
692+
if (!std::regex_match(subcommand, app_name_regex))
682693
{
683-
if (!std::regex_match(sub, app_name_regex))
684-
{
685-
std::string const error_message =
686-
detail::to_string(std::quoted(info.app_name),
687-
" contains an invalid subcommand name: ",
688-
std::quoted(sub),
689-
". The subcommand name must only contain alpha-numeric characters ",
690-
"or '_' and '-' (regex: \"^[a-zA-Z0-9_-]+$\").");
691-
throw design_error{error_message};
692-
};
693-
}
694+
std::string const error_message =
695+
detail::to_string(std::quoted(info.app_name),
696+
" contains an invalid subcommand name: ",
697+
std::quoted(subcommand),
698+
". The subcommand name must only contain alpha-numeric characters ",
699+
"or '_' and '-' (regex: \"^[a-zA-Z0-9_-]+$\").");
700+
throw design_error{error_message};
701+
};
694702

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

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

702710
init();
711+
712+
return sub_parser.get();
703713
}
704714

705715
private:

0 commit comments

Comments
 (0)