Primary entry point. This function accepts a set of format strings and variable pointers. Each string contains an option name and a scanf-like format string to enumerate the arguments of that option (eg. "-option %d %f %s"). The format string is followed by a list of pointers to the argument variables, just like scanf. All format strings and arguments are parsed to create a list of ArgOption o
| 373 | // After all ArgOptions are created, the command line is parsed and |
| 374 | // the sublist option callbacks are invoked. |
| 375 | int |
| 376 | ArgParse::options (const char *intro, ...) |
| 377 | { |
| 378 | va_list ap; |
| 379 | va_start (ap, intro); |
| 380 | |
| 381 | m_intro = intro; |
| 382 | for (const char *cur = va_arg(ap, char *); cur; cur = va_arg(ap, char *)) { |
| 383 | if (find_option (cur) && |
| 384 | strcmp(cur, "<SEPARATOR>")) { |
| 385 | error ("Option \"%s\" is multiply defined", cur); |
| 386 | return -1; |
| 387 | } |
| 388 | |
| 389 | // Build a new option and then parse the values |
| 390 | ArgOption *option = new ArgOption (cur); |
| 391 | if (option->initialize() < 0) { |
| 392 | return -1; |
| 393 | } |
| 394 | |
| 395 | if (cur[0] == '\0' || |
| 396 | (cur[0] == '%' && cur[1] == '*' && cur[2] == '\0')) { |
| 397 | // set default global option |
| 398 | m_global = option; |
| 399 | } |
| 400 | |
| 401 | // Grab any parameters and store them with this option |
| 402 | for (int i = 0; i < option->parameter_count(); i++) { |
| 403 | void *p = va_arg (ap, void *); |
| 404 | if (p == NULL) { |
| 405 | error ("Missing argument parameter for \"%s\"", |
| 406 | option->name().c_str()); |
| 407 | return -1; |
| 408 | } |
| 409 | |
| 410 | option->add_parameter (i, p); |
| 411 | |
| 412 | if (option == m_global) |
| 413 | option->set_callback ((ArgOption::callback_t)p); |
| 414 | } |
| 415 | |
| 416 | // Last argument is description |
| 417 | option->description ((const char *) va_arg (ap, const char *)); |
| 418 | m_option.push_back(option); |
| 419 | } |
| 420 | |
| 421 | va_end (ap); |
| 422 | return 0; |
| 423 | } |
| 424 | |
| 425 | |
| 426 |