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)) {
235 char *tmpvalue = strdup(__opts[ (
char *)argn ]);
237 if ( strchr(tmpvalue,
':') != NULL ) {
239 *host = strtok_r(tmpvalue,
":", &save_ptr);
240 char *tmpport = strtok_r(NULL,
"", &save_ptr);
242 int port_num = atoi(tmpport);
243 if ( (port_num < 0) || (port_num > 0xFFFF) ) {
274 if ((__opts.count(argn) == 0) || (__opts[argn] == NULL))
return false;
276 std::string tmpvalue = __opts[argn];
278 size_t col_idx = tmpvalue.find_last_of(
':');
279 if ( col_idx == tmpvalue.npos ) {
284 host = tmpvalue.substr(0, col_idx);
285 std::string tmpport = tmpvalue.substr(col_idx + 1);
287 int port_num = atoi(tmpport.c_str());
288 if ( (port_num < 0) || (port_num > 0xFFFF) ) {
308 if ((__opts.count(argn) > 0) && (__opts[argn] != NULL)) {
310 long int rv = strtol(__opts[argn], &endptr, 10);
311 if ( endptr[0] != 0 ) {
316 throw Exception(
"Value for '%s' not available", argn);
332 if ((__opts.count(argn) > 0) && (__opts[argn] != NULL)) {
334 double rv = strtod(__opts[argn], &endptr);
335 if ( endptr[0] != 0 ) {
340 throw Exception(
"Value for '%s' not available", argn);
356 if (index < __items.size()) {
358 long int rv = strtol(__items[index], &endptr, 10);
359 if ( endptr[0] != 0 ) {
364 throw Exception(
"Value for item %u not available", index);
380 if (index < __items.size()) {
382 double rv = strtod(__items[index], &endptr);
383 if ( endptr[0] != 0 ) {
388 throw Exception(
"Value for item %u not available", index);
397 const std::vector< const char* > &
407 std::vector< const char* >::size_type
410 return __items.size();
430 return (
const char **)__argv;
440 return __program_name;
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.
double parse_item_float(unsigned int index)
Parse item as double.
const std::vector< const char * > & items() const
Get non-option items.
int argc() const
Get number of arguments.
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.
Base class for exceptions in Fawkes.
~ArgumentParser()
Destructor.
const char ** argv() const
Program argument array as supplied to constructor.
const char * program_name() const
Get name of program.
bool has_arg(const char *argn)
Check if argument has been supplied.
std::vector< const char * >::size_type num_items() const
Get number of non-option items.
Thrown if unknown argument was supplied.
Expected parameter is missing.
long int parse_item_int(unsigned int index)
Parse item as integer.