2025-01-05 13:25:40 -04:00
|
|
|
#include <opts.h>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <stdexcept>
|
|
|
|
|
#include <getopt.h>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
struct option OptionParser::longOptions[] = {
|
2025-01-07 14:05:23 -04:00
|
|
|
{"devicespec", required_argument, 0, 's'},
|
|
|
|
|
{"spec", required_argument, 0, 's'},
|
|
|
|
|
{"devspec", required_argument, 0, 's'},
|
2025-01-05 13:25:40 -04:00
|
|
|
{"devfile", required_argument, 0, 'd'},
|
|
|
|
|
{"devicefile", required_argument, 0, 'd'},
|
|
|
|
|
{"verbose", no_argument, 0, 'v'},
|
2025-01-05 13:35:14 -04:00
|
|
|
{"help", no_argument, 0, '?'},
|
2025-01-05 13:25:40 -04:00
|
|
|
{0, 0, 0, 0}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void OptionParser::parseArguments(int argc, char *argv[])
|
|
|
|
|
{
|
|
|
|
|
int opt;
|
|
|
|
|
int optionIndex = 0;
|
|
|
|
|
optind = 1; // Reset optind to 1 before parsing
|
|
|
|
|
while ((opt = getopt_long(
|
2025-01-07 14:05:23 -04:00
|
|
|
argc, argv, "s:d:v?", longOptions, &optionIndex)) != -1)
|
2025-01-05 13:25:40 -04:00
|
|
|
{
|
|
|
|
|
switch (opt)
|
|
|
|
|
{
|
2025-01-07 14:05:23 -04:00
|
|
|
case 's':
|
|
|
|
|
if (!deviceSpecs.empty()) {
|
|
|
|
|
deviceSpecs += "||";
|
|
|
|
|
}
|
|
|
|
|
deviceSpecs += std::string(optarg);
|
2025-01-05 13:25:40 -04:00
|
|
|
break;
|
|
|
|
|
case 'd':
|
2025-01-05 14:04:31 -04:00
|
|
|
deviceSpecFiles.push_back(optarg);
|
2025-01-05 13:25:40 -04:00
|
|
|
break;
|
|
|
|
|
case 'v':
|
|
|
|
|
verbose = true;
|
|
|
|
|
break;
|
2025-01-05 13:35:14 -04:00
|
|
|
case '?':
|
|
|
|
|
printUsage = true;
|
|
|
|
|
return;
|
2025-01-05 13:25:40 -04:00
|
|
|
default:
|
|
|
|
|
throw std::invalid_argument("Invalid argument encountered: " + std::string(argv[optind - 1]));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string OptionParser::getUsage() const
|
|
|
|
|
{
|
2025-01-07 14:05:23 -04:00
|
|
|
return "Usage: program [-s|--devicespec|--spec|--devspec <device_spec>] "
|
2025-01-05 13:25:40 -04:00
|
|
|
"[-d|--devfile|--devicefile <filename>] "
|
2025-01-05 13:35:14 -04:00
|
|
|
"[-v|--verbose] "
|
|
|
|
|
"[-?|--help]";
|
2025-01-05 13:25:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OptionParser::dumpOptions() const
|
|
|
|
|
{
|
|
|
|
|
if (verbose) {
|
|
|
|
|
std::cout << "Verbose mode is on" << std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-07 14:05:23 -04:00
|
|
|
std::cout << "Device Specs: " << deviceSpecs << std::endl;
|
2025-01-05 13:25:40 -04:00
|
|
|
|
2025-01-05 14:04:31 -04:00
|
|
|
std::cout << "Device Spec Files: ";
|
|
|
|
|
for (const auto& file : deviceSpecFiles) {
|
|
|
|
|
std::cout << file << " ";
|
|
|
|
|
}
|
|
|
|
|
std::cout << std::endl;
|
2025-01-05 13:25:40 -04:00
|
|
|
}
|