24#include <core/exceptions/software.h>
25#include <utils/system/argparser.h>
94 opt_string_ = opt_string;
97 option *tmplo = long_options;
98 while (tmplo->name != 0) {
99 long_opts_.push_back(*tmplo);
108 program_name_ = strdup(basename(
argv[0]));
111 char *tmp = strdup(
argv[0]);
112 program_name_ = strdup(basename(tmp));
116 if (long_options == NULL) {
120 while ((c = getopt(
argc,
argv, opt_string)) != -1) {
123 }
else if (c ==
':') {
126 sprintf(tmp,
"%c", c);
132 while ((c = getopt_long(
argc,
argv, opt_string, long_options, &opt_ind)) != -1) {
137 opts_[long_options[opt_ind].name] = optarg;
140 sprintf(tmp,
"%c", c);
149 items_.push_back(
argv[ind++]);
167 return (opts_.count((
char *)argn) > 0);
179 if ((opts_.count(argn) > 0) && (opts_[argn] != NULL)) {
180 return opts_[(
char *)argn];
198 if ((opts_.count(argn) > 0) && (opts_[argn] != NULL)) {
199 *value = strdup(opts_[(
char *)argn]);
226 if ((opts_.count(argn) > 0) && (opts_[argn] != NULL)) {
254 size_t num_colons = 0;
255 std::string::size_type idx = 0;
256 while ((idx = tmp.find(
':', idx)) != std::string::npos) {
261 if (num_colons == 1) {
263 *host = strdup(tmp.substr(0, idx).c_str());
264 if (!tmp.substr(idx + 1).empty()) {
265 *port = atoi(tmp.substr(idx + 1).c_str());
267 }
else if (num_colons > 1) {
271 std::string::size_type closing_idx = tmp.find(
']');
272 if (closing_idx == std::string::npos) {
273 throw Exception(
"No closing bracket for IPv6 address");
274 }
else if (closing_idx < (tmp.length() - 1)) {
276 if (tmp[closing_idx + 1] !=
':') {
277 throw Exception(
"Expected colon after closing IPv6 address bracket");
278 }
else if (closing_idx > tmp.length() - 3) {
280 "Malformed IPv6 address with port, not enough characters after closing bracket");
282 *host = strdup(tmp.substr(1, closing_idx - 1).c_str());
283 *port = atoi(tmp.substr(closing_idx + 2).c_str());
287 *host = strdup(tmp.substr(1, closing_idx - 2).c_str());
291 *host = strdup(tmp.c_str());
295 *host = strdup(tmp.c_str());
315 if ((opts_.count(argn) == 0) || (opts_[argn] == NULL))
318 char * tmp_host = NULL;
319 unsigned short int tmp_port = port;
343 char * tmp_host = NULL;
344 unsigned short int tmp_port = port;
361 if ((opts_.count(argn) > 0) && (opts_[argn] != NULL)) {
363 long int rv = strtol(opts_[argn], &endptr, 10);
364 if (endptr[0] != 0) {
369 throw Exception(
"Value for '%s' not available", argn);
384 if ((opts_.count(argn) > 0) && (opts_[argn] != NULL)) {
386 double rv = strtod(opts_[argn], &endptr);
387 if (endptr[0] != 0) {
392 throw Exception(
"Value for '%s' not available", argn);
407 if (index < items_.size()) {
409 long int rv = strtol(items_[index], &endptr, 10);
410 if (endptr[0] != 0) {
415 throw Exception(
"Value for item %u not available", index);
430 if (index < items_.size()) {
432 double rv = strtod(items_[index], &endptr);
433 if (endptr[0] != 0) {
438 throw Exception(
"Value for item %u not available", index);
446const std::vector<const char *> &
455std::vector<const char *>::size_type
458 return items_.size();
476 return (
const char **)argv_;
485 return program_name_;
int argc() const
Get number of arguments.
const char * program_name() const
Get name of program.
~ArgumentParser()
Destructor.
double parse_item_float(unsigned int index)
Parse item as double.
ArgumentParser(int argc, char **argv, const char *opt_string, option *long_options=NULL)
Constructor.
const std::vector< const char * > & items() const
Get non-option items.
static void parse_hostport_s(const char *s, char **host, unsigned short int *port)
Parse host:port string.
const char * arg(const char *argn)
Get argument value.
bool parse_hostport(const char *argn, char **host, unsigned short int *port)
Parse host:port string.
std::vector< constchar * >::size_type num_items() const
Get number of non-option items.
const char ** argv() const
Program argument array as supplied to constructor.
double parse_float(const char *argn)
Parse argument as double.
long int parse_int(const char *argn)
Parse argument as integer.
long int parse_item_int(unsigned int index)
Parse item as integer.
bool has_arg(const char *argn)
Check if argument has been supplied.
Base class for exceptions in Fawkes.
Expected parameter is missing.
Thrown if required argument was missing.
Thrown if unknown argument was supplied.
Fawkes library namespace.