cmdopts: Add a -?/--help option
This commit is contained in:
@@ -9,19 +9,19 @@
|
|||||||
class OptionParser
|
class OptionParser
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
OptionParser() : verbose(false) {}
|
OptionParser() : verbose(false), printUsage(false) {}
|
||||||
~OptionParser() = default;
|
~OptionParser() = default;
|
||||||
|
|
||||||
void parseArguments(int argc, char *argv[]);
|
void parseArguments(int argc, char *argv[]);
|
||||||
void dumpOptions() const;
|
void dumpOptions() const;
|
||||||
std::string getUsage() const;
|
std::string getUsage() const;
|
||||||
|
|
||||||
private:
|
public:
|
||||||
std::vector<std::string> extro;
|
std::vector<std::string> extro;
|
||||||
std::vector<std::string> intero;
|
std::vector<std::string> intero;
|
||||||
std::vector<std::string> actuator;
|
std::vector<std::string> actuator;
|
||||||
std::string devFile;
|
std::string devFile;
|
||||||
bool verbose;
|
bool verbose, printUsage;
|
||||||
|
|
||||||
static struct option longOptions[];
|
static struct option longOptions[];
|
||||||
};
|
};
|
||||||
|
|||||||
+7
-2
@@ -15,6 +15,7 @@ struct option OptionParser::longOptions[] = {
|
|||||||
{"devfile", required_argument, 0, 'd'},
|
{"devfile", required_argument, 0, 'd'},
|
||||||
{"devicefile", required_argument, 0, 'd'},
|
{"devicefile", required_argument, 0, 'd'},
|
||||||
{"verbose", no_argument, 0, 'v'},
|
{"verbose", no_argument, 0, 'v'},
|
||||||
|
{"help", no_argument, 0, '?'},
|
||||||
{0, 0, 0, 0}
|
{0, 0, 0, 0}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -24,7 +25,7 @@ void OptionParser::parseArguments(int argc, char *argv[])
|
|||||||
int optionIndex = 0;
|
int optionIndex = 0;
|
||||||
optind = 1; // Reset optind to 1 before parsing
|
optind = 1; // Reset optind to 1 before parsing
|
||||||
while ((opt = getopt_long(
|
while ((opt = getopt_long(
|
||||||
argc, argv, "e:i:a:d:v", longOptions, &optionIndex)) != -1)
|
argc, argv, "e:i:a:d:v?", longOptions, &optionIndex)) != -1)
|
||||||
{
|
{
|
||||||
switch (opt)
|
switch (opt)
|
||||||
{
|
{
|
||||||
@@ -43,6 +44,9 @@ void OptionParser::parseArguments(int argc, char *argv[])
|
|||||||
case 'v':
|
case 'v':
|
||||||
verbose = true;
|
verbose = true;
|
||||||
break;
|
break;
|
||||||
|
case '?':
|
||||||
|
printUsage = true;
|
||||||
|
return;
|
||||||
default:
|
default:
|
||||||
throw std::invalid_argument("Invalid argument encountered: " + std::string(argv[optind - 1]));
|
throw std::invalid_argument("Invalid argument encountered: " + std::string(argv[optind - 1]));
|
||||||
}
|
}
|
||||||
@@ -55,7 +59,8 @@ std::string OptionParser::getUsage() const
|
|||||||
"[-i|--intero|--interoceptor <sensor_dev_spec>] "
|
"[-i|--intero|--interoceptor <sensor_dev_spec>] "
|
||||||
"[-a|--act|--actuator <actuator_dev_spec>] "
|
"[-a|--act|--actuator <actuator_dev_spec>] "
|
||||||
"[-d|--devfile|--devicefile <filename>] "
|
"[-d|--devfile|--devicefile <filename>] "
|
||||||
"[-v|--verbose]";
|
"[-v|--verbose] "
|
||||||
|
"[-?|--help]";
|
||||||
}
|
}
|
||||||
|
|
||||||
void OptionParser::dumpOptions() const
|
void OptionParser::dumpOptions() const
|
||||||
|
|||||||
@@ -15,11 +15,14 @@ try {
|
|||||||
options.parseArguments(argc, argv);
|
options.parseArguments(argc, argv);
|
||||||
}
|
}
|
||||||
catch (const std::invalid_argument& e) {
|
catch (const std::invalid_argument& e) {
|
||||||
std::cerr << e.what() << std::endl;
|
std::cerr << e.what() <<'\n' << options.getUsage() <<'\n';
|
||||||
std::cerr << options.getUsage() << std::endl;
|
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (options.printUsage) {
|
||||||
|
std::cout << options.getUsage() << std::endl;
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
options.dumpOptions();
|
options.dumpOptions();
|
||||||
}
|
}
|
||||||
catch (...) {
|
catch (...) {
|
||||||
|
|||||||
Reference in New Issue
Block a user