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)) {
255 size_t num_colons = 0;
256 std::string::size_type idx = 0;
257 while ((idx = tmp.find(
':', idx)) != std::string::npos) {
262 if (num_colons == 1) {
264 *host = strdup(tmp.substr(0, idx).c_str());
265 if (!tmp.substr(idx + 1).empty()) {
266 *port = atoi(tmp.substr(idx + 1).c_str());
268 }
else if (num_colons > 1) {
272 std::string::size_type closing_idx = tmp.find(
']');
273 if (closing_idx == std::string::npos) {
274 throw Exception(
"No closing bracket for IPv6 address");
275 }
else if (closing_idx < (tmp.length() - 1)) {
277 if (tmp[closing_idx + 1] !=
':') {
278 throw Exception(
"Expected colon after closing IPv6 address bracket");
279 }
else if (closing_idx > tmp.length() - 3) {
281 "Malformed IPv6 address with port, not enough characters after closing bracket");
283 *host = strdup(tmp.substr(1, closing_idx - 1).c_str());
284 *port = atoi(tmp.substr(closing_idx + 2).c_str());
288 *host = strdup(tmp.substr(1, closing_idx - 2).c_str());
292 *host = strdup(tmp.c_str());
296 *host = strdup(tmp.c_str());
316 if ((opts_.count(argn) == 0) || (opts_[argn] == NULL))
319 char * tmp_host = NULL;
320 unsigned short int tmp_port = port;
345 char * tmp_host = NULL;
346 unsigned short int tmp_port = port;
363 if ((opts_.count(argn) > 0) && (opts_[argn] != NULL)) {
365 long int rv = strtol(opts_[argn], &endptr, 10);
366 if (endptr[0] != 0) {
371 throw Exception(
"Value for '%s' not available", argn);
386 if ((opts_.count(argn) > 0) && (opts_[argn] != NULL)) {
388 double rv = strtod(opts_[argn], &endptr);
389 if (endptr[0] != 0) {
394 throw Exception(
"Value for '%s' not available", argn);
409 if (index < items_.size()) {
411 long int rv = strtol(items_[index], &endptr, 10);
412 if (endptr[0] != 0) {
417 throw Exception(
"Value for item %u not available", index);
432 if (index < items_.size()) {
434 double rv = strtod(items_[index], &endptr);
435 if (endptr[0] != 0) {
440 throw Exception(
"Value for item %u not available", index);
448 const std::vector<const char *> &
457 std::vector<const char *>::size_type
460 return items_.size();
478 return (
const char **)argv_;
487 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.
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.
const std::vector< const char * > & items() const
Get 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.
std::vector< const char * >::size_type num_items() const
Get number of non-option items.
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.