Fawkes API
Fawkes Development Version
|
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... | |
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
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.
fawkes::ArgumentParser::ArgumentParser | ( | int | argc, |
char ** | argv, | ||
const char * | opt_string, | ||
option * | long_options = NULL |
||
) |
Constructor.
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 157 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.
argn | argument name to retrieve |
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().
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.
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. |
Definition at line 202 of file argparser.cpp.
int fawkes::ArgumentParser::argc | ( | ) | const |
Get number of arguments.
Definition at line 482 of file argparser.cpp.
Referenced by ArgumentParser().
const char ** fawkes::ArgumentParser::argv | ( | ) | const |
Program argument array as supplied to constructor.
Definition at line 492 of file argparser.cpp.
Referenced by ArgumentParser().
|
inline |
Get long option configuration.
Definition at line 104 of file argparser.h.
|
inline |
Get option string.
Definition at line 99 of file argparser.h.
bool fawkes::ArgumentParser::has_arg | ( | const char * | argn | ) |
Check if argument has been supplied.
argn | argument name to check for |
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().
const std::vector< const char *> & fawkes::ArgumentParser::items | ( | ) | const |
Get non-option items.
Definition at line 462 of file argparser.cpp.
Referenced by fawkes::runtime::InitOptions::InitOptions(), LaserDeadSpotCalibrator::num_detected_spots(), and ConfigChangeWatcherTool::run().
std::vector< const char *>::size_type fawkes::ArgumentParser::num_items | ( | ) | const |
Get number of non-option items.
Definition at line 472 of file argparser.cpp.
Referenced by fawkes::runtime::InitOptions::InitOptions(), and LaserDeadSpotCalibrator::num_detected_spots().
double fawkes::ArgumentParser::parse_float | ( | const char * | argn | ) |
Parse argument as double.
Converts the value of the given argument to a double.
argn | argument name to retrieve |
IllegalArgumentException | thrown if the value cannot be properly converted to a double |
Exception | thrown if the argument has not been supplied |
Definition at line 394 of file argparser.cpp.
Referenced by LaserDeadSpotCalibrator::num_detected_spots().
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!
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 |
OutOfBoundsException | thrown 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().
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.
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) |
OutOfBoundsException | thrown if port is not in the range [0..65535] |
Definition at line 323 of file argparser.cpp.
References parse_hostport().
|
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!
s | string to parse |
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 |
Exception | thrown on parsing error |
Definition at line 260 of file argparser.cpp.
Referenced by fawkes::ServiceChooserDialog::get_selected_service(), parse_hostport(), and parse_hostport_s().
|
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.
s | string to parse |
host | Upon successful return contains the hostname part |
port | upon successful return contains the port part (unchanged if not supplied) |
OutOfBoundsException | thrown if port is not in the range [0..65535] |
Definition at line 351 of file argparser.cpp.
References parse_hostport_s().
long int fawkes::ArgumentParser::parse_int | ( | const char * | argn | ) |
Parse argument as integer.
Converts the value of the given argument to an integer.
argn | argument name to retrieve |
IllegalArgumentException | thrown if the value cannot be properly converted to an integer |
Exception | thrown 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().
double fawkes::ArgumentParser::parse_item_float | ( | unsigned int | index | ) |
Parse item as double.
Converts the value of the given item to a double.
index | item index |
IllegalArgumentException | thrown if the value cannot be properly converted to a double |
Exception | thrown if the argument has not been supplied |
Definition at line 442 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.
index | item index |
IllegalArgumentException | thrown if the value cannot be properly converted to an integer |
Exception | thrown if the argument has not been supplied |
Definition at line 418 of file argparser.cpp.
Referenced by LaserDeadSpotCalibrator::num_detected_spots().
const char * fawkes::ArgumentParser::program_name | ( | ) | const |
Get name of program.
Definition at line 502 of file argparser.cpp.
Referenced by fawkes::runtime::InitOptions::InitOptions(), LaserDeadSpotCalibrator::num_detected_spots(), PluginTool::PluginTool(), and ConfigChangeWatcherTool::run().