@@ -189,7 +189,11 @@ class parser
189
189
{
190
190
info.app_name = app_name;
191
191
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 ();
193
197
}
194
198
195
199
// !\overload
@@ -453,12 +457,18 @@ class parser
453
457
* \details
454
458
* \stableapi{Since version 1.0.}
455
459
*/
456
- parser & get_sub_parser ()
460
+ [[nodiscard]] parser & get_sub_parser ()
457
461
{
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
+
458
470
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!" );
462
472
463
473
return *sub_parser;
464
474
}
@@ -675,31 +685,31 @@ class parser
675
685
* \param[in] subcommands A list of subcommands.
676
686
* \throws sharg::design_error if the subcommand name contains illegal characters.
677
687
*/
678
- void add_subcommands (std::vector<std:: string> const & subcommands )
688
+ [[nodiscard]] parser * add_subcommand (std::string subcommand )
679
689
{
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))
682
693
{
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
+ };
694
702
695
703
auto & parser_subcommands = this ->subcommands ;
696
- parser_subcommands.insert (parser_subcommands. end (), subcommands. cbegin (), subcommands. cend ( ));
704
+ parser_subcommands.emplace_back ( std::move (subcommand ));
697
705
698
706
std::ranges::sort (parser_subcommands);
699
707
auto const [first, last] = std::ranges::unique (parser_subcommands);
700
708
parser_subcommands.erase (first, last);
701
709
702
710
init ();
711
+
712
+ return sub_parser.get ();
703
713
}
704
714
705
715
private:
0 commit comments