argos::CCommandLineArgParser Class Reference

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

#include <command_line_arg_parser.h>

Inheritance diagram for argos::CCommandLineArgParser:

Public Member Functions

 CCommandLineArgParser ()
 Class constructor. More...
 
virtual ~CCommandLineArgParser ()
 Class destructor. More...
 
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. More...
 
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. More...
 
virtual void PrintUsage (CARGoSLog &c_log)
 Prints the arguments on the log. More...
 
virtual void Parse (SInt32 n_argc, char **ppch_argv)
 Parses the arguments on the command line. More...
 

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

◆ CCommandLineArgParser()

argos::CCommandLineArgParser::CCommandLineArgParser ( )

Class constructor.

Definition at line 15 of file command_line_arg_parser.cpp.

◆ ~CCommandLineArgParser()

argos::CCommandLineArgParser::~CCommandLineArgParser ( )
virtual

Class destructor.

Definition at line 22 of file command_line_arg_parser.cpp.

Member Function Documentation

◆ AddArgument()

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.

◆ AddFlag()

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.

◆ Parse()

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.

◆ PrintUsage()

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.

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.