ARGoS  3
A parallel, multi-engine simulator for swarm robotics
argos::CCommandLineArgParser Class Reference

Easy-to-use command line argument parser. More...

#include <command_line_arg_parser.h>

+ Inheritance diagram for argos::CCommandLineArgParser:

List of all members.

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.

Detailed Description

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.


Constructor & Destructor Documentation

Class constructor.

Definition at line 15 of file command_line_arg_parser.cpp.

Class destructor.

Definition at line 22 of file command_line_arg_parser.cpp.


Member Function Documentation

template<typename T >
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.

Parameters:
ch_short_optionA single character for the short option
str_long_optionThe long option
str_descriptionThe description shown by PrintUsage()
t_bufferThe 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.

Parameters:
ch_short_optionA single character for the short option
str_long_optionThe long option
str_descriptionThe description shown by PrintUsage()
b_flagThe 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.

Parameters:
n_argcThe number of arguments to parse
ppch_argvThe string array containing the command line

Reimplemented in argos::CARGoSCommandLineArgParser.

Definition at line 48 of file command_line_arg_parser.cpp.

Prints the arguments on the log.

If you want a better layout, you need to extend this method.

Parameters:
c_logThe destination log: LOG or LOGERR
See also:
CARGoSLog

Reimplemented in argos::CARGoSCommandLineArgParser.

Definition at line 32 of file command_line_arg_parser.cpp.


The documentation for this class was generated from the following files: