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);
446 const std::vector<const char *> &
455 std::vector<const char *>::size_type
458 return items_.size();
476 return (
const char **)argv_;
485 return program_name_;