@@ -169,8 +169,6 @@ class parser
169169 * \param[in] version_updates Notify users about version updates (default sharg::update_notifications::on).
170170 * \param[in] subcommands A list of subcommands (see \link subcommand_parse subcommand parsing \endlink).
171171 *
172- * \throws sharg::design_error if the application name contains illegal characters.
173- *
174172 * The application name must only contain alpha-numeric characters, `_` or `-` ,
175173 * i.e. the following regex must evaluate to true: `"^[a-zA-Z0-9_-]+$"` .
176174 *
@@ -185,9 +183,9 @@ class parser
185183 update_notifications version_updates = update_notifications::on,
186184 std::vector<std::string> subcommands = {}) :
187185 version_check_dev_decision{version_updates},
188- subcommands{std::move (subcommands)},
189186 arguments{arguments}
190187 {
188+ add_subcommands (subcommands);
191189 info.app_name = app_name;
192190 }
193191
@@ -352,7 +350,7 @@ class parser
352350 * related code and should be enclosed in a try catch block as the parser may throw.
353351 *
354352 * \throws sharg::design_error if this function was already called before.
355- *
353+ * \throws sharg::design_error if the application name or subcommands contain illegal characters.
356354 * \throws sharg::option_declared_multiple_times if an option that is not a list was declared multiple times.
357355 * \throws sharg::user_input_error if an incorrect argument is given as (positional) option value.
358356 * \throws sharg::required_option_missing if the user did not provide a required option.
@@ -639,6 +637,27 @@ class parser
639637
640638 operations.push_back (std::move (operation));
641639 }
640+
641+ /* !\brief Adds subcommands to the parser.
642+ * \param[in] subcommands A list of subcommands.
643+ * \details
644+ * Adds subcommands to the current parser. The list of subcommands is sorted and duplicates are removed.
645+ *
646+ * ### Example
647+ *
648+ * \include test/snippet/add_subcommands.cpp
649+ *
650+ * \experimentalapi{Experimental since version 1.1.2}
651+ */
652+ void add_subcommands (std::vector<std::string> const & subcommands)
653+ {
654+ auto & parser_subcommands = this ->subcommands ;
655+ parser_subcommands.insert (parser_subcommands.end (), subcommands.cbegin (), subcommands.cend ());
656+
657+ std::ranges::sort (parser_subcommands);
658+ auto const [first, last] = std::ranges::unique (parser_subcommands);
659+ parser_subcommands.erase (first, last);
660+ }
642661 // !\}
643662
644663 /* !\brief Aggregates all parser related meta data (see sharg::parser_meta_data struct).
0 commit comments