Fawkes API  Fawkes Development Version
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
fawkes::ArgumentParser Class Reference

Parse command line arguments. More...

#include <>>

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.
std::string get_optstring () const
 Get option string.
std::vector< option > get_long_opts () const
 Get long option configuration.

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:
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().

fawkes::ArgumentParser::~ArgumentParser ( )

Destructor.

Definition at line 157 of file argparser.cpp.


Member Function Documentation

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 fawkes::runtime::InitOptions::InitOptions(), and PluginTool::PluginTool().

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.

int fawkes::ArgumentParser::argc ( ) const

Get number of arguments.

Returns:
number of arguments

Definition at line 418 of file argparser.cpp.

Referenced by ArgumentParser().

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

Program argument array as supplied to constructor.

Returns:
argument array.

Definition at line 428 of file argparser.cpp.

Referenced by ArgumentParser().

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 100 of file argparser.h.

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

Get option string.

Returns:
option string used to create instance

Definition at line 95 of file argparser.h.

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 fawkes::runtime::InitOptions::InitOptions(), and PluginTool::PluginTool().

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

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

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 330 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:
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.

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

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

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

Referenced by fawkes::runtime::InitOptions::InitOptions().

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 378 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:
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 354 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 438 of file argparser.cpp.

Referenced by fawkes::runtime::InitOptions::InitOptions(), and PluginTool::PluginTool().


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