2021-12-10 23:39:26 +01:00
|
|
|
#ifndef DEF_ArgParseCpp
|
|
|
|
|
#define DEF_ArgParseCpp
|
|
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <map>
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <cstdlib>
|
|
|
|
|
|
|
|
|
|
#include <utils.hpp>
|
|
|
|
|
|
|
|
|
|
std::vector<std::string> SplitString(const std::string & str, char sep);
|
|
|
|
|
std::string RemovePrefixDashes(const std::string & str);
|
|
|
|
|
|
|
|
|
|
namespace ArgParseCpp
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/// Used to describe the parser parameter type.
|
|
|
|
|
enum ParameterType
|
|
|
|
|
{
|
|
|
|
|
StandaloneParam, // Represents all parameters such as "--param"
|
|
|
|
|
SingleValueParam, // Represents all parameters such as "--param value"
|
|
|
|
|
EqualParam // Represents all parameters such as "--param=value"
|
|
|
|
|
// ListParam // Represents all parameters such as "--param=value1,value2,..,valueN"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// Used to describe the parser parameter value type.
|
|
|
|
|
enum ParameterValueType
|
|
|
|
|
{
|
|
|
|
|
Bool,
|
|
|
|
|
Int,
|
|
|
|
|
Double,
|
|
|
|
|
String
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// Converts a ParameterValueType into a string for display.
|
|
|
|
|
std::string ParameterValueType2Str(const ParameterValueType & paramValueType);
|
|
|
|
|
|
|
|
|
|
/// This class defines an argument parser parameter, with its short and long name, its type, its value type, and its docstring.
|
|
|
|
|
class ParserParam
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
/// Builds a new parser param object with the specified parameters.
|
|
|
|
|
ParserParam(const std::string & shortName_, const std::string & longName_, ParameterType type_, ParameterValueType valueType_ = ArgParseCpp::Bool, const std::string & docString_ = "");
|
|
|
|
|
|
|
|
|
|
/// Generates automatically a doc string based on the parameter type and value type, and repaces the current doc string with the newly generated one.
|
|
|
|
|
const std::string & GenerateDocString();
|
|
|
|
|
|
|
|
|
|
/// If no doc string has been specified, it is automatically generated.
|
|
|
|
|
void AutoGenerateDocString();
|
|
|
|
|
|
|
|
|
|
/// Returns either the full name without prefix dashes, or the short name without prefix dashes.
|
|
|
|
|
std::string SimpleName() const;
|
|
|
|
|
|
|
|
|
|
/// Returns the help entry string : "-p --param value Parameter "param" with value "value_param" of type integer number"
|
|
|
|
|
std::string GetHelpEntryStr() const;
|
|
|
|
|
|
2021-12-11 20:21:08 +01:00
|
|
|
/// Parses the argument(s) given, according to the parameter type. Returns true if the parsing was successful.
|
|
|
|
|
bool Parse(const std::string & arg, std::string argval = "");
|
|
|
|
|
|
|
|
|
|
/// Returns true if the provided name corresponds to either the short name or the long name, even if the parameter is of type "equal"
|
|
|
|
|
bool IsThisParamNamed(const std::string & name) const;
|
|
|
|
|
|
|
|
|
|
/// Returns whether the parameter's value is valid or not
|
|
|
|
|
bool IsValid() const;
|
|
|
|
|
|
2021-12-10 23:39:26 +01:00
|
|
|
public:
|
|
|
|
|
std::string shortName;
|
|
|
|
|
std::string longName;
|
|
|
|
|
ParameterType type;
|
|
|
|
|
ParameterValueType valueType;
|
|
|
|
|
std::string docString;
|
2021-12-11 20:21:08 +01:00
|
|
|
|
|
|
|
|
bool validity; //! indicates whether the parameter's value is valid or not
|
|
|
|
|
|
|
|
|
|
// Storage for all types of values the parameter can have
|
|
|
|
|
bool boolVal;
|
|
|
|
|
int intVal;
|
|
|
|
|
double doubleVal;
|
|
|
|
|
std::string stringVal;
|
2021-12-10 23:39:26 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// This class implements a simple argument parser in C++. It can be used to parse a variety of input arguments to programs.
|
|
|
|
|
class Parser
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
/// Creates an empty parser.
|
|
|
|
|
Parser();
|
|
|
|
|
|
|
|
|
|
/// Adds a parameter to be parsed.
|
|
|
|
|
void AddParameter(ParserParam param);
|
|
|
|
|
|
|
|
|
|
/// Parses the arguments passed as parameters.
|
2021-12-11 20:21:08 +01:00
|
|
|
bool Parse(int argc, char **argv);
|
2021-12-10 23:39:26 +01:00
|
|
|
|
|
|
|
|
/// Parses the arguments passed as a single string.
|
2021-12-11 20:21:08 +01:00
|
|
|
bool Parse(const std::string & argString);
|
2021-12-10 23:39:26 +01:00
|
|
|
|
|
|
|
|
/// Parses the arguments passed as parameters.
|
2021-12-11 20:21:08 +01:00
|
|
|
bool Parse(std::vector<std::string> args);
|
2021-12-10 23:39:26 +01:00
|
|
|
|
|
|
|
|
/// Prints the custom help message if it has been defined, or an automatically constructed one.
|
|
|
|
|
void PrintHelp() const;
|
|
|
|
|
|
|
|
|
|
/// Prints the parsed arguments and their associated values.
|
|
|
|
|
void PrintParsedArguments() const;
|
|
|
|
|
|
2021-12-11 20:21:08 +01:00
|
|
|
/// Returns the value of the specified argument.
|
|
|
|
|
bool GetArgumentValueBool(const std::string & argName);
|
|
|
|
|
int GetArgumentValueInt(const std::string & argName);
|
|
|
|
|
double GetArgumentValueDouble(const std::string & argName);
|
|
|
|
|
std::string const& GetArgumentValueString(const std::string & argName);
|
2021-12-10 23:39:26 +01:00
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
/// Cleans the argument list passed as parameter.
|
|
|
|
|
void CleanArgs(std::vector<std::string> & args) const;
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
std::vector<ParserParam> parserParams;
|
|
|
|
|
std::string customHelpMessage;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace ArgParseCpp
|
|
|
|
|
|
|
|
|
|
#endif
|