fawkes::ArgumentParser Class Reference

Parse command line arguments. More...

#include <utils/system/argparser.h>

List of all members.

Public Member Functions

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


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

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

Constructor.

Parameters:
argc argument count.
argv argument vector
opt_string option string, see ArgumentParser
long_options long options, see ArgumentParser

Definition at line 89 of file argparser.cpp.

fawkes::ArgumentParser::~ArgumentParser (  ) 

Destructor.

Definition at line 146 of file argparser.cpp.


Member Function Documentation

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:
argn argument name to retrieve
value a 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 191 of file argparser.cpp.

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

Get argument value.

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

Parameters:
argn argument 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 171 of file argparser.cpp.

Referenced by FawkesMainThread::FawkesMainThread(), FawkesMainThread::once(), PluginTool::PluginTool(), FireVisionNetworkTool::run(), and SkillGuiBatchRenderer::SkillGuiBatchRenderer().

int fawkes::ArgumentParser::argc (  )  const

Get number of arguments.

Returns:
number of arguments

Definition at line 406 of file argparser.cpp.

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

Program argument array as supplied to constructor.

Returns:
argument array.

Definition at line 416 of file argparser.cpp.

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

Check if argument has been supplied.

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

Definition at line 158 of file argparser.cpp.

Referenced by FawkesMainThread::FawkesMainThread(), FawkesMainThread::once(), PluginTool::PluginTool(), FireVisionNetworkTool::run(), and SkillGuiBatchRenderer::SkillGuiBatchRenderer().

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 386 of file argparser.cpp.

Referenced by SkillShellThread::loop(), and FireVisionNetworkTool::run().

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 396 of file argparser.cpp.

Referenced by SkillShellThread::loop(), and FireVisionNetworkTool::run().

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

Parse argument as double.

Converts the value of the given argument to a double.

Parameters:
argn argument name to retrieve
Returns:
value of string as double
Exceptions:
IllegalArgumentException thrown if the value cannot be properly converted to a double
Exception thrown if the argument has not been supplied

Definition at line 318 of file argparser.cpp.

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:
argn argument name to retrieve
host Upon successful return contains the hostname part
port upon successful return contains the port part (unchanged if not supplied)
Returns:
true, if the argument was supplied, false otherwise
Exceptions:
OutOfBoundsException thrown if port is not in the range [0..65535]

Definition at line 260 of file argparser.cpp.

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:
argn argument name to retrieve
host Upon successful return contains a pointer to a newly alloated string with the hostname part. Free it after you are finished.
port upon successful return contains the port part
Returns:
true, if the argument was supplied, false otherwise
Exceptions:
OutOfBoundsException thrown if port is not in the range [0..65535]

Definition at line 220 of file argparser.cpp.

Referenced by JoystickBlackBoardLogger::JoystickBlackBoardLogger(), JoystickBlackBoardPoster::JoystickBlackBoardPoster(), and SkillShellThread::SkillShellThread().

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

Parse argument as integer.

Converts the value of the given argument to an integer.

Parameters:
argn argument name to retrieve
Returns:
value of string as long int
Exceptions:
IllegalArgumentException thrown if the value cannot be properly converted to an integer
Exception thrown if the argument has not been supplied

Definition at line 294 of file argparser.cpp.

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

Parse item as double.

Converts the value of the given item to a double.

Parameters:
index item index
Returns:
value of string as double
Exceptions:
IllegalArgumentException thrown if the value cannot be properly converted to a double
Exception thrown if the argument has not been supplied

Definition at line 366 of file argparser.cpp.

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:
index item index
Returns:
value of string as long int
Exceptions:
IllegalArgumentException thrown if the value cannot be properly converted to an integer
Exception thrown if the argument has not been supplied

Definition at line 342 of file argparser.cpp.

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 426 of file argparser.cpp.

Referenced by PluginTool::PluginTool(), FireVisionNetworkTool::print_usage(), and SkillGuiBatchRenderer::usage().


The documentation for this class was generated from the following files:
Generated on Tue Feb 22 13:32:55 2011 for Fawkes API by  doxygen 1.4.7