ArgParse is doxygen-commented

master
Denes Matetelki 13 years ago
parent dba6b1c91d
commit d1bdd12a2c

@ -6,8 +6,10 @@
#include <map> #include <map>
#include <set> #include <set>
// Aims for the functionality of Python argparse /** @brief Aim to achieve the functionality of Python argparse
// http://docs.python.org/library/argparse.html#module-argparse *
* http://docs.python.org/library/argparse.html#module-argparse
*/
class ArgParse class ArgParse
{ {
@ -26,21 +28,46 @@ public:
REQUIRED REQUIRED
}; };
/** @param description Exmplanation, before the usage lines.
* @param epilog Lines after the usage and options. Usually contact e-mail.
* @param addHelp Add a "-h,--help" option.
*/
ArgParse(const std::string description, ArgParse(const std::string description,
const std::string epilog = std::string(""), const std::string epilog = std::string(""),
const bool add_Help = true); const bool addHelp = true);
void addArgument(const std::string name, // "-f,--foo" /** @brief Adds an argument which the object will accept.
*
* @param name short and/or long form: "-f,--foo"
* @param help Description of the argument, printed when --help is given.
* @param type Type of the paramterer, required by the argument.
* @param valueRequired Parameter requiered/optional after the argument.
* @param valueName Default is the type. But some short text can be better.
* @param choices Comma separeted list of strings: "yes,no,maybe"
* or a range accepted numbers: NUM..NUM
*/
void addArgument(const std::string name,
const std::string help, const std::string help,
const enum ValueType type = NONE, const enum ValueType type = NONE,
const enum ValueRequired valueRequired = REQUIRED, const enum ValueRequired valueRequired = REQUIRED,
const std::string valueName = std::string(""), const std::string valueName = std::string(""),
const std::string choices = std::string("")); // {"yes,no"} or range: {1..100} const std::string choices = std::string(""));
// throw(std::runtime_error) /** @brief Parse command line arguments according to the accepted arguments.
bool parseArgs(const int argc, *
* Wrapper around the other version of parseArgs.
*
* @param argc Argumetn counter of the main function.
* @param argv Argument vector of the main function.
* @throw std::runtime_error When the command line args are bad.
* Shall be cought be the client code!
* @throw std::logic_error If the addArgument was bad.
* @todo addArgument shall handle this!
*/
void parseArgs(const int argc,
const char* argv[]); const char* argv[]);
bool parseArgs(const std::list<std::string> argList);
void parseArgs(const std::list<std::string> argList);
// is this arg in the map of understood arguments? // is this arg in the map of understood arguments?
bool isArg(const std::string arg) const; bool isArg(const std::string arg) const;

@ -16,13 +16,13 @@
ArgParse::ArgParse(const std::string description, ArgParse::ArgParse(const std::string description,
const std::string epilog, const std::string epilog,
const bool add_Help) const bool addHelp)
: m_description(description) : m_description(description)
, m_epilog(epilog) , m_epilog(epilog)
, m_programName() , m_programName()
, m_params() , m_params()
{ {
if (add_Help) if (addHelp)
addArgument("-h,--help", "Prints this help message"); addArgument("-h,--help", "Prints this help message");
} }
@ -51,7 +51,7 @@ bool ArgParse::parseArgs(const int argc,
} }
bool ArgParse::parseArgs(const std::list<std::string> argList) void ArgParse::parseArgs(const std::list<std::string> argList)
{ {
m_programName = argList.front(); m_programName = argList.front();
@ -180,15 +180,13 @@ bool ArgParse::parseArgs(const std::list<std::string> argList)
(*it2).second.m_valueHasBeenSet = true; (*it2).second.m_valueHasBeenSet = true;
++it; ++it;
} }
return true;
} }
bool ArgParse::isArg(const std::string arg) const void ArgParse::isArg(const std::string arg) const
{ {
ArgMap::const_iterator it = m_params.find(arg); ArgMap::const_iterator it = m_params.find(arg);
return it != m_params.end(); it != m_params.end();
} }
@ -388,7 +386,8 @@ ArgParse::parseCommaSepStringToSet(const std::string s) const
return stringSet; return stringSet;
} }
std::string ArgParse::typeToString(const ValueType type, std::string valueName) const std::string
ArgParse::typeToString(const ValueType type, std::string valueName) const
{ {
if ( type != NONE && valueName.empty() ) { if ( type != NONE && valueName.empty() ) {
switch ( type ) { switch ( type ) {

Loading…
Cancel
Save