Fawkes API  Fawkes Development Version
fawkes::ArgumentParser Class Reference

Parse command line arguments. More...

#include <>>

Public Member Functions

 ArgumentParser (int argc, char **argv, const char *opt_string, option *long_options=NULL)
 Constructor. More...
 
 ~ArgumentParser ()
 Destructor. More...
 
bool has_arg (const char *argn)
 Check if argument has been supplied. More...
 
const char * arg (const char *argn)
 Get argument value. More...
 
bool arg (const char *argn, char **value)
 Get argument while checking availability. More...
 
const char * program_name () const
 Get name of program. More...
 
bool parse_hostport (const char *argn, char **host, unsigned short int *port)
 Parse host:port string. More...
 
bool parse_hostport (const char *argn, std::string &host, unsigned short int &port)
 Parse host:port string. More...
 
long int parse_int (const char *argn)
 Parse argument as integer. More...
 
double parse_float (const char *argn)
 Parse argument as double. More...
 
long int parse_item_int (unsigned int index)
 Parse item as integer. More...
 
double parse_item_float (unsigned int index)
 Parse item as double. More...
 
const std::vector< const char *> & items () const
 Get non-option items. More...
 
std::vector< const char *>::size_type num_items () const
 Get number of non-option items. More...
 
int argc () const
 Get number of arguments. More...
 
const char ** argv () const
 Program argument array as supplied to constructor. More...
 
std::string get_optstring () const
 Get option string. More...
 
std::vector< option > get_long_opts () const
 Get long option configuration. More...
 

Static Public Member Functions

static void parse_hostport_s (const char *s, char **host, unsigned short int *port)
 Parse host:port string. More...
 
static void parse_hostport_s (const char *s, std::string &host, unsigned short int &port)
 Parse host:port string. More...
 

Detailed Description

Parse command line arguments.

Interface to GNU getopt and getopt_long. Parses command line arguments and separates long and short options.

The supplied opt_string is a string containing the legitimate option characters. A character c denotes an option of the type "-c" (single dash). If such a character is followed by a colon, the option requires an argument, Two colons mean an option takes an optional arg.

If long_options is supplied options started out by two dashes are recognized. Long option names may be abbreviated if the abbreviation is unique or is an exact match for some defined option. A long option may take a parameter, of the form –arg=param or –arg param.

long_options is a pointer to the first element of an array of struct option declared in <getopt.h> as

struct option {
const char *name;
int has_arg;
int *flag;
int val;
};

The meanings of the different fields are:

name is the name of the long option.

has_arg is: no_argument (or 0) if the option does not take an argument; required_argument (or 1) if the option requires an argument; or optional_argument (or 2) if the option takes an optional argument.

flag specifies how results are returned for a long option. If flag is NULL, then getopt_long() returns val. (For example, the calling program may set val to the equivalent short option character.) Otherwise, getopt_long() returns 0, and flag points to a variable which is set to val if the option is found, but left unchanged if the option is not found. Handled internally in ArgumentParser

For more information see man 3 getopt.

All arguments that do not belong to parsed options are stored as items and can be retrieved via items().

Definition at line 66 of file argparser.h.

Constructor & Destructor Documentation

◆ ArgumentParser()

fawkes::ArgumentParser::ArgumentParser ( int  argc,
char **  argv,
const char *  opt_string,
option *  long_options = NULL 
)

Constructor.

Parameters
argcargument count.
argvargument vector
opt_stringoption string, see ArgumentParser
long_optionslong options, see ArgumentParser

Definition at line 89 of file argparser.cpp.

References argc(), and argv().

◆ ~ArgumentParser()

fawkes::ArgumentParser::~ArgumentParser ( )

Destructor.

Definition at line 157 of file argparser.cpp.

Member Function Documentation

◆ arg() [1/2]

const char * fawkes::ArgumentParser::arg ( const char *  argn)

Get argument value.

Use this method to get the value supplied to the given option.

Parameters
argnargument name to retrieve
Returns
the argument value. Pointer to static program array. Do not free! Returns NULL if argument was not supplied on command line.

Definition at line 182 of file argparser.cpp.

Referenced by JoystickBlackBoardActListener::bb_interface_message_received(), FawkesBeepDaemon::handle_signal(), fawkes::runtime::InitOptions::InitOptions(), LaserDeadSpotCalibrator::num_detected_spots(), and PluginTool::PluginTool().

◆ arg() [2/2]

bool fawkes::ArgumentParser::arg ( const char *  argn,
char **  value 
)

Get argument while checking availability.

The argument will be a newly allocated copy of the string. You have to free it after you are done with it.

Parameters
argnargument name to retrieve
valuea pointer to a newly allocated copy of the argument value will be stored here if the argument has been found. The value is unchanged if argument was not supplied.
Returns
true, if the argument was supplied, false otherwise

Definition at line 202 of file argparser.cpp.

◆ argc()

int fawkes::ArgumentParser::argc ( ) const

Get number of arguments.

Returns
number of arguments

Definition at line 482 of file argparser.cpp.

Referenced by ArgumentParser().

◆ argv()

const char ** fawkes::ArgumentParser::argv ( ) const

Program argument array as supplied to constructor.

Returns
argument array.

Definition at line 492 of file argparser.cpp.

Referenced by ArgumentParser().

◆ get_long_opts()

std::vector<option> fawkes::ArgumentParser::get_long_opts ( ) const
inline

Get long option configuration.

Returns
vector of long options used to create instance

Definition at line 104 of file argparser.h.

◆ get_optstring()

std::string fawkes::ArgumentParser::get_optstring ( ) const
inline

Get option string.

Returns
option string used to create instance

Definition at line 99 of file argparser.h.

◆ has_arg()

bool fawkes::ArgumentParser::has_arg ( const char *  argn)

Check if argument has been supplied.

Parameters
argnargument name to check for
Returns
true, if the argument was given on the command line, false otherwise

Definition at line 169 of file argparser.cpp.

Referenced by JoystickBlackBoardActListener::bb_interface_message_received(), SkillShellThread::connection_established(), FawkesBeepDaemon::handle_signal(), fawkes::runtime::InitOptions::InitOptions(), LaserDeadSpotCalibrator::num_detected_spots(), PluginTool::PluginTool(), and ConfigChangeWatcherTool::run().

◆ items()

const std::vector< const char *> & fawkes::ArgumentParser::items ( ) const

Get non-option items.

Returns
pointer to vector of pointer to non-argument values. Handled internally, do not free or delete!

Definition at line 462 of file argparser.cpp.

Referenced by fawkes::runtime::InitOptions::InitOptions(), LaserDeadSpotCalibrator::num_detected_spots(), and ConfigChangeWatcherTool::run().

◆ num_items()

std::vector< const char *>::size_type fawkes::ArgumentParser::num_items ( ) const

Get number of non-option items.

Returns
number of non-opt items.

Definition at line 472 of file argparser.cpp.

Referenced by fawkes::runtime::InitOptions::InitOptions(), and LaserDeadSpotCalibrator::num_detected_spots().

◆ parse_float()

double fawkes::ArgumentParser::parse_float ( const char *  argn)

Parse argument as double.

Converts the value of the given argument to a double.

Parameters
argnargument name to retrieve
Returns
value of string as double
Exceptions
IllegalArgumentExceptionthrown if the value cannot be properly converted to a double
Exceptionthrown if the argument has not been supplied

Definition at line 394 of file argparser.cpp.

Referenced by LaserDeadSpotCalibrator::num_detected_spots().

◆ parse_hostport() [1/2]

bool fawkes::ArgumentParser::parse_hostport ( const char *  argn,
char **  host,
unsigned short int *  port 
)

Parse host:port string.

The value referenced by the given argn is parsed for the pattern "host:port". If the string does not match this pattern an exception is thrown. The host will be a newly allocated copy of the string. You have to free it after you are done with it. If no port is supplied in the string (plain hostname string) the port argument is left unchanged. If the argument has not been supplied at all both values are left unchanged. Thus it is safe to put the default values into the variables before passing them to this method. Note however that you have to free the returned host string in case of a successful return, and only in that case probably!

Parameters
argnargument name to retrieve
hostUpon successful return contains a pointer to a newly alloated string with the hostname part. Free it after you are finished.
portupon successful return contains the port part
Returns
true, if the argument was supplied, false otherwise
Exceptions
OutOfBoundsExceptionthrown if port is not in the range [0..65535]

Definition at line 231 of file argparser.cpp.

References parse_hostport_s().

Referenced by JoystickBlackBoardActListener::bb_interface_message_received(), JoystickBlackBoardLogger::JoystickBlackBoardLogger(), LaserDeadSpotCalibrator::num_detected_spots(), parse_hostport(), ConfigChangeWatcherTool::run(), and SkillShellThread::SkillShellThread().

◆ parse_hostport() [2/2]

bool fawkes::ArgumentParser::parse_hostport ( const char *  argn,
std::string &  host,
unsigned short int &  port 
)

Parse host:port string.

The value referenced by the given argn is parsed for the pattern "host:port". If the string does not match this pattern an exception is thrown. If no port is supplied in the string (plain hostname string) the port argument is left unchanged. If the argument has not been supplied at all both values are left unchanged. Thus it is safe to put the default values into the variables before passing them to this method.

Parameters
argnargument name to retrieve
hostUpon successful return contains the hostname part
portupon successful return contains the port part (unchanged if not supplied)
Returns
true, if the argument was supplied, false otherwise
Exceptions
OutOfBoundsExceptionthrown if port is not in the range [0..65535]

Definition at line 323 of file argparser.cpp.

References parse_hostport().

◆ parse_hostport_s() [1/2]

void fawkes::ArgumentParser::parse_hostport_s ( const char *  s,
char **  host,
unsigned short int *  port 
)
static

Parse host:port string.

The value referenced by the given argn is parsed for the pattern "host:port". If the string does not match this pattern an exception is thrown. The host will be a newly allocated copy of the string. You have to free it after you are done with it. If no port is supplied in the string (plain hostname string) the port argument is left unchanged. If the argument has not been supplied at all both values are left unchanged. Thus it is safe to put the default values into the variables before passing them to this method. Note however that you have to free the returned host string in case of a successful return, and only in that case probably!

Parameters
sstring to parse
hostUpon successful return contains a pointer to a newly alloated string with the hostname part. Free it after you are finished.
portupon successful return contains the port part
Returns
true, if the argument was supplied, false otherwise
Exceptions
Exceptionthrown on parsing error

Definition at line 260 of file argparser.cpp.

Referenced by fawkes::ServiceChooserDialog::get_selected_service(), parse_hostport(), and parse_hostport_s().

◆ parse_hostport_s() [2/2]

void fawkes::ArgumentParser::parse_hostport_s ( const char *  s,
std::string &  host,
unsigned short int &  port 
)
static

Parse host:port string.

The value referenced by the given argn is parsed for the pattern "host:port". If the string does not match this pattern an exception is thrown. If no port is supplied in the string (plain hostname string) the port argument is left unchanged. If the argument has not been supplied at all both values are left unchanged. Thus it is safe to put the default values into the variables before passing them to this method.

Parameters
sstring to parse
hostUpon successful return contains the hostname part
portupon successful return contains the port part (unchanged if not supplied)
Returns
true, if the argument was supplied, false otherwise
Exceptions
OutOfBoundsExceptionthrown if port is not in the range [0..65535]

Definition at line 351 of file argparser.cpp.

References parse_hostport_s().

◆ parse_int()

long int fawkes::ArgumentParser::parse_int ( const char *  argn)

Parse argument as integer.

Converts the value of the given argument to an integer.

Parameters
argnargument name to retrieve
Returns
value of string as long int
Exceptions
IllegalArgumentExceptionthrown if the value cannot be properly converted to an integer
Exceptionthrown if the argument has not been supplied

Definition at line 370 of file argparser.cpp.

Referenced by fawkes::runtime::InitOptions::InitOptions(), and LaserDeadSpotCalibrator::num_detected_spots().

◆ parse_item_float()

double fawkes::ArgumentParser::parse_item_float ( unsigned int  index)

Parse item as double.

Converts the value of the given item to a double.

Parameters
indexitem index
Returns
value of string as double
Exceptions
IllegalArgumentExceptionthrown if the value cannot be properly converted to a double
Exceptionthrown if the argument has not been supplied

Definition at line 442 of file argparser.cpp.

◆ parse_item_int()

long int fawkes::ArgumentParser::parse_item_int ( unsigned int  index)

Parse item as integer.

Converts the value of the given item to an integer.

Parameters
indexitem index
Returns
value of string as long int
Exceptions
IllegalArgumentExceptionthrown if the value cannot be properly converted to an integer
Exceptionthrown if the argument has not been supplied

Definition at line 418 of file argparser.cpp.

Referenced by LaserDeadSpotCalibrator::num_detected_spots().

◆ program_name()

const char * fawkes::ArgumentParser::program_name ( ) const

Get name of program.

Returns
the name of the program (argv[0] of argument vector supplied to constructor).

Definition at line 502 of file argparser.cpp.

Referenced by fawkes::runtime::InitOptions::InitOptions(), LaserDeadSpotCalibrator::num_detected_spots(), PluginTool::PluginTool(), and ConfigChangeWatcherTool::run().


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