cmdopts: Add --interceptor, --extrospector, --actuator and --devicefile
This commit is contained in:
+1
-1
@@ -1,4 +1,4 @@
|
||||
AM_CPPFLAGS+= -I"$(top_srcdir)/hcore/include"
|
||||
|
||||
noinst_LIBRARIES = libhcore.a
|
||||
libhcore_a_SOURCES = mind.cpp
|
||||
libhcore_a_SOURCES = mind.cpp opts.cpp
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
#ifndef OPTS_H
|
||||
#define OPTS_H
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <getopt.h>
|
||||
|
||||
// Define a class to hold the options and parse arguments
|
||||
class OptionParser
|
||||
{
|
||||
public:
|
||||
OptionParser() : verbose(false) {}
|
||||
~OptionParser() = default;
|
||||
|
||||
void parseArguments(int argc, char *argv[]);
|
||||
void dumpOptions() const;
|
||||
std::string getUsage() const;
|
||||
|
||||
private:
|
||||
std::vector<std::string> extro;
|
||||
std::vector<std::string> intero;
|
||||
std::vector<std::string> actuator;
|
||||
std::string devFile;
|
||||
bool verbose;
|
||||
|
||||
static struct option longOptions[];
|
||||
};
|
||||
|
||||
#endif // OPTS_H
|
||||
@@ -0,0 +1,86 @@
|
||||
#include <opts.h>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <getopt.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
struct option OptionParser::longOptions[] = {
|
||||
{"extro", required_argument, 0, 'e'},
|
||||
{"extrospector", required_argument, 0, 'e'},
|
||||
{"intero", required_argument, 0, 'i'},
|
||||
{"interoceptor", required_argument, 0, 'i'},
|
||||
{"act", required_argument, 0, 'a'},
|
||||
{"actuator", required_argument, 0, 'a'},
|
||||
{"devfile", required_argument, 0, 'd'},
|
||||
{"devicefile", required_argument, 0, 'd'},
|
||||
{"verbose", no_argument, 0, 'v'},
|
||||
{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, "e:i:a:d:v", longOptions, &optionIndex)) != -1)
|
||||
{
|
||||
switch (opt)
|
||||
{
|
||||
case 'e':
|
||||
extro.push_back(optarg);
|
||||
break;
|
||||
case 'i':
|
||||
intero.push_back(optarg);
|
||||
break;
|
||||
case 'a':
|
||||
actuator.push_back(optarg);
|
||||
break;
|
||||
case 'd':
|
||||
devFile = optarg;
|
||||
break;
|
||||
case 'v':
|
||||
verbose = true;
|
||||
break;
|
||||
default:
|
||||
throw std::invalid_argument("Invalid argument encountered: " + std::string(argv[optind - 1]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::string OptionParser::getUsage() const
|
||||
{
|
||||
return "Usage: program [-e|--extro|--extrospector <sensor_dev_spec>] "
|
||||
"[-i|--intero|--interoceptor <sensor_dev_spec>] "
|
||||
"[-a|--act|--actuator <actuator_dev_spec>] "
|
||||
"[-d|--devfile|--devicefile <filename>] "
|
||||
"[-v|--verbose]";
|
||||
}
|
||||
|
||||
void OptionParser::dumpOptions() const
|
||||
{
|
||||
if (verbose) {
|
||||
std::cout << "Verbose mode is on" << std::endl;
|
||||
}
|
||||
|
||||
std::cout << "Extro: ";
|
||||
for (const auto& extro : extro) {
|
||||
std::cout << extro << " ";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
std::cout << "Intero: ";
|
||||
for (const auto& intero : intero) {
|
||||
std::cout << intero << " ";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
std::cout << "Actuator: ";
|
||||
for (const auto& actuator : actuator) {
|
||||
std::cout << actuator << " ";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
std::cout << "Dev file: " << devFile << std::endl;
|
||||
}
|
||||
@@ -1,22 +1,31 @@
|
||||
#include <iostream>
|
||||
#include <exception>
|
||||
#include <opts.h>
|
||||
#include <mind.h>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
try {
|
||||
OptionParser options;
|
||||
hk::Mind mind;
|
||||
|
||||
try {
|
||||
std::cout <<PACKAGE_NAME <<" " <<PACKAGE_VERSION <<std::endl;
|
||||
|
||||
try {
|
||||
options.parseArguments(argc, argv);
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "Exception occurred: " <<e.what() <<std::endl;
|
||||
catch (const std::invalid_argument& e) {
|
||||
std::cerr << e.what() << std::endl;
|
||||
std::cerr << options.getUsage() << std::endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
catch (...) {
|
||||
|
||||
options.dumpOptions();
|
||||
}
|
||||
catch (...) {
|
||||
std::cerr << "Unknown exception occurred" <<std::endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user