125 lines
4.6 KiB
C++
125 lines
4.6 KiB
C++
#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;
|
|
|
|
/// 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". If "permissive" is set to true, even the parameter name without preceding "-" will match the parameter.
|
|
bool IsThisParamNamed(const std::string & name, bool permissive = false) const;
|
|
|
|
/// Returns whether the parameter's value is valid or not
|
|
bool IsValid() const;
|
|
|
|
public:
|
|
std::string shortName;
|
|
std::string longName;
|
|
ParameterType type;
|
|
ParameterValueType valueType;
|
|
std::string docString;
|
|
|
|
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;
|
|
};
|
|
|
|
/// 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.
|
|
bool Parse(int argc, char **argv);
|
|
|
|
/// Parses the arguments passed as a single string.
|
|
bool Parse(const std::string & argString);
|
|
|
|
/// Parses the arguments passed as parameters.
|
|
bool Parse(std::vector<std::string> args);
|
|
|
|
/// 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;
|
|
|
|
/// 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);
|
|
|
|
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
|