Top level command line parsing function called after all options have been parsed and created from the format strings. This function parses the command line (argc,argv) stored internally in the constructor. Each command line argument is parsed and checked to see if it matches an existing option. If there is no match, and error is reported and the function returns early. If there is a match, all
| 317 | // function returns early. If there is a match, all the arguments for |
| 318 | // that option are parsed and the associated variables are set. |
| 319 | int |
| 320 | ArgParse::parse (int xargc, const char **xargv) |
| 321 | { |
| 322 | m_argc = xargc; |
| 323 | m_argv = xargv; |
| 324 | |
| 325 | for (int i = 1; i < m_argc; i++) { |
| 326 | if (m_argv[i][0] == '-' && |
| 327 | (isalpha (m_argv[i][1]) || m_argv[i][1] == '-')) { // flag |
| 328 | ArgOption *option = find_option (m_argv[i]); |
| 329 | if (option == NULL) { |
| 330 | error ("Invalid option \"%s\"", m_argv[i]); |
| 331 | return -1; |
| 332 | } |
| 333 | |
| 334 | option->found_on_command_line(); |
| 335 | |
| 336 | if (option->is_flag()) { |
| 337 | option->set_parameter(0, NULL); |
| 338 | } else { |
| 339 | assert (option->is_regular()); |
| 340 | for (int j = 0; j < option->parameter_count(); j++) { |
| 341 | if (j+i+1 >= m_argc) { |
| 342 | error ("Missing parameter %d from option " |
| 343 | "\"%s\"", j+1, option->name().c_str()); |
| 344 | return -1; |
| 345 | } |
| 346 | option->set_parameter (j, m_argv[i+j+1]); |
| 347 | } |
| 348 | i += option->parameter_count(); |
| 349 | } |
| 350 | } else { |
| 351 | // not an option nor an option parameter, glob onto global list |
| 352 | if (m_global) |
| 353 | m_global->invoke_callback (1, m_argv+i); |
| 354 | else { |
| 355 | error ("Argument \"%s\" does not have an associated " |
| 356 | "option", m_argv[i]); |
| 357 | return -1; |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | return 0; |
| 363 | } |
| 364 | |
| 365 | |
| 366 |