118 lines
3.0 KiB
C++
118 lines
3.0 KiB
C++
#include <opts.h>
|
|
#include <iostream>
|
|
#include <stdexcept>
|
|
#include <getopt.h>
|
|
#include <string>
|
|
#include <vector>
|
|
#include <sys/stat.h>
|
|
#include <sstream>
|
|
|
|
|
|
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'},
|
|
{"sense-api-lib", required_argument, 0, 'a'},
|
|
{"senseapi", required_argument, 0, 'a'},
|
|
{"sense-api-path", required_argument, 0, 'p'},
|
|
{"verbose", no_argument, 0, 'v'},
|
|
{"help", no_argument, 0, '?'},
|
|
{0, 0, 0, 0}
|
|
};
|
|
|
|
void OptionParser::parseArguments(int argc, char *argv[], char **envp)
|
|
{
|
|
(void)envp;
|
|
int opt;
|
|
int optionIndex = 0;
|
|
|
|
argv0 = argv[0];
|
|
|
|
optind = 1; // Reset optind to 1 before parsing
|
|
while ((opt = getopt_long(
|
|
argc, argv, "s:d:a:p: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 'a':
|
|
senseApiLibs.push_back(optarg);
|
|
break;
|
|
case 'p':
|
|
{
|
|
struct stat info;
|
|
|
|
if (stat(optarg, &info) != 0 || !(info.st_mode & S_IFDIR))
|
|
{
|
|
throw std::invalid_argument(
|
|
std::string(__func__) + " - The specified path is not a "
|
|
"directory: " + optarg);
|
|
}
|
|
|
|
senseApiLibPath.push_back(optarg);
|
|
break;
|
|
}
|
|
case 'v':
|
|
verbose = true;
|
|
break;
|
|
case '?':
|
|
printUsage = true;
|
|
return;
|
|
default:
|
|
throw std::invalid_argument(
|
|
std::string(__func__) + " - Invalid argument encountered: "
|
|
+ std::string(argv[optind - 1]));
|
|
}
|
|
}
|
|
}
|
|
|
|
std::string OptionParser::getUsage() const
|
|
{
|
|
return "Usage: " + argv0 + " [-s|--devicespec|--spec|--devspec <device_spec>] "
|
|
"[-d|--devfile|--devicefile <filename>] "
|
|
"[-a|--sense-api-lib|--senseapi <filename>] "
|
|
"[-p|--sense-api-path <directory>] "
|
|
"[-v|--verbose] "
|
|
"[-?|--help]";
|
|
}
|
|
|
|
std::string OptionParser::stringifyOptions(void) const
|
|
{
|
|
std::ostringstream oss;
|
|
|
|
if (verbose) {
|
|
oss << "Verbose mode is on" << std::endl;
|
|
}
|
|
|
|
oss << "Device Specs: " << deviceSpecs << std::endl;
|
|
|
|
oss << "Device Spec Files: ";
|
|
for (const auto& file : deviceSpecFiles) {
|
|
oss << file << " ";
|
|
}
|
|
oss << std::endl;
|
|
|
|
oss << "Sense API Library Paths: ";
|
|
for (const auto& path : senseApiLibPath) {
|
|
oss << path << " ";
|
|
}
|
|
oss << std::endl;
|
|
oss << "Sense API Libraries: ";
|
|
for (const auto& lib : senseApiLibs) {
|
|
oss << lib << " ";
|
|
}
|
|
oss << std::endl;
|
|
|
|
return oss.str();
|
|
}
|