#ifndef DEF_ArgParseCpp #define DEF_ArgParseCpp #include #include #include #include #include #include std::vector 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; public: std::string shortName; std::string longName; ParameterType type; ParameterValueType valueType; std::string docString; }; /// 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. void Parse(int argc, char **argv); /// Parses the arguments passed as a single string. void Parse(const std::string & argString); /// Parses the arguments passed as parameters. void Parse(std::vector 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. A conversion is done between the input text and the output value type. template T GetArgumentValue(const std::string & argName) { } protected: /// Cleans the argument list passed as parameter. void CleanArgs(std::vector & args) const; protected: std::vector parserParams; std::map boolParameters; std::map intParameters; std::map doubleParameters; std::map stringParameters; std::string customHelpMessage; }; } // namespace ArgParseCpp #endif