Jim Flynn | 3091b06 | 2019-02-15 14:45:04 +0000 | [diff] [blame^] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "CommandLineProcessor.hpp" |
| 7 | |
| 8 | #define BOOST_FILESYSTEM_NO_DEPRECATED |
| 9 | |
| 10 | #include <boost/program_options.hpp> |
| 11 | #include <boost/filesystem/operations.hpp> |
| 12 | #include <boost/filesystem/path.hpp> |
| 13 | |
| 14 | namespace armnnQuantizer |
| 15 | { |
| 16 | |
| 17 | bool ValidateOutputDirectory(std::string& dir) |
| 18 | { |
| 19 | if (dir.empty()) |
| 20 | { |
| 21 | std::cerr << "No output directory specified" << std::endl; |
| 22 | return false; |
| 23 | } |
| 24 | |
| 25 | if (dir[dir.length() - 1] != '/') |
| 26 | { |
| 27 | dir += "/"; |
| 28 | } |
| 29 | |
| 30 | if (!boost::filesystem::exists(dir)) |
| 31 | { |
| 32 | std::cerr << "Output directory [" << dir << "] does not exist" << std::endl; |
| 33 | return false; |
| 34 | } |
| 35 | |
| 36 | if (!boost::filesystem::is_directory(dir)) |
| 37 | { |
| 38 | std::cerr << "Given output directory [" << dir << "] is not a directory" << std::endl; |
| 39 | return false; |
| 40 | } |
| 41 | |
| 42 | return true; |
| 43 | } |
| 44 | |
| 45 | bool ValidateInputFile(const std::string& inputFileName) |
| 46 | { |
| 47 | if (!boost::filesystem::exists(inputFileName)) |
| 48 | { |
| 49 | std::cerr << "Input file [" << inputFileName << "] does not exist" << std::endl; |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | if (boost::filesystem::is_directory(inputFileName)) |
| 54 | { |
| 55 | std::cerr << "Given input file [" << inputFileName << "] is a directory" << std::endl; |
| 56 | return false; |
| 57 | } |
| 58 | |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | bool CommandLineProcessor::ProcessCommandLine(int argc, char* argv[]) |
| 63 | { |
| 64 | namespace po = boost::program_options; |
| 65 | |
| 66 | po::options_description desc("Options"); |
| 67 | try |
| 68 | { |
| 69 | desc.add_options() |
| 70 | ("help,h", "Display help messages") |
| 71 | ("infile,f", po::value<std::string>(&m_InputFileName)->required(), |
| 72 | "Input file containing float 32 ArmNN Input Graph") |
| 73 | ("outdir,d", po::value<std::string>(&m_OutputDirectory)->required(), |
| 74 | "Directory that output file will be written to") |
| 75 | ("outfile,o", po::value<std::string>(&m_OutputFileName)->required(), "Output file name"); |
| 76 | } |
| 77 | catch (const std::exception& e) |
| 78 | { |
| 79 | std::cerr << "Fatal internal error: [" << e.what() << "]" << std::endl; |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | po::variables_map vm; |
| 84 | |
| 85 | try |
| 86 | { |
| 87 | po::store(po::parse_command_line(argc, argv, desc), vm); |
| 88 | |
| 89 | if (vm.count("help")) |
| 90 | { |
| 91 | std::cout << desc << std::endl; |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | po::notify(vm); |
| 96 | } |
| 97 | catch (const po::error& e) |
| 98 | { |
| 99 | std::cerr << e.what() << std::endl << std::endl; |
| 100 | std::cerr << desc << std::endl; |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | if (!armnnQuantizer::ValidateInputFile(m_InputFileName)) |
| 105 | { |
| 106 | return false; |
| 107 | } |
| 108 | |
| 109 | if (!armnnQuantizer::ValidateOutputDirectory(m_OutputDirectory)) |
| 110 | { |
| 111 | return false; |
| 112 | } |
| 113 | |
| 114 | std::string output(m_OutputDirectory); |
| 115 | output.append(m_OutputFileName); |
| 116 | |
| 117 | if (boost::filesystem::exists(output)) |
| 118 | { |
| 119 | std::cerr << "Output file [" << output << "] already exists" << std::endl; |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | return true; |
| 124 | } |
| 125 | |
| 126 | } // namespace armnnQuantizer |