Util: Add argument parsing to ArgParser
This commit is contained in:
+16
-2
@@ -1,6 +1,7 @@
|
||||
#ifndef ARG_PARSER_H
|
||||
#define ARG_PARSER_H
|
||||
|
||||
#include <cstddef> // size_t
|
||||
#include <functional> // function
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
@@ -40,6 +41,13 @@ public:
|
||||
};
|
||||
|
||||
struct Argument {
|
||||
const char* name { nullptr };
|
||||
const char* usageString { nullptr };
|
||||
const char* manString { nullptr };
|
||||
size_t minValues { 0 };
|
||||
size_t maxValues { 1 };
|
||||
size_t addedValues { 0 };
|
||||
std::function<bool(const char*)> acceptValue;
|
||||
};
|
||||
|
||||
bool parse(int argc, const char* argv[]);
|
||||
@@ -54,6 +62,10 @@ public:
|
||||
void addOption(double& value, char shortName, const char* longName, const char* usageString, const char* manString, const char* argumentName = "", Required requiresArgument = Required::No);
|
||||
void addOption(std::vector<std::string>& values, char shortName, const char* longName, const char* usageString, const char* manString, const char* argumentName = "", Required requiresArgument = Required::No);
|
||||
|
||||
void addArgument(Argument&& argument);
|
||||
void addArgument(std::string& value, const char* name, const char* usageString, const char* manString, Required required = Required::No);
|
||||
void addArgument(std::vector<std::string>& values, const char* name, const char* usageString, const char* manString, Required required = Required::No);
|
||||
|
||||
void setErrorReporting(bool state) { m_errorReporting = state; }
|
||||
void setExitOnFirstError(bool state) { m_exitOnFirstError = state; }
|
||||
void setStopParsingOnFirstNonOption(bool state) { m_stopParsingOnFirstNonOption = state; }
|
||||
@@ -63,15 +75,17 @@ private:
|
||||
void printOptionError(const char* name, Error error, bool longName = true);
|
||||
bool parseShortOption(std::string_view option, std::string_view next);
|
||||
bool parseLongOption(std::string_view option, std::string_view next);
|
||||
bool parseArgument(std::string_view argument);
|
||||
|
||||
bool m_errorReporting { true };
|
||||
bool m_exitOnFirstError { true };
|
||||
bool m_stopParsingOnFirstNonOption { false };
|
||||
|
||||
int m_optionIndex { 1 };
|
||||
size_t m_optionIndex { 1 };
|
||||
size_t m_argumentIndex { 0 };
|
||||
bool m_nonOptionMode { false };
|
||||
|
||||
const char* m_name;
|
||||
const char* m_name { nullptr };
|
||||
std::vector<Option> m_options;
|
||||
std::vector<Argument> m_arguments;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user