24 #include <utils/system/argparser.h> 25 #include <core/exceptions/software.h> 90 const char *opt_string, option *long_options)
95 __opt_string = opt_string;
98 option *tmplo = long_options;
99 while (tmplo->name != 0) {
100 __long_opts.push_back(*tmplo);
109 __program_name = strdup(basename( argv[0] ));
112 char *tmp = strdup(argv[0]);
113 __program_name = strdup(basename(tmp));
117 if (long_options == NULL) {
121 while ((c = getopt(argc, argv, opt_string)) != -1) {
124 }
else if (c ==
':') {
127 sprintf(tmp,
"%c", c);
128 __opts[ tmp ] = optarg;
133 while ((c = getopt_long(argc, argv, opt_string, long_options, &opt_ind)) != -1) {
138 __opts[ long_options[opt_ind].name ] = optarg;
141 sprintf(tmp,
"%c", c);
142 __opts[ tmp ] = optarg;
150 __items.push_back( argv[ind++] );
159 free(__program_name);
171 return (__opts.count((
char *)argn) > 0);
184 if ((__opts.count(argn) > 0) && (__opts[argn] != NULL)) {
185 return __opts[ (
char *)argn ];
204 if ((__opts.count(argn) > 0) && (__opts[argn] != NULL)) {
205 *value = strdup(__opts[ (
char *)argn ]);
232 unsigned short int *port)
234 if ((__opts.count(argn) > 0) && (__opts[argn] != NULL)) {
261 unsigned short int *port)
264 size_t num_colons = 0;
265 std::string::size_type idx = 0;
266 while ((idx = tmp.find(
':', idx)) != std::string::npos) {
271 if (num_colons == 1) {
273 *host = strdup(tmp.substr(0, idx).c_str());
274 if (! tmp.substr(idx+1).empty()) {
275 *port = atoi(tmp.substr(idx+1).c_str());
277 }
else if (num_colons > 1) {
281 std::string::size_type closing_idx = tmp.find(
']');
282 if (closing_idx == std::string::npos) {
283 throw Exception(
"No closing bracket for IPv6 address");
284 }
else if (closing_idx < (tmp.length() - 1)) {
286 if (tmp[closing_idx + 1] !=
':') {
287 throw Exception(
"Expected colon after closing IPv6 address bracket");
288 }
else if (closing_idx > tmp.length() - 3) {
289 throw Exception(
"Malformed IPv6 address with port, not enough characters after closing bracket");
291 *host = strdup(tmp.substr(1, closing_idx - 1).c_str());
292 *port = atoi(tmp.substr(closing_idx + 2).c_str());
296 *host = strdup(tmp.substr(1, closing_idx - 2).c_str());
300 *host = strdup(tmp.c_str());
304 *host = strdup(tmp.c_str());
325 if ((__opts.count(argn) == 0) || (__opts[argn] == NULL))
return false;
327 char *tmp_host = NULL;
328 unsigned short int tmp_port = port;
353 char *tmp_host = NULL;
354 unsigned short int tmp_port = port;
372 if ((__opts.count(argn) > 0) && (__opts[argn] != NULL)) {
374 long int rv = strtol(__opts[argn], &endptr, 10);
375 if ( endptr[0] != 0 ) {
380 throw Exception(
"Value for '%s' not available", argn);
396 if ((__opts.count(argn) > 0) && (__opts[argn] != NULL)) {
398 double rv = strtod(__opts[argn], &endptr);
399 if ( endptr[0] != 0 ) {
404 throw Exception(
"Value for '%s' not available", argn);
420 if (index < __items.size()) {
422 long int rv = strtol(__items[index], &endptr, 10);
423 if ( endptr[0] != 0 ) {
428 throw Exception(
"Value for item %u not available", index);
444 if (index < __items.size()) {
446 double rv = strtod(__items[index], &endptr);
447 if ( endptr[0] != 0 ) {
452 throw Exception(
"Value for item %u not available", index);
461 const std::vector< const char* > &
471 std::vector< const char* >::size_type
474 return __items.size();
494 return (
const char **)__argv;
504 return __program_name;
const char * program_name() const
Get name of program.
const char * arg(const char *argn)
Get argument value.
Thrown if required argument was missing.
bool parse_hostport(const char *argn, char **host, unsigned short int *port)
Parse host:port string.
double parse_float(const char *argn)
Parse argument as double.
const std::vector< const char *> & items() const
Get non-option items.
Fawkes library namespace.
double parse_item_float(unsigned int index)
Parse item as double.
const char ** argv() const
Program argument array as supplied to constructor.
long int parse_int(const char *argn)
Parse argument as integer.
ArgumentParser(int argc, char **argv, const char *opt_string, option *long_options=NULL)
Constructor.
std::vector< const char *>::size_type num_items() const
Get number of non-option items.
Base class for exceptions in Fawkes.
int argc() const
Get number of arguments.
~ArgumentParser()
Destructor.
bool has_arg(const char *argn)
Check if argument has been supplied.
Thrown if unknown argument was supplied.
Expected parameter is missing.
static void parse_hostport_s(const char *s, char **host, unsigned short int *port)
Parse host:port string.
long int parse_item_int(unsigned int index)
Parse item as integer.