Cmdline: use exceptions for control flow
This is generally frowned upon but it makes this code 10x cleaner. We handle commandLine usage msg printing by using exceptions for control flow. This allows us to centralize the logic for killing the Mind threads in one place. At least with respect to printing the usage msg.
This commit is contained in:
@@ -4,6 +4,8 @@
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <getopt.h>
|
||||
#include <stdexcept>
|
||||
#include <exception>
|
||||
|
||||
// Define a class to hold the options and parse arguments
|
||||
class OptionParser
|
||||
@@ -23,6 +25,14 @@ public:
|
||||
}
|
||||
|
||||
public:
|
||||
class Exception : public std::exception
|
||||
{
|
||||
public:
|
||||
Exception() = default;
|
||||
~Exception() override = default;
|
||||
const char* what() const noexcept override = 0;
|
||||
};
|
||||
|
||||
std::string argv0;
|
||||
std::vector<std::string> senseApiLibPath;
|
||||
std::vector<std::string> senseApiLibs;
|
||||
@@ -33,4 +43,25 @@ public:
|
||||
static struct option longOptions[];
|
||||
};
|
||||
|
||||
class OptionsParserError
|
||||
: public std::invalid_argument, public OptionParser::Exception
|
||||
{
|
||||
public:
|
||||
OptionsParserError(
|
||||
const std::string& errorMessage, const OptionParser& parser);
|
||||
|
||||
const char* what() const noexcept override;
|
||||
};
|
||||
|
||||
class JustPrintUsageNoError
|
||||
: public OptionParser::Exception
|
||||
{
|
||||
public:
|
||||
explicit JustPrintUsageNoError(const OptionParser& parser);
|
||||
const char* what() const noexcept override;
|
||||
|
||||
private:
|
||||
std::string message;
|
||||
};
|
||||
|
||||
#endif // OPTS_H
|
||||
|
||||
Reference in New Issue
Block a user