/** * @file parse_arguments.cpp * @brief Parsing of command line arguments. * @kaspersky_support D. Postnikov * @date 03.02.2026 * @license Apache 2.0 * @copyright © 2026 AO Kaspersky Lab * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "parse_arguments.h" #include #include #include #include namespace po = boost::program_options; std::optional parse_arguments(int argc, char** argv) { po::options_description desc("Usage"); desc.add_options()("help,h", "print available options")( "model,m", po::value()->default_value("blifat"), "model type: blifat or altai")( "train_iters,t", po::value()->default_value(60000), "number of images for training")( "inference_iters,i", po::value()->default_value(10000), "number of images for inference")( "images", po::value()->default_value("MNIST.bin"), "path to raw images file")( "labels", po::value()->default_value("MNIST.target"), "path to images labels file")( "log_path", po::value()->default_value(""), "path for saving logs (if empty, no logs are saved)")( "inference_only", "skip training and use `model_path` to load a pre-trained model")( "training_backend", po::value()->default_value("knp-cpu-single-threaded-backend"), "path to backend used for training")( "inference_backend", po::value(), "path to backend for inference (if empty, `training_backend` is used)")( "extensive_logs_path", po::value()->default_value(""), "path for storing extensive logs (if empty, no extensive logs are produced)")( "model_path", po::value()->default_value(""), "path for saving trained model (if empty, model is not saved)")( "logging_level,l", po::value()->default_value("info"), "logging level: trace, debug, info, warn, error, critical, or none"); po::variables_map vm; po::store(po::parse_command_line(argc, argv, desc), vm); po::notify(vm); if (vm.count("help")) { std::cout << desc << std::endl; return std::nullopt; } ModelDescription model_desc; auto common_path = std::filesystem::weakly_canonical(std::filesystem::path(argv[0]).parent_path()); if (vm.count("model")) { std::string model_type = vm["model"].as(); if (model_type == "blifat") { model_desc.type_ = SupportedModelType::BLIFAT; } else if (model_type == "altai") { model_desc.type_ = SupportedModelType::AltAI; } else { std::cout << "Not supported model type." << std::endl; std::cout << desc << std::endl; return std::nullopt; } } else { std::cout << "Model type not specified." << std::endl; std::cout << desc << std::endl; return std::nullopt; } if (vm.count("train_iters")) { model_desc.train_images_amount_ = vm["train_iters"].as(); } else { std::cout << "Train iterations not specified." << std::endl; std::cout << desc << std::endl; return std::nullopt; } if (vm.count("inference_iters")) { model_desc.inference_images_amount_ = vm["inference_iters"].as(); } else { std::cout << "Inference iterations not specified." << std::endl; std::cout << desc << std::endl; return std::nullopt; } if (vm.count("images")) { model_desc.images_file_path_ = vm["images"].as(); } else { std::cout << "Images path not specified." << std::endl; std::cout << desc << std::endl; return std::nullopt; } if (vm.count("labels")) { model_desc.labels_file_path_ = vm["labels"].as(); } else { std::cout << "Labels path not specified." << std::endl; std::cout << desc << std::endl; return std::nullopt; } if (vm.count("training_backend")) { model_desc.training_backend_path_ = common_path / vm["training_backend"].as(); } else { std::cout << "Training backend path not specified." << std::endl; std::cout << desc << std::endl; return std::nullopt; } if (!vm.count("inference_backend") || vm["inference_backend"].as().empty()) { model_desc.inference_backend_path_ = model_desc.training_backend_path_; } else { model_desc.inference_backend_path_ = common_path / vm["inference_backend"].as(); } if (vm.count("extensive_logs_path")) { model_desc.log_path_ = vm["extensive_logs_path"].as(); } else { model_desc.log_path_ = ""; } if (vm.count("model_path")) { model_desc.model_saving_path_ = vm["model_path"].as(); } else { model_desc.model_saving_path_ = ""; } model_desc.inference_only_ = vm.count("inference_only") > 0; if (vm.count("logging_level")) { knp::framework::logging::Level logging_level = knp::framework::logging::str_to_level(vm["logging_level"].as()); knp::framework::logging::set_level(logging_level); std::cout << "Set logging level to \"" << knp::framework::logging::level_to_str(logging_level) << "\"" << std::endl; } else { std::cout << "Logging level not specified." << std::endl; std::cout << desc << std::endl; return std::nullopt; } return model_desc; }