7 #include <cxxopts/cxxopts.hpp> 20 class CommandLineProcessor
26 if (result.count(
"indir"))
28 std::string dir = result[
"indir"].as<std::string>();
35 m_InputDirectory = dir;
39 std::cerr <<
"-i/--indir parameter is mandatory." << std::endl;
44 if (result.count(
"outfile"))
46 if (!ValidateOutputFile(result[
"outfile"].as<std::string>()))
53 std::cerr <<
"-o/--outfile parameter is mandatory." << std::endl;
57 if (result.count(
"layer-binding-id"))
59 if(!ValidateBindingId(result[
"layer-binding-id"].as<std::string>()))
71 std::cerr <<
"No directory specified" << std::endl;
75 if (dir[dir.length() - 1] !=
'/')
82 std::cerr <<
"Directory [" << dir <<
"] does not exist" << std::endl;
86 if (!fs::is_directory(dir))
88 std::cerr <<
"Given directory [" << dir <<
"] is not a directory" << std::endl;
95 bool ValidateOutputFile(
const std::string& outputFileName)
97 if (outputFileName.empty())
99 std::cerr <<
"No output file name specified" << std::endl;
103 if (fs::exists(outputFileName))
105 std::cerr <<
"Output file [" << outputFileName <<
"] already exists" << std::endl;
109 if (fs::is_directory(outputFileName))
111 std::cerr <<
"Output file [" << outputFileName <<
"] is a directory" << std::endl;
115 fs::path outputPath(outputFileName);
116 if (!fs::exists(outputPath.parent_path()))
118 std::cerr <<
"Directory [" << outputPath.parent_path().c_str() <<
"] does not exist" << std::endl;
125 bool ValidateBindingId(
const std::string&
id)
127 if (!std::all_of(
id.begin(),
id.end(), ::isdigit))
129 std::cerr <<
"Invalid input binding Id" << std::endl;
136 bool ProcessCommandLine(
int argc,
char* argv[])
140 cxxopts::Options options(
"ImageCSVFileGenerator",
141 "Program for creating a CSV file that " 142 "contains a list of .raw tensor files. " 143 "These .raw tensor files can be generated using the ImageTensorGenerator");
145 options.add_options()
146 (
"h,help",
"Display help messages")
148 "Directory that .raw files are stored in",
149 cxxopts::value<std::string>(m_InputDirectory))
151 "Output CSV file path",
152 cxxopts::value<std::string>(m_OutputFileName))
153 (
"l, layer-binding-id",
154 "Input layer binding Id, Defaults to 0",
155 cxxopts::value<std::string>(m_InputBindingId)->default_value(
"0"));
157 auto result = options.parse(argc, argv);
159 if (result.count(
"help"))
161 std::cout << options.help() << std::endl;
170 catch (
const cxxopts::OptionException& e)
172 std::cerr << e.what() << std::endl << std::endl;
175 catch (
const std::exception& e)
177 std::cerr <<
"Fatal internal error: [" << e.what() <<
"]" << std::endl;
184 std::string GetInputDirectory() {
return m_InputDirectory;}
185 std::string GetOutputFileName() {
return m_OutputFileName;}
186 std::string GetInputBindingId() {
return m_InputBindingId;}
189 std::string m_InputDirectory;
190 std::string m_OutputFileName;
191 std::string m_InputBindingId;
196 int main(
int argc,
char* argv[])
198 CommandLineProcessor cmdline;
199 if (!cmdline.ProcessCommandLine(argc, argv))
204 const std::string fileFormat(
".raw");
206 const std::string rawDirectory(cmdline.GetInputDirectory());
207 const std::string outputPath(cmdline.GetOutputFileName());
208 const std::string bindingId(cmdline.GetInputBindingId());
210 std::vector<fs::path> rawFiles;
211 for (
auto& entry : fs::directory_iterator(rawDirectory))
213 if (entry.path().extension().c_str() == fileFormat)
215 rawFiles.push_back(entry.path());
219 if (!rawFiles.empty())
221 unsigned int pass = 0;
222 std::ofstream refinementData;
223 refinementData.open(outputPath, std::ofstream::out);
224 if (refinementData.is_open())
226 for (
auto const& raw : rawFiles)
228 refinementData << pass <<
", " << bindingId <<
", " << raw.c_str() <<
"\n";
231 std::cerr <<
"Failed to write to output file: " << outputPath << std::endl;
236 refinementData.close();
240 std::cerr <<
"Failed to open output file: " << outputPath << std::endl;
246 std::cerr <<
"No matching files with the \".raw\" extension found in the directory: " 247 << rawDirectory << std::endl;
void ParseOptions(const std::vector< BackendOptions > &options, BackendId backend, F f)
int main(int argc, char *argv[])
bool ValidateDirectory(std::string &dir)