@@ -640,6 +640,51 @@ BOOST_AUTO_TEST_CASE(util_GetArg)
640640 BOOST_CHECK_EQUAL (testArgs.GetArg (" pritest4" , " default" ), " b" );
641641}
642642
643+ BOOST_AUTO_TEST_CASE (util_AddCommand)
644+ {
645+ enum TestFail { SUCCESS, PARSE_FAIL, PARSE_ERROR, NO_COMMAND, COMMAND_OPTS };
646+
647+ auto testfn = [&](const auto & argv) -> TestFail {
648+ TestArgsManager test_args;
649+ test_args.AddArg (" -opt1=<file name>" , " Opt 1" , ArgsManager::ALLOW_ANY | ArgsManager::DISALLOW_NEGATION, OptionsCategory::COMMAND_OPTIONS);
650+ test_args.AddArg (" -opt2=<file name>" , " Opt 2" , ArgsManager::ALLOW_ANY | ArgsManager::DISALLOW_NEGATION, OptionsCategory::COMMAND_OPTIONS);
651+ test_args.AddArg (" -opt3=<file name>" , " Opt 3" , ArgsManager::ALLOW_ANY | ArgsManager::DISALLOW_NEGATION, OptionsCategory::OPTIONS);
652+
653+ test_args.AddCommand (" cmd1" , " No specific options" );
654+ test_args.AddCommand (" cmd2" , " Opt 1" , {" -opt1" });
655+ test_args.AddCommand (" cmd3" , " Opt 1 or 2" , {" -opt1" , " -opt2" });
656+
657+ std::string error;
658+ if (!test_args.ParseParameters (argv.size (), argv.data (), error)) return PARSE_FAIL;
659+ if (!error.empty ()) return PARSE_ERROR;
660+ const auto command = test_args.GetCommand ();
661+ if (!command) return NO_COMMAND;
662+ std::vector<std::string> details;
663+ if (!test_args.CheckCommandOptions (command->command , &details)) return COMMAND_OPTS;
664+ return SUCCESS;
665+ };
666+
667+ BOOST_CHECK_EQUAL (COMMAND_OPTS, testfn (std::array{" x" , " -opt1=foo" , " cmd1" }));
668+ BOOST_CHECK_EQUAL (SUCCESS, testfn (std::array{" x" , " cmd1" , " -opt1=foo" })); // things after the command are "args" and left unparsed, not options
669+
670+ BOOST_CHECK_EQUAL (SUCCESS, testfn (std::array{" x" , " -opt1=foo" , " cmd2" }));
671+ BOOST_CHECK_EQUAL (SUCCESS, testfn (std::array{" x" , " -opt1=foo" , " cmd3" }));
672+ BOOST_CHECK_EQUAL (COMMAND_OPTS, testfn (std::array{" x" , " -opt2=foo" , " cmd1" }));
673+ BOOST_CHECK_EQUAL (COMMAND_OPTS, testfn (std::array{" x" , " -opt2=foo" , " cmd2" }));
674+ BOOST_CHECK_EQUAL (SUCCESS, testfn (std::array{" x" , " -opt2=foo" , " cmd3" }));
675+ BOOST_CHECK_EQUAL (SUCCESS, testfn (std::array{" x" , " -opt3=foo" , " cmd1" }));
676+ BOOST_CHECK_EQUAL (SUCCESS, testfn (std::array{" x" , " -opt3=foo" , " cmd2" }));
677+ BOOST_CHECK_EQUAL (SUCCESS, testfn (std::array{" x" , " -opt3=foo" , " cmd3" }));
678+
679+ BOOST_CHECK_EQUAL (COMMAND_OPTS, testfn (std::array{" x" , " -opt1=foo" , " -opt3=bar" , " -opt2=baz" , " cmd1" }));
680+ BOOST_CHECK_EQUAL (COMMAND_OPTS, testfn (std::array{" x" , " -opt1=foo" , " -opt3=bar" , " -opt2=baz" , " cmd2" }));
681+ BOOST_CHECK_EQUAL (SUCCESS, testfn (std::array{" x" , " -opt1=foo" , " -opt3=bar" , " -opt2=baz" , " cmd3" }));
682+
683+ BOOST_CHECK_EQUAL (PARSE_FAIL, testfn (std::array{" x" , " cmd4" }));
684+ BOOST_CHECK_EQUAL (NO_COMMAND, testfn (std::array{" x" , " -opt3=foo" }));
685+ BOOST_CHECK_EQUAL (PARSE_FAIL, testfn (std::array{" x" , " -opt4=foo" }));
686+ }
687+
643688BOOST_AUTO_TEST_CASE (util_GetChainTypeString)
644689{
645690 TestArgsManager test_args;
0 commit comments