@@ -52,7 +52,8 @@ std::string getConfigPath(const std::string& configPath) {
5252 return configPath;
5353}
5454
55- void CLIParser::parse (int argc, char ** argv) {
55+ std::variant<bool , std::pair<int , std::string>> CLIParser::parse (int argc, char ** argv) {
56+ std::stringstream ss;
5657 try {
5758 options = std::make_unique<cxxopts::Options>(argv[0 ], " OpenVINO Model Server" );
5859 auto configOptions = std::make_unique<cxxopts::Options>(" ovms --model_name <MODEL_NAME> --add_to_config <CONFIG_PATH> --model_repository_path <MODEL_REPO_PATH> \n ovms --model_path <MODEL_PATH> --model_name <MODEL_NAME> --add_to_config <CONFIG_PATH> \n ovms --remove_from_config <CONFIG_PATH> --model_name <MODEL_NAME>" , " config management commands:" );
@@ -353,78 +354,78 @@ void CLIParser::parse(int argc, char** argv) {
353354 break ;
354355 }
355356 case UNKNOWN_GRAPH: {
356- std::cerr << " error parsing options - --task parameter unsupported value: " + result->operator [](" task" ).as <std::string>();
357- exit (OVMS_EX_USAGE);
357+ ss << " error parsing options - --task parameter unsupported value: " + result->operator [](" task" ).as <std::string>();
358+ return std::make_pair (OVMS_EX_USAGE, ss. str () );
358359 }
359360 }
360361 } else {
361- std::cerr << " error parsing options - --task parameter wasn't passed" ;
362- exit (OVMS_EX_USAGE);
362+ ss << " error parsing options - --task parameter wasn't passed" ;
363+ return std::make_pair (OVMS_EX_USAGE, ss. str () );
363364 }
364365
365366 if (unmatchedOptions.size ()) {
366- std::cerr << " task: " << enumToString (task) << " - error parsing options - unmatched arguments : " ;
367+ ss << " task: " << enumToString (task) << " - error parsing options - unmatched arguments : " ;
367368 for (auto & argument : unmatchedOptions) {
368- std::cerr << argument << " , " ;
369+ ss << argument << " , " ;
369370 }
370- std::cerr << std::endl;
371- exit (OVMS_EX_USAGE);
371+ ss << std::endl;
372+ return std::make_pair (OVMS_EX_USAGE, ss. str () );
372373 }
373374 } else if (result->unmatched ().size ()){
374- std::cerr << " error parsing options - unmatched arguments: " ;
375+ ss << " error parsing options - unmatched arguments: " ;
375376 for (auto & argument : result->unmatched ()) {
376- std::cerr << argument << " , " ;
377+ ss << argument << " , " ;
377378 }
378- std::cerr << std::endl;
379- exit (OVMS_EX_USAGE);
379+ ss << std::endl;
380+ return std::make_pair (OVMS_EX_USAGE, ss. str () );
380381 }
381382 if (isHFPullOrPullAndStart (this ->result ) && result->count (" list_models" )) {
382- std::cerr << " error parsing options - --list_models cannot be used with --pull or --task" << std::endl;
383- exit (OVMS_EX_USAGE);
383+ ss << " error parsing options - --list_models cannot be used with --pull or --task" << std::endl;
384+ return std::make_pair (OVMS_EX_USAGE, ss. str () );
384385 }
385386 if (isHFPullOrPullAndStart (this ->result ) && result->count (" remove_from_config" )) {
386- std::cerr << " error parsing options - --remove_from_config cannot be used with --pull or --task" << std::endl;
387- exit (OVMS_EX_USAGE);
387+ ss << " error parsing options - --remove_from_config cannot be used with --pull or --task" << std::endl;
388+ return std::make_pair (OVMS_EX_USAGE, ss. str () );
388389 }
389390 if (isHFPullOrPullAndStart (this ->result ) && result->count (" add_to_config" )) {
390- std::cerr << " error parsing options - --add_to_config cannot be used with --pull or --task" << std::endl;
391- exit (OVMS_EX_USAGE);
391+ ss << " error parsing options - --add_to_config cannot be used with --pull or --task" << std::endl;
392+ return std::make_pair (OVMS_EX_USAGE, ss. str () );
392393 }
393394 if (result->count (" add_to_config" ) && result->count (" list_models" )) {
394- std::cerr << " error parsing options - --list_models cannot be used with --add_to_config" << std::endl;
395- exit (OVMS_EX_USAGE);
395+ ss << " error parsing options - --list_models cannot be used with --add_to_config" << std::endl;
396+ return std::make_pair (OVMS_EX_USAGE, ss. str () );
396397 }
397398 if (result->count (" remove_from_config" ) && result->count (" list_models" )) {
398- std::cerr << " error parsing options - --list_models cannot be used with --remove_from_config" << std::endl;
399- exit (OVMS_EX_USAGE);
399+ ss << " error parsing options - --list_models cannot be used with --remove_from_config" << std::endl;
400+ return std::make_pair (OVMS_EX_USAGE, ss. str () );
400401 }
401402 if (result->count (" add_to_config" ) && result->count (" model_repository_path" ) && result->count (" model_path" )) {
402- std::cerr << " error parsing options - --model_repository_path cannot be used with --model_path" << std::endl;
403- exit (OVMS_EX_USAGE);
403+ ss << " error parsing options - --model_repository_path cannot be used with --model_path" << std::endl;
404+ return std::make_pair (OVMS_EX_USAGE, ss. str () );
404405 }
405406 if (result->count (" remove_from_config" ) && result->count (" model_repository_path" )) {
406- std::cerr << " error parsing options - --model_repository_path cannot be used with --remove_from_config" << std::endl;
407- exit (OVMS_EX_USAGE);
407+ ss << " error parsing options - --model_repository_path cannot be used with --remove_from_config" << std::endl;
408+ return std::make_pair (OVMS_EX_USAGE, ss. str () );
408409 }
409410 if (result->count (" remove_from_config" ) && result->count (" model_path" )) {
410- std::cerr << " error parsing options - --model_path cannot be used with --remove_from_config" << std::endl;
411- exit (OVMS_EX_USAGE);
411+ ss << " error parsing options - --model_path cannot be used with --remove_from_config" << std::endl;
412+ return std::make_pair (OVMS_EX_USAGE, ss. str () );
412413 }
413414#pragma warning(push)
414415#pragma warning(disable : 4129)
415416 if (result->count (" version" )) {
416417 std::string project_name (PROJECT_NAME);
417418 std::string project_version (PROJECT_VERSION);
418- std::cout << project_name + " " + project_version << std::endl;
419- std::cout << " OpenVINO backend " << OPENVINO_NAME << std::endl;
420- std::cout << " Bazel build flags: " << BAZEL_BUILD_FLAGS << std::endl;
419+ ss << project_name + " " + project_version << std::endl;
420+ ss << " OpenVINO backend " << OPENVINO_NAME << std::endl;
421+ ss << " Bazel build flags: " << BAZEL_BUILD_FLAGS << std::endl;
421422#pragma warning(pop)
422- exit (OVMS_EX_OK);
423+ return std::make_pair (OVMS_EX_OK, ss. str () );
423424 }
424425
425426 if (result->count (" help" ) || result->arguments ().size () == 0 ) {
426- std::cout << options->help ({" " , " multi model" , " single model" , " pull hf model" }) << std::endl;
427- std::cout << configOptions->help ({CONFIG_MANAGEMENT_HELP_GROUP}) << std::endl;
427+ ss << options->help ({" " , " multi model" , " single model" , " pull hf model" }) << std::endl;
428+ ss << configOptions->help ({CONFIG_MANAGEMENT_HELP_GROUP}) << std::endl;
428429 GraphCLIParser parser1;
429430 RerankGraphCLIParser parser2;
430431 EmbeddingsGraphCLIParser parser3;
@@ -435,11 +436,13 @@ void CLIParser::parse(int argc, char** argv) {
435436 parser2.printHelp ();
436437 parser3.printHelp ();
437438 imageGenParser.printHelp ();
438- exit (OVMS_EX_OK);
439+ return std::make_pair (OVMS_EX_OK, ss. str () );
439440 }
441+
442+ return true ;
440443 } catch (const std::exception& e) {
441- std::cerr << " error parsing options: " << e.what () << std::endl;
442- exit (OVMS_EX_USAGE);
444+ ss << " error parsing options: " << e.what () << std::endl;
445+ return std::make_pair (OVMS_EX_USAGE, ss. str () );
443446 }
444447}
445448
@@ -532,8 +535,7 @@ void CLIParser::prepareServer(ServerSettingsImpl& serverSettings) {
532535 }
533536 file.close ();
534537 } else {
535- std::cerr << " Error reading API key file: Unable to open file " << apiKeyFile << std::endl;
536- exit (OVMS_EX_USAGE);
538+ throw std::filesystem::filesystem_error (" Error reading API key file: Unable to open file " , apiKeyFile, std::error_code (2 , std::generic_category ()));
537539 }
538540 } else {
539541 const char * envApiKey = std::getenv (API_KEY_ENV_VAR);
0 commit comments