ARGoS
3
A parallel, multi-engine simulator for swarm robotics
|
Easy-to-use command line argument parser. More...
#include <command_line_arg_parser.h>
Classes | |
class | CAbstractArgument |
class | CArgument |
Public Member Functions | |
CCommandLineArgParser () | |
Class constructor. | |
virtual | ~CCommandLineArgParser () |
Class destructor. | |
void | AddFlag (char ch_short_option, const std::string &str_long_option, const std::string &str_description, bool &b_flag) |
Adds a flag to the parser. | |
template<typename T > | |
void | AddArgument (char ch_short_option, const std::string &str_long_option, const std::string &str_description, T &t_buffer) |
Adds an argument to the parser. | |
virtual void | PrintUsage (CARGoSLog &c_log) |
Prints the arguments on the log. | |
virtual void | Parse (SInt32 n_argc, char **ppch_argv) |
Parses the arguments on the command line. |
Easy-to-use command line argument parser.
Example: you want to parse
$ program --flag --arg1 intvalue --arg2 stringvalue # GNU-style $ program -f -1 intvalue -2 stringvalue # short style
The code to do it:
int main(int argc, char** argv) { bool bIsFlag; // will be true if the flag was specified, false otherwise UInt32 unIntValue; // stores th parsed intvalue std::string strValue; // stores the parsed stringvalue
// Create the parser CCommandLineArgParser cCLAP; // Add the flag cCLAP.AddFlag( 'f', "--flag", "This is a flag", bIsFlag); // Add the int argument cCLAP.AddArgument<UInt32>( '1', "--arg1", "This is an int value", unIntValue); // Add the string argument cCLAP.AddArgument<std::string>( '2', "--arg2", "This is a string value", strValue);
// Parse the command line! // In case of errors, a CARGoSException is thrown try { cCLAP.Parse(argc, argv); } catch(CARGoSException& ex) { LOGERR << "Error: " << ex.what() << std::endl; }
Definition at line 90 of file command_line_arg_parser.h.
Class constructor.
Definition at line 15 of file command_line_arg_parser.cpp.
argos::CCommandLineArgParser::~CCommandLineArgParser | ( | ) | [virtual] |
Class destructor.
Definition at line 22 of file command_line_arg_parser.cpp.
void argos::CCommandLineArgParser::AddArgument | ( | char | ch_short_option, |
const std::string & | str_long_option, | ||
const std::string & | str_description, | ||
T & | t_buffer | ||
) | [inline] |
Adds an argument to the parser.
ch_short_option | A single character for the short option |
str_long_option | The long option |
str_description | The description shown by PrintUsage() |
t_buffer | The buffer variable to associate to this entry |
Definition at line 132 of file command_line_arg_parser.h.
void argos::CCommandLineArgParser::AddFlag | ( | char | ch_short_option, |
const std::string & | str_long_option, | ||
const std::string & | str_description, | ||
bool & | b_flag | ||
) | [inline] |
Adds a flag to the parser.
ch_short_option | A single character for the short option |
str_long_option | The long option |
str_description | The description shown by PrintUsage() |
b_flag | The boolean variable to associate to this entry |
Definition at line 111 of file command_line_arg_parser.h.
void argos::CCommandLineArgParser::Parse | ( | SInt32 | n_argc, |
char ** | ppch_argv | ||
) | [virtual] |
Parses the arguments on the command line.
n_argc | The number of arguments to parse |
ppch_argv | The string array containing the command line |
Reimplemented in argos::CARGoSCommandLineArgParser.
Definition at line 48 of file command_line_arg_parser.cpp.
void argos::CCommandLineArgParser::PrintUsage | ( | CARGoSLog & | c_log | ) | [virtual] |
Prints the arguments on the log.
If you want a better layout, you need to extend this method.
c_log | The destination log: LOG or LOGERR |
Reimplemented in argos::CARGoSCommandLineArgParser.
Definition at line 32 of file command_line_arg_parser.cpp.