From 620e0732abede92f505f69d7676bfbd9b5d4584f Mon Sep 17 00:00:00 2001 From: Matthew Sloyan Date: Fri, 25 Sep 2020 11:43:32 +0100 Subject: IVGCVSW-5281 Switch tests/ImageCSVFileGenerator over to cxxopts Signed-off-by: Matthew Sloyan Change-Id: I1b2ba4fcaebbafb70693b83b40b79db0071b4522 --- .../ImageCSVFileGenerator.cpp | 128 +++++++++++++-------- 1 file changed, 80 insertions(+), 48 deletions(-) (limited to 'tests') diff --git a/tests/ImageCSVFileGenerator/ImageCSVFileGenerator.cpp b/tests/ImageCSVFileGenerator/ImageCSVFileGenerator.cpp index 0d2252dfc9..5405df9e16 100644 --- a/tests/ImageCSVFileGenerator/ImageCSVFileGenerator.cpp +++ b/tests/ImageCSVFileGenerator/ImageCSVFileGenerator.cpp @@ -4,7 +4,8 @@ // #include -#include +#include +#include #include #include @@ -20,6 +21,50 @@ namespace class CommandLineProcessor { public: + bool ParseOptions(cxxopts::ParseResult& result) + { + // indir is mandatory, dir could possibly be changed. + if (result.count("indir")) + { + std::string dir = result["indir"].as(); + + if (!ValidateDirectory(dir)) + { + return false; + } + + m_InputDirectory = dir; + } + else + { + std::cerr << "-i/--indir parameter is mandatory." << std::endl; + return false; + } + + // outfile is mandatory + if (result.count("outfile")) + { + if (!ValidateOutputFile(result["outfile"].as())) + { + return false; + } + } + else + { + std::cerr << "-o/--outfile parameter is mandatory." << std::endl; + return false; + } + + if (result.count("layer-binding-id")) + { + if(!ValidateBindingId(result["layer-binding-id"].as())) + { + return false; + } + } + return true; + } + bool ValidateDirectory(std::string& dir) { if (dir.empty()) @@ -48,7 +93,7 @@ public: return true; } - bool ValidateOutputFile(std::string& outputFileName) + bool ValidateOutputFile(const std::string& outputFileName) { if (outputFileName.empty()) { @@ -80,70 +125,57 @@ public: bool ValidateBindingId(const std::string& id) { - if (!std::all_of(id.begin(), id.end(), ::isdigit)) - { - std::cerr << "Invalid input binding Id" << std::endl; - return false; - } + if (!std::all_of(id.begin(), id.end(), ::isdigit)) + { + std::cerr << "Invalid input binding Id" << std::endl; + return false; + } return true; } bool ProcessCommandLine(int argc, char* argv[]) { - namespace po = boost::program_options; - - po::options_description desc("Options"); - try - { - desc.add_options() - ("help,h", "Display help messages") - ("indir,i", po::value(&m_InputDirectory)->required(), - "Directory that .raw files are stored in") - ("outfile,o", po::value(&m_OutputFileName)->required(), - "Output CSV file path") - ("layer-binding-id,l", po::value(&m_InputBindingId)->default_value("0"), - "Input layer binding Id, Defaults to 0"); - } - catch (const std::exception& e) - { - std::cerr << "Fatal internal error: [" << e.what() << "]" << std::endl; - return false; - } - - po::variables_map vm; - try { - po::store(po::parse_command_line(argc, argv, desc), vm); - - if (vm.count("help")) + cxxopts::Options options("ImageCSVFileGenerator", + "Program for creating a CSV file that " + "contains a list of .raw tensor files. " + "These .raw tensor files can be generated using the ImageTensorGenerator"); + + options.add_options() + ("h,help", "Display help messages") + ("i,indir", + "Directory that .raw files are stored in", + cxxopts::value(m_InputDirectory)) + ("o,outfile", + "Output CSV file path", + cxxopts::value(m_OutputFileName)) + ("l, layer-binding-id", + "Input layer binding Id, Defaults to 0", + cxxopts::value(m_InputBindingId)->default_value("0")); + + auto result = options.parse(argc, argv); + + if (result.count("help")) { - std::cout << desc << std::endl; + std::cout << options.help() << std::endl; return false; } - po::notify(vm); + // Check for mandatory parameters and validate inputs + if(!ParseOptions(result)){ + return false; + } } - catch (const po::error& e) + catch (const cxxopts::OptionException& e) { std::cerr << e.what() << std::endl << std::endl; - std::cerr << desc << std::endl; - return false; - } - - if (!ValidateDirectory(m_InputDirectory)) - { return false; } - - if (!ValidateOutputFile(m_OutputFileName)) - { - return false; - } - - if(!ValidateBindingId(m_InputBindingId)) + catch (const std::exception& e) { + std::cerr << "Fatal internal error: [" << e.what() << "]" << std::endl; return false; } -- cgit v1.2.1