Files
salmanoff/hcore/opts.cpp
T
hayodea 3b6ca14275 CmdOpts: merge -[i|a|e] into one -s option
We augmented the grammar to specify the type of device (intero,
extro or actuator) so we can now collapse the 3 dev spec cmd
line opts into 1 opt.
2025-01-07 14:05:23 -04:00

72 lines
1.8 KiB
C++

#include <opts.h>
#include <iostream>
#include <stdexcept>
#include <getopt.h>
#include <string>
#include <vector>
struct option OptionParser::longOptions[] = {
{"devicespec", required_argument, 0, 's'},
{"spec", required_argument, 0, 's'},
{"devspec", required_argument, 0, 's'},
{"devfile", required_argument, 0, 'd'},
{"devicefile", required_argument, 0, 'd'},
{"verbose", no_argument, 0, 'v'},
{"help", no_argument, 0, '?'},
{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(
argc, argv, "s:d:v?", longOptions, &optionIndex)) != -1)
{
switch (opt)
{
case 's':
if (!deviceSpecs.empty()) {
deviceSpecs += "||";
}
deviceSpecs += std::string(optarg);
break;
case 'd':
deviceSpecFiles.push_back(optarg);
break;
case 'v':
verbose = true;
break;
case '?':
printUsage = true;
return;
default:
throw std::invalid_argument("Invalid argument encountered: " + std::string(argv[optind - 1]));
}
}
}
std::string OptionParser::getUsage() const
{
return "Usage: program [-s|--devicespec|--spec|--devspec <device_spec>] "
"[-d|--devfile|--devicefile <filename>] "
"[-v|--verbose] "
"[-?|--help]";
}
void OptionParser::dumpOptions() const
{
if (verbose) {
std::cout << "Verbose mode is on" << std::endl;
}
std::cout << "Device Specs: " << deviceSpecs << std::endl;
std::cout << "Device Spec Files: ";
for (const auto& file : deviceSpecFiles) {
std::cout << file << " ";
}
std::cout << std::endl;
}