blob: a7baa5cac45c1c423dc6dedc0a387d82006f66dc [file] [log] [blame]
Jim Flynn3091b062019-02-15 14:45:04 +00001//
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
14namespace armnnQuantizer
15{
16
17bool 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
Sadik Armagan2b03d642019-04-12 15:17:02 +010045bool ValidateProvidedFile(const std::string& inputFileName)
Jim Flynn3091b062019-02-15 14:45:04 +000046{
47 if (!boost::filesystem::exists(inputFileName))
48 {
Sadik Armagan2b03d642019-04-12 15:17:02 +010049 std::cerr << "Provided file [" << inputFileName << "] does not exist" << std::endl;
Jim Flynn3091b062019-02-15 14:45:04 +000050 return false;
51 }
52
53 if (boost::filesystem::is_directory(inputFileName))
54 {
Sadik Armagan2b03d642019-04-12 15:17:02 +010055 std::cerr << "Given file [" << inputFileName << "] is a directory" << std::endl;
Jim Flynn3091b062019-02-15 14:45:04 +000056 return false;
57 }
58
59 return true;
60}
61
62bool 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")
Sadik Armagan2b03d642019-04-12 15:17:02 +010073 ("csvfile,c", po::value<std::string>(&m_CsvFileName)->default_value(""),
74 "CSV file containing paths for RAW input tensors")
Jim Flynn3091b062019-02-15 14:45:04 +000075 ("outdir,d", po::value<std::string>(&m_OutputDirectory)->required(),
76 "Directory that output file will be written to")
77 ("outfile,o", po::value<std::string>(&m_OutputFileName)->required(), "Output file name");
78 }
79 catch (const std::exception& e)
80 {
81 std::cerr << "Fatal internal error: [" << e.what() << "]" << std::endl;
82 return false;
83 }
84
85 po::variables_map vm;
86
87 try
88 {
89 po::store(po::parse_command_line(argc, argv, desc), vm);
90
91 if (vm.count("help"))
92 {
93 std::cout << desc << std::endl;
94 return false;
95 }
96
97 po::notify(vm);
98 }
99 catch (const po::error& e)
100 {
101 std::cerr << e.what() << std::endl << std::endl;
102 std::cerr << desc << std::endl;
103 return false;
104 }
105
Sadik Armagan2b03d642019-04-12 15:17:02 +0100106 if (!armnnQuantizer::ValidateProvidedFile(m_InputFileName))
Jim Flynn3091b062019-02-15 14:45:04 +0000107 {
108 return false;
109 }
110
Sadik Armagan2b03d642019-04-12 15:17:02 +0100111 if (m_CsvFileName != "")
112 {
113 if (!armnnQuantizer::ValidateProvidedFile(m_CsvFileName))
114 {
115 return false;
116 }
117 else
118 {
119 boost::filesystem::path csvFilePath(m_CsvFileName);
120 m_CsvFileDirectory = csvFilePath.parent_path().c_str();
121 }
122 }
123
Jim Flynn3091b062019-02-15 14:45:04 +0000124 if (!armnnQuantizer::ValidateOutputDirectory(m_OutputDirectory))
125 {
126 return false;
127 }
128
129 std::string output(m_OutputDirectory);
130 output.append(m_OutputFileName);
131
132 if (boost::filesystem::exists(output))
133 {
134 std::cerr << "Output file [" << output << "] already exists" << std::endl;
135 return false;
136 }
137
138 return true;
139}
140
141} // namespace armnnQuantizer