| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
#include <esl_config.h> |
| 16 |
|
| 17 |
#include <stdlib.h> |
| 18 |
#include <stdio.h> |
| 19 |
#include <string.h> |
| 20 |
#include <ctype.h> |
| 21 |
|
| 22 |
#include "easel.h" |
| 23 |
#include "esl_getopts.h" |
| 24 |
|
| 25 |
|
| 26 |
static int set_option(ESL_GETOPTS *g, int opti, char *optarg, |
| 27 |
int setby, int do_alloc); |
| 28 |
static int get_optidx_exactly(const ESL_GETOPTS *g, char *optname, int *ret_opti); |
| 29 |
static int get_optidx_abbrev(ESL_GETOPTS *g, char *optname, int n, |
| 30 |
int *ret_opti); |
| 31 |
static int esl_getopts(ESL_GETOPTS *g, int *ret_opti, char **ret_optarg); |
| 32 |
static int process_longopt(ESL_GETOPTS *g, int *ret_opti, char **ret_optarg); |
| 33 |
static int process_stdopt(ESL_GETOPTS *g, int *ret_opti, char **ret_optarg); |
| 34 |
static int verify_type_and_range(ESL_GETOPTS *g, int i, char *val, int setby); |
| 35 |
static int verify_integer_range(char *arg, char *range); |
| 36 |
static int verify_real_range(char *arg, char *range); |
| 37 |
static int verify_char_range(char *arg, char *range); |
| 38 |
static int parse_rangestring(char *range, char c, char **ret_lowerp, |
| 39 |
int *ret_geq, char **ret_upperp, int *ret_leq); |
| 40 |
static int process_optlist(ESL_GETOPTS *g, char **ret_s, int *ret_opti); |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
ESL_GETOPTS * |
| 62 |
esl_getopts_Create(const ESL_OPTIONS *opt) |
| 63 |
{ |
| 64 |
ESL_GETOPTS *g = NULL; |
| 65 |
int status; |
| 66 |
int i; |
| 67 |
|
| 68 |
ESL_ALLOC(g, sizeof(ESL_GETOPTS)); |
| 69 |
|
| 70 |
g->opt = opt; |
| 71 |
g->argc = 0; |
| 72 |
g->argv = NULL; |
| 73 |
g->optind = 1; |
| 74 |
g->nfiles = 0; |
| 75 |
g->val = NULL; |
| 76 |
g->setby = NULL; |
| 77 |
g->valloc = NULL; |
| 78 |
g->optstring = NULL; |
| 79 |
g->spoof = NULL; |
| 80 |
g->spoof_argv= NULL; |
| 81 |
g->errbuf[0] = '\0'; |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
g->nopts = 0; |
| 97 |
while (g->opt[g->nopts].name != NULL) { |
| 98 |
if (g->opt[g->nopts].name[0] != '-') |
| 99 |
ESL_XEXCEPTION(eslEINVAL, "option %d didn't start with '-';\nyou may have forgotten to NULL-terminate the ESL_OPTIONS array", g->nopts); |
| 100 |
g->nopts++; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
ESL_ALLOC(g->val, sizeof(char *) * g->nopts); |
| 110 |
ESL_ALLOC(g->setby, sizeof(int) * g->nopts); |
| 111 |
ESL_ALLOC(g->valloc, sizeof(int) * g->nopts); |
| 112 |
|
| 113 |
for (i = 0; i < g->nopts; i++) |
| 114 |
{ |
| 115 |
g->val[i] = g->opt[i].defval; |
| 116 |
g->setby[i] = eslARG_SETBY_DEFAULT; |
| 117 |
g->valloc[i] = 0; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
for (i = 0; i < g->nopts; i++) |
| 124 |
if (verify_type_and_range(g, i, g->val[i], eslARG_SETBY_DEFAULT) != eslOK) |
| 125 |
ESL_XEXCEPTION(eslEINVAL, "%s\n", g->errbuf); |
| 126 |
return g; |
| 127 |
|
| 128 |
ERROR: |
| 129 |
esl_getopts_Destroy(g); |
| 130 |
return NULL; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
ESL_GETOPTS * |
| 199 |
esl_getopts_CreateDefaultApp(const ESL_OPTIONS *options, int nargs, int argc, char **argv, char *banner, char *usage) |
| 200 |
{ |
| 201 |
ESL_GETOPTS *go = NULL; |
| 202 |
|
| 203 |
go = esl_getopts_Create(options); |
| 204 |
if (esl_opt_ProcessCmdline(go, argc, argv) != eslOK || |
| 205 |
esl_opt_VerifyConfig(go) != eslOK) |
| 206 |
{ |
| 207 |
printf("Failed to parse command line: %s\n", go->errbuf); |
| 208 |
if (usage != NULL) esl_usage(stdout, argv[0], usage); |
| 209 |
printf("\nTo see more help on available options, do %s -h\n\n", argv[0]); |
| 210 |
exit(1); |
| 211 |
} |
| 212 |
if (esl_opt_GetBoolean(go, "-h") == TRUE) |
| 213 |
{ |
| 214 |
if (banner != NULL) esl_banner(stdout, argv[0], banner); |
| 215 |
if (usage != NULL) esl_usage (stdout, argv[0], usage); |
| 216 |
puts("\nOptions:"); |
| 217 |
esl_opt_DisplayHelp(stdout, go, 0, 2, 80); |
| 218 |
exit(0); |
| 219 |
} |
| 220 |
if (nargs != -1 && esl_opt_ArgNumber(go) != nargs) |
| 221 |
{ |
| 222 |
puts("Incorrect number of command line arguments."); |
| 223 |
esl_usage(stdout, argv[0], usage); |
| 224 |
printf("\nTo see more help on available options, do %s -h\n\n", argv[0]); |
| 225 |
exit(1); |
| 226 |
} |
| 227 |
return go; |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
int |
| 246 |
esl_getopts_Reuse(ESL_GETOPTS *g) |
| 247 |
{ |
| 248 |
int i; |
| 249 |
|
| 250 |
|
| 251 |
for (i = 0; i < g->nopts; i++) |
| 252 |
{ |
| 253 |
if (g->valloc[i] > 0) { free(g->val[i]); } |
| 254 |
g->val[i] = g->opt[i].defval; |
| 255 |
g->setby[i] = eslARG_SETBY_DEFAULT; |
| 256 |
g->valloc[i] = 0; |
| 257 |
} |
| 258 |
|
| 259 |
if (g->spoof != NULL) free(g->spoof); |
| 260 |
if (g->spoof_argv != NULL) free(g->spoof_argv); |
| 261 |
|
| 262 |
g->argc = 0; |
| 263 |
g->argv = NULL; |
| 264 |
g->optind = 1; |
| 265 |
g->nfiles = 0; |
| 266 |
g->optstring = NULL; |
| 267 |
g->spoof = NULL; |
| 268 |
g->spoof_argv = NULL; |
| 269 |
g->errbuf[0] = '\0'; |
| 270 |
return eslOK; |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
void |
| 282 |
esl_getopts_Destroy(ESL_GETOPTS *g) |
| 283 |
{ |
| 284 |
int i; |
| 285 |
|
| 286 |
if (g != NULL) |
| 287 |
{ |
| 288 |
if (g->val != NULL) |
| 289 |
{ |
| 290 |
|
| 291 |
|
| 292 |
for (i = 0; i < g->nopts; i++) |
| 293 |
if (g->valloc[i] > 0) |
| 294 |
free(g->val[i]); |
| 295 |
free(g->val); |
| 296 |
} |
| 297 |
if (g->setby != NULL) free(g->setby); |
| 298 |
if (g->valloc != NULL) free(g->valloc); |
| 299 |
if (g->spoof != NULL) free(g->spoof); |
| 300 |
if (g->spoof_argv != NULL) free(g->spoof_argv); |
| 301 |
free(g); |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
void |
| 313 |
esl_getopts_Dump(FILE *ofp, ESL_GETOPTS *g) |
| 314 |
{ |
| 315 |
int i, j; |
| 316 |
|
| 317 |
if (g->argv != NULL) |
| 318 |
{ |
| 319 |
fprintf(ofp, "argv[0]: %s\n", g->argv[0]); |
| 320 |
for (i = 1, j = g->optind; j < g->argc; j++, i++) |
| 321 |
fprintf(ofp, "argument %2d (argv[%2d]): %s\n", i, j, g->argv[j]); |
| 322 |
fputc('\n', ofp); |
| 323 |
} |
| 324 |
|
| 325 |
fprintf(ofp, "%12s %12s %9s\n", "Option", "Setting", "Set by"); |
| 326 |
fprintf(ofp, "------------ ------------ ---------\n"); |
| 327 |
|
| 328 |
for (i = 0; i < g->nopts; i++) |
| 329 |
{ |
| 330 |
fprintf(ofp, "%-12s ", g->opt[i].name); |
| 331 |
|
| 332 |
if (g->opt[i].type == eslARG_NONE) fprintf(ofp, "%-12s ", g->val[i] == NULL ? "off" : "on"); |
| 333 |
else fprintf(ofp, "%-12s ", g->val[i]); |
| 334 |
|
| 335 |
if (g->setby[i] == eslARG_SETBY_DEFAULT) fprintf(ofp, "(default) "); |
| 336 |
else if (g->setby[i] == eslARG_SETBY_CMDLINE) fprintf(ofp, "cmdline "); |
| 337 |
else if (g->setby[i] == eslARG_SETBY_ENV) fprintf(ofp, "environ "); |
| 338 |
else if (g->setby[i] >= eslARG_SETBY_CFGFILE) fprintf(ofp, "cfgfile "); |
| 339 |
|
| 340 |
fprintf(ofp, "\n"); |
| 341 |
} |
| 342 |
return; |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
char * |
| 364 |
esl_getopts_CreateOptsLine(ESL_GETOPTS *g) |
| 365 |
{ |
| 366 |
char *ret_string = (char *) malloc(256); |
| 367 |
char quotemark[2]; |
| 368 |
quotemark[0] = 34; |
| 369 |
quotemark[1] = '\0'; |
| 370 |
if (ret_string == NULL) return(NULL); |
| 371 |
int ret_length =256; |
| 372 |
int used = 1; |
| 373 |
ret_string[0] = '\0'; |
| 374 |
int i, optsize; |
| 375 |
for (i=0; i < g->nopts; i++){ |
| 376 |
if (g->setby[i] != eslARG_SETBY_DEFAULT && !((g->opt[i].type == eslARG_NONE) && (g->val[i] == 0))){ |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
optsize = strlen(g->opt[i].name) +2; |
| 381 |
if(g->opt[i].type != eslARG_NONE){ |
| 382 |
optsize+=(strlen(g->val[i])) + 1; |
| 383 |
if(g->opt[i].type ==eslARG_STRING){ |
| 384 |
optsize += 2; |
| 385 |
} |
| 386 |
} |
| 387 |
while(used + optsize > ret_length){ |
| 388 |
ret_string = realloc(ret_string, ret_length *2); |
| 389 |
ret_length *=2; |
| 390 |
if(ret_string == 0){ |
| 391 |
return(NULL); |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
strcat(ret_string, " "); |
| 396 |
strcat(ret_string, g->opt[i].name); |
| 397 |
if(g->opt[i].type != eslARG_NONE){ |
| 398 |
strcat(ret_string, " "); |
| 399 |
if(g->opt[i].type== eslARG_STRING){ |
| 400 |
strcat(ret_string, quotemark); |
| 401 |
} |
| 402 |
strcat(ret_string, g->val[i]); |
| 403 |
if(g->opt[i].type== eslARG_STRING){ |
| 404 |
strcat(ret_string, quotemark); |
| 405 |
} |
| 406 |
} |
| 407 |
used += optsize; |
| 408 |
} |
| 409 |
} |
| 410 |
return(ret_string); |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
int |
| 441 |
esl_opt_ProcessConfigfile(ESL_GETOPTS *g, char *filename, FILE *fp) |
| 442 |
{ |
| 443 |
char *buf = NULL; |
| 444 |
int n = 0; |
| 445 |
char *s; |
| 446 |
char *optname; |
| 447 |
char *optarg; |
| 448 |
char *comment; |
| 449 |
int line; |
| 450 |
int opti; |
| 451 |
int status; |
| 452 |
|
| 453 |
line = 0; |
| 454 |
while ((status = esl_fgets(&buf, &n, fp)) != eslEOF) |
| 455 |
{ |
| 456 |
if (status != eslOK) return status; |
| 457 |
|
| 458 |
line++; |
| 459 |
optname = NULL; |
| 460 |
optarg = NULL; |
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
s = buf; |
| 465 |
esl_strtok(&s, " \t\n", &optname); |
| 466 |
if (optname == NULL) continue; |
| 467 |
if (*optname == '#') continue; |
| 468 |
if (*optname != '-') |
| 469 |
ESL_FAIL(eslESYNTAX, g->errbuf, |
| 470 |
"Parse failed at line %d of cfg file %.24s (saw %.24s, not an option)\n", |
| 471 |
line, filename, optname); |
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
if (*s == '"') esl_strtok(&s, "\"", &optarg); |
| 476 |
else esl_strtok(&s, " \t\n", &optarg); |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
esl_strtok(&s, " \t\n", &comment); |
| 481 |
if (comment != NULL && *comment != '#') |
| 482 |
ESL_FAIL(eslESYNTAX, g->errbuf, |
| 483 |
"Parse failed at line %d of cfg file %.24s (saw %.24s, not a comment)\n", |
| 484 |
line, filename, comment); |
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
if (get_optidx_exactly(g, optname, &opti) != eslOK) |
| 490 |
ESL_FAIL(eslESYNTAX, g->errbuf, |
| 491 |
"%.24s is not a recognized option (config file %.24s, line %d)\n", |
| 492 |
optname, filename, line); |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
status = set_option(g, opti, optarg, |
| 500 |
eslARG_SETBY_CFGFILE+g->nfiles, |
| 501 |
TRUE); |
| 502 |
if (status != eslOK) return status; |
| 503 |
} |
| 504 |
|
| 505 |
if (buf != NULL) free(buf); |
| 506 |
g->nfiles++; |
| 507 |
return eslOK; |
| 508 |
} |
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
int |
| 536 |
esl_opt_ProcessEnvironment(ESL_GETOPTS *g) |
| 537 |
{ |
| 538 |
int i; |
| 539 |
char *optarg; |
| 540 |
int status; |
| 541 |
|
| 542 |
for (i = 0; i < g->nopts; i++) |
| 543 |
if (g->opt[i].envvar != NULL && |
| 544 |
(optarg = getenv(g->opt[i].envvar)) != NULL) |
| 545 |
{ |
| 546 |
status = set_option(g, i, optarg, eslARG_SETBY_ENV, FALSE); |
| 547 |
if (status != eslOK) return status; |
| 548 |
} |
| 549 |
return eslOK; |
| 550 |
} |
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
int |
| 601 |
esl_opt_ProcessCmdline(ESL_GETOPTS *g, int argc, char **argv) |
| 602 |
{ |
| 603 |
int opti; |
| 604 |
char *optarg; |
| 605 |
int status, setstatus; |
| 606 |
|
| 607 |
g->argc = argc; |
| 608 |
g->argv = argv; |
| 609 |
g->optind = 1; |
| 610 |
g->optstring = NULL; |
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
while ((status = esl_getopts(g, &opti, &optarg)) == eslOK) |
| 617 |
{ |
| 618 |
setstatus = set_option(g, opti, optarg, eslARG_SETBY_CMDLINE, FALSE); |
| 619 |
if (setstatus != eslOK) return setstatus; |
| 620 |
} |
| 621 |
if (status == eslEOD) return eslOK; |
| 622 |
else return status; |
| 623 |
} |
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
int |
| 648 |
esl_opt_ProcessSpoof(ESL_GETOPTS *g, const char *cmdline) |
| 649 |
{ |
| 650 |
int argc = 0; |
| 651 |
char *s = NULL; |
| 652 |
void *p; |
| 653 |
char *tok; |
| 654 |
int status; |
| 655 |
|
| 656 |
if (g->spoof != NULL || g->spoof_argv != NULL) |
| 657 |
ESL_XFAIL(eslEINVAL, g->errbuf, "cannot process more than one spoofed command line"); |
| 658 |
|
| 659 |
if ((status = esl_strdup(cmdline, -1, &(g->spoof))) != eslOK) goto ERROR; |
| 660 |
s = g->spoof; |
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
while(*s != '\0'){ |
| 671 |
int status2; |
| 672 |
if(*s == '\"'){ |
| 673 |
status2 = esl_strtok(&s, "\"", &tok); |
| 674 |
} |
| 675 |
else{ |
| 676 |
status2 = esl_strtok(&s, " \t\n", &tok); |
| 677 |
} |
| 678 |
if(status2 != eslOK){ |
| 679 |
break; |
| 680 |
} |
| 681 |
argc++; |
| 682 |
ESL_RALLOC(g->spoof_argv, p, sizeof(char *) * argc); |
| 683 |
g->spoof_argv[argc-1] = tok; |
| 684 |
} |
| 685 |
status = esl_opt_ProcessCmdline(g, argc, g->spoof_argv); |
| 686 |
return status; |
| 687 |
|
| 688 |
ERROR: |
| 689 |
if (g->spoof != NULL) { free(g->spoof); g->spoof = NULL; } |
| 690 |
if (g->spoof_argv != NULL) { free(g->spoof_argv); g->spoof_argv = NULL; } |
| 691 |
return status; |
| 692 |
} |
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
int |
| 719 |
esl_opt_VerifyConfig(ESL_GETOPTS *g) |
| 720 |
{ |
| 721 |
int i,reqi,incompati; |
| 722 |
char *s; |
| 723 |
int status; |
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
for (i = 0; i < g->nopts; i++) |
| 730 |
{ |
| 731 |
if (g->setby[i] != eslARG_SETBY_DEFAULT && g->val[i] != NULL) |
| 732 |
{ |
| 733 |
s = g->opt[i].required_opts; |
| 734 |
while ((status = process_optlist(g, &s, &reqi)) != eslEOD) |
| 735 |
{ |
| 736 |
if (status != eslOK) ESL_EXCEPTION(eslEINVAL, "something's wrong with format of optlist: %s\n", s); |
| 737 |
if (g->val[reqi] == NULL) |
| 738 |
{ |
| 739 |
if (g->setby[i] >= eslARG_SETBY_CFGFILE) |
| 740 |
ESL_FAIL(eslESYNTAX, g->errbuf, "Option %.24s (set by cfg file %d) requires (or has no effect without) option(s) %.24s", |
| 741 |
g->opt[i].name, g->setby[i]-2, g->opt[i].required_opts); |
| 742 |
else if (g->setby[i] == eslARG_SETBY_ENV) |
| 743 |
ESL_FAIL(eslESYNTAX, g->errbuf, "Option %.24s (set by env var %s) requires (or has no effect without) option(s) %.24s", |
| 744 |
g->opt[i].name, g->opt[i].envvar, g->opt[i].required_opts); |
| 745 |
else |
| 746 |
ESL_FAIL(eslESYNTAX, g->errbuf, "Option %.24s requires (or has no effect without) option(s) %.24s", |
| 747 |
g->opt[i].name, g->opt[i].required_opts); |
| 748 |
} |
| 749 |
} |
| 750 |
} |
| 751 |
} |
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
for (i = 0; i < g->nopts; i++) |
| 758 |
{ |
| 759 |
if (g->setby[i] != eslARG_SETBY_DEFAULT && g->val[i] != NULL) |
| 760 |
{ |
| 761 |
s = g->opt[i].incompat_opts; |
| 762 |
while ((status = process_optlist(g, &s, &incompati)) != eslEOD) |
| 763 |
{ |
| 764 |
if (status != eslOK) ESL_EXCEPTION(eslEINVAL, "something's wrong with format of optlist: %s\n", s); |
| 765 |
if (incompati != i && (g->setby[incompati] != eslARG_SETBY_DEFAULT && g->val[incompati] != NULL)) |
| 766 |
{ |
| 767 |
if (g->setby[i] >= eslARG_SETBY_CFGFILE) |
| 768 |
ESL_FAIL(eslESYNTAX, g->errbuf, "Option %.24s (set by cfg file %d) is incompatible with option(s) %.24s", |
| 769 |
g->opt[i].name, g->setby[i]-2, g->opt[i].incompat_opts); |
| 770 |
|
| 771 |
else if (g->setby[i] == eslARG_SETBY_ENV) |
| 772 |
ESL_FAIL(eslESYNTAX, g->errbuf, "Option %.24s (set by env var %s) is incompatible with option(s) %.24s", |
| 773 |
g->opt[i].name, g->opt[i].envvar, g->opt[i].incompat_opts); |
| 774 |
else |
| 775 |
ESL_FAIL(eslESYNTAX, g->errbuf, "Option %.24s is incompatible with option(s) %.24s", |
| 776 |
g->opt[i].name, g->opt[i].incompat_opts); |
| 777 |
} |
| 778 |
} |
| 779 |
} |
| 780 |
} |
| 781 |
return eslOK; |
| 782 |
} |
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
int |
| 795 |
esl_opt_ArgNumber(const ESL_GETOPTS *g) |
| 796 |
{ |
| 797 |
return ((g)->argc - (g)->optind); |
| 798 |
} |
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
int |
| 817 |
esl_opt_SpoofCmdline(const ESL_GETOPTS *g, char **ret_cmdline) |
| 818 |
{ |
| 819 |
char *cmdline = NULL; |
| 820 |
char *p = NULL; |
| 821 |
int ntot = 0; |
| 822 |
int n; |
| 823 |
int i, j; |
| 824 |
int status; |
| 825 |
|
| 826 |
|
| 827 |
ntot = strlen(g->argv[0]) + 1; |
| 828 |
ESL_ALLOC(cmdline, sizeof(char) * (ntot+1)); |
| 829 |
snprintf(cmdline, ntot+1, "%s ", g->argv[0]); |
| 830 |
|
| 831 |
|
| 832 |
for (i = 0; i < g->nopts; i++) |
| 833 |
if (g->setby[i] != eslARG_SETBY_DEFAULT) |
| 834 |
{ |
| 835 |
if (g->opt[i].type == eslARG_NONE) n = strlen(g->opt[i].name) + 1; |
| 836 |
else n = (strlen(g->opt[i].name) + strlen(g->val[i])) + 2; |
| 837 |
|
| 838 |
ESL_RALLOC(cmdline, p, sizeof(char) * (ntot + n + 1)); |
| 839 |
|
| 840 |
if (g->opt[i].type == eslARG_NONE) snprintf(cmdline + ntot, n+1, "%s ", g->opt[i].name); |
| 841 |
else snprintf(cmdline + ntot, n+1, "%s %s ", g->opt[i].name, g->val[i]); |
| 842 |
|
| 843 |
ntot += n; |
| 844 |
} |
| 845 |
|
| 846 |
|
| 847 |
for (j = g->optind; j < g->argc; j++) |
| 848 |
{ |
| 849 |
n = strlen(g->argv[j]) + 1; |
| 850 |
ESL_RALLOC(cmdline, p, sizeof(char) * (ntot + n + 1)); |
| 851 |
snprintf(cmdline + ntot, n+1, "%s ", g->argv[j]); |
| 852 |
ntot += n; |
| 853 |
} |
| 854 |
|
| 855 |
|
| 856 |
cmdline[ntot] = '\0'; |
| 857 |
*ret_cmdline = cmdline; |
| 858 |
return eslOK; |
| 859 |
|
| 860 |
ERROR: |
| 861 |
if (cmdline != NULL) free(cmdline); |
| 862 |
*ret_cmdline = NULL; |
| 863 |
return status; |
| 864 |
} |
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
int |
| 879 |
esl_opt_IsDefault(const ESL_GETOPTS *g, char *optname) |
| 880 |
{ |
| 881 |
int opti; |
| 882 |
|
| 883 |
if (get_optidx_exactly(g, optname, &opti) != eslOK) esl_fatal("no such option %s\n", optname); |
| 884 |
if (g->setby[opti] == eslARG_SETBY_DEFAULT) return TRUE; |
| 885 |
|
| 886 |
if (g->val[opti] == NULL && g->opt[opti].defval == NULL) return TRUE; |
| 887 |
if (esl_strcmp(g->opt[opti].defval, g->val[opti]) == 0) return TRUE; |
| 888 |
return FALSE; |
| 889 |
} |
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
int |
| 911 |
esl_opt_IsOn(const ESL_GETOPTS *g, char *optname) |
| 912 |
{ |
| 913 |
int opti; |
| 914 |
|
| 915 |
if (get_optidx_exactly(g, optname, &opti) != eslOK) esl_fatal("no such option %s\n", optname); |
| 916 |
if (g->val[opti] == NULL) return FALSE; |
| 917 |
else return TRUE; |
| 918 |
} |
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
int |
| 935 |
esl_opt_IsUsed(const ESL_GETOPTS *g, char *optname) |
| 936 |
{ |
| 937 |
int opti; |
| 938 |
|
| 939 |
if (get_optidx_exactly(g, optname, &opti) != eslOK) esl_fatal("no such option %s\n", optname); |
| 940 |
if (esl_opt_IsDefault(g, optname)) return FALSE; |
| 941 |
if (g->val[opti] == NULL) return FALSE; |
| 942 |
return TRUE; |
| 943 |
} |
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
int |
| 959 |
esl_opt_GetSetter(const ESL_GETOPTS *g, char *optname) |
| 960 |
{ |
| 961 |
int opti; |
| 962 |
|
| 963 |
if (get_optidx_exactly(g, optname, &opti) != eslOK) esl_fatal("no such option %s\n", optname); |
| 964 |
return g->setby[opti]; |
| 965 |
} |
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
int |
| 975 |
esl_opt_GetBoolean(const ESL_GETOPTS *g, char *optname) |
| 976 |
{ |
| 977 |
int opti; |
| 978 |
|
| 979 |
if (get_optidx_exactly(g, optname, &opti) == eslENOTFOUND) |
| 980 |
esl_fatal("no such option %s\n", optname); |
| 981 |
if (g->opt[opti].type != eslARG_NONE) |
| 982 |
esl_fatal("option %s is not a boolean; code called _GetBoolean", optname); |
| 983 |
|
| 984 |
if (g->val[opti] == NULL) return FALSE; |
| 985 |
else return TRUE; |
| 986 |
} |
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
int |
| 995 |
esl_opt_GetInteger(const ESL_GETOPTS *g, char *optname) |
| 996 |
{ |
| 997 |
int opti; |
| 998 |
|
| 999 |
if (get_optidx_exactly(g, optname, &opti) == eslENOTFOUND) |
| 1000 |
esl_fatal("no such option %s\n", optname); |
| 1001 |
if (g->opt[opti].type != eslARG_INT) |
| 1002 |
esl_fatal("option %s does not take an integer arg; code called _GetInteger", optname); |
| 1003 |
return atoi(g->val[opti]); |
| 1004 |
} |
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
double |
| 1013 |
esl_opt_GetReal(const ESL_GETOPTS *g, char *optname) |
| 1014 |
{ |
| 1015 |
int opti; |
| 1016 |
|
| 1017 |
if (get_optidx_exactly(g, optname, &opti) == eslENOTFOUND) |
| 1018 |
esl_fatal("no such option %s\n", optname); |
| 1019 |
if (g->opt[opti].type != eslARG_REAL) |
| 1020 |
esl_fatal("option %s does not take a real-valued arg; code called _GetReal", optname); |
| 1021 |
|
| 1022 |
return (atof(g->val[opti])); |
| 1023 |
} |
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
char |
| 1032 |
esl_opt_GetChar(const ESL_GETOPTS *g, char *optname) |
| 1033 |
{ |
| 1034 |
int opti; |
| 1035 |
|
| 1036 |
if (get_optidx_exactly(g, optname, &opti) == eslENOTFOUND) |
| 1037 |
esl_fatal("no such option %s\n", optname); |
| 1038 |
if (g->opt[opti].type != eslARG_CHAR) |
| 1039 |
esl_fatal("option %s does not take a char arg; code called _GetChar", optname); |
| 1040 |
|
| 1041 |
return (*g->val[opti]); |
| 1042 |
} |
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
char * |
| 1055 |
esl_opt_GetString(const ESL_GETOPTS *g, char *optname) |
| 1056 |
{ |
| 1057 |
int opti; |
| 1058 |
|
| 1059 |
if (get_optidx_exactly(g, optname, &opti) == eslENOTFOUND) |
| 1060 |
esl_fatal("no such option %s\n", optname); |
| 1061 |
if (g->opt[opti].type != eslARG_STRING && |
| 1062 |
g->opt[opti].type != eslARG_INFILE && |
| 1063 |
g->opt[opti].type != eslARG_OUTFILE) |
| 1064 |
esl_fatal("option %s does not take a string arg; code called _GetString", optname); |
| 1065 |
|
| 1066 |
return g->val[opti]; |
| 1067 |
} |
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
char * |
| 1089 |
esl_opt_GetArg(const ESL_GETOPTS *g, int which) |
| 1090 |
{ |
| 1091 |
if (which <= 0) return NULL; |
| 1092 |
if (g->optind+which-1 >= g->argc) return NULL; |
| 1093 |
return g->argv[g->optind+which-1]; |
| 1094 |
} |
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
|
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
|
| 1123 |
|
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
int |
| 1138 |
esl_opt_DisplayHelp(FILE *ofp, const ESL_GETOPTS *go, int docgroup, int indent, |
| 1139 |
int textwidth) |
| 1140 |
{ |
| 1141 |
int optwidth = 0; |
| 1142 |
int helpwidth[3] = {0,0,0}; |
| 1143 |
int show_defaults; |
| 1144 |
int show_ranges; |
| 1145 |
int i, n; |
| 1146 |
|
| 1147 |
|
| 1148 |
|
| 1149 |
for (i = 0; i < go->nopts; i++) |
| 1150 |
if (! docgroup || docgroup == go->opt[i].docgrouptag) |
| 1151 |
{ |
| 1152 |
n = strlen(go->opt[i].name); |
| 1153 |
if (go->opt[i].type != eslARG_NONE) n += 4; |
| 1154 |
if (n > optwidth) optwidth = n; |
| 1155 |
|
| 1156 |
n = 2; |
| 1157 |
if (go->opt[i].help != NULL) |
| 1158 |
n = strlen(go->opt[i].help) + 1; |
| 1159 |
if (n > helpwidth[2]) helpwidth[2] = n; |
| 1160 |
|
| 1161 |
if (go->opt[i].defval != NULL) |
| 1162 |
n += strlen(go->opt[i].defval) + 4; |
| 1163 |
if (n > helpwidth[1]) helpwidth[1] = n; |
| 1164 |
|
| 1165 |
if (go->opt[i].range != NULL) |
| 1166 |
n += strlen(go->opt[i].range) + 4; |
| 1167 |
if (n > helpwidth[0]) helpwidth[0] = n; |
| 1168 |
} |
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
if (indent + optwidth + helpwidth[0] <= textwidth) |
| 1173 |
{ |
| 1174 |
show_defaults = TRUE; |
| 1175 |
show_ranges = TRUE; |
| 1176 |
} |
| 1177 |
else if (indent + optwidth + helpwidth[1] <= textwidth) |
| 1178 |
{ |
| 1179 |
show_defaults = TRUE; |
| 1180 |
show_ranges = FALSE; |
| 1181 |
} |
| 1182 |
else if (indent + optwidth + helpwidth[2] <= textwidth) |
| 1183 |
{ |
| 1184 |
show_defaults = FALSE; |
| 1185 |
show_ranges = FALSE; |
| 1186 |
} |
| 1187 |
else ESL_EXCEPTION(eslEINVAL, "Help line too long"); |
| 1188 |
|
| 1189 |
|
| 1190 |
for (i = 0; i < go->nopts; i++) |
| 1191 |
if (! docgroup || docgroup == go->opt[i].docgrouptag) |
| 1192 |
{ |
| 1193 |
if (fprintf(ofp, "%*s", indent, "") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed"); |
| 1194 |
n = 0; |
| 1195 |
if (fprintf(ofp, "%s", go->opt[i].name) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed"); |
| 1196 |
n += strlen(go->opt[i].name); |
| 1197 |
|
| 1198 |
switch (go->opt[i].type) { |
| 1199 |
case eslARG_NONE: break; |
| 1200 |
case eslARG_INT: if (fprintf(ofp, " <n>") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed"); n += 4; break; |
| 1201 |
case eslARG_REAL: if (fprintf(ofp, " <x>") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed"); n += 4; break; |
| 1202 |
case eslARG_CHAR: if (fprintf(ofp, " <c>") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed"); n += 4; break; |
| 1203 |
case eslARG_STRING: if (fprintf(ofp, " <s>") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed"); n += 4; break; |
| 1204 |
case eslARG_INFILE: if (fprintf(ofp, " <f>") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed"); n += 4; break; |
| 1205 |
case eslARG_OUTFILE: if (fprintf(ofp, " <f>") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed"); n += 4; break; |
| 1206 |
} |
| 1207 |
|
| 1208 |
if (fprintf(ofp, "%*s", optwidth-n, "") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed"); |
| 1209 |
if (fprintf(ofp, " :") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed"); |
| 1210 |
|
| 1211 |
if (go->opt[i].help != NULL) |
| 1212 |
{ if (fprintf(ofp, " %s", go->opt[i].help) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed"); } |
| 1213 |
|
| 1214 |
if (show_defaults && go->opt[i].defval != NULL) |
| 1215 |
if (go->opt[i].type != eslARG_CHAR || *(go->opt[i].defval) != '\0') |
| 1216 |
{ if (fprintf(ofp, " [%s]", go->opt[i].defval) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed"); } |
| 1217 |
|
| 1218 |
if (show_ranges && go->opt[i].range != NULL) |
| 1219 |
{ if (fprintf(ofp, " (%s)", go->opt[i].range) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed"); } |
| 1220 |
|
| 1221 |
if (fprintf(ofp, "\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed"); |
| 1222 |
} |
| 1223 |
return eslOK; |
| 1224 |
} |
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
|
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
|
| 1234 |
|
| 1235 |
|
| 1236 |
|
| 1237 |
|
| 1238 |
|
| 1239 |
|
| 1240 |
|
| 1241 |
|
| 1242 |
|
| 1243 |
|
| 1244 |
|
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
|
| 1252 |
|
| 1253 |
|
| 1254 |
|
| 1255 |
|
| 1256 |
int |
| 1257 |
set_option(ESL_GETOPTS *g, int opti, char *optarg, int setby, int do_alloc) |
| 1258 |
{ |
| 1259 |
int arglen; |
| 1260 |
char *where = NULL; |
| 1261 |
char *s; |
| 1262 |
int togi; |
| 1263 |
int status; |
| 1264 |
void *tmp; |
| 1265 |
|
| 1266 |
if (setby == eslARG_SETBY_DEFAULT) where = "as default"; |
| 1267 |
else if (setby == eslARG_SETBY_CMDLINE) where = "on cmdline"; |
| 1268 |
else if (setby == eslARG_SETBY_ENV) where = "in env"; |
| 1269 |
else if (setby >= eslARG_SETBY_CFGFILE) where = "in cfgfile"; |
| 1270 |
|
| 1271 |
|
| 1272 |
if (g->setby[opti] == setby) |
| 1273 |
ESL_FAIL(eslESYNTAX, g->errbuf, |
| 1274 |
"Option %.24s has already been set %s.", |
| 1275 |
g->opt[opti].name, where); |
| 1276 |
|
| 1277 |
|
| 1278 |
|
| 1279 |
if (verify_type_and_range(g, opti, optarg, setby) != eslOK) return eslESYNTAX; |
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
g->setby[opti] = setby; |
| 1285 |
if (g->opt[opti].type == eslARG_NONE) |
| 1286 |
{ |
| 1287 |
if (g->opt[opti].defval) g->val[opti] = g->opt[opti].defval; |
| 1288 |
else g->val[opti] = (char *) TRUE; |
| 1289 |
} |
| 1290 |
else |
| 1291 |
{ |
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
if (!do_alloc || optarg == NULL) |
| 1297 |
{ |
| 1298 |
if (g->valloc[opti] > 0) { free(g->val[opti]); g->valloc[opti] = 0; } |
| 1299 |
g->val[opti] = optarg; |
| 1300 |
} |
| 1301 |
|
| 1302 |
|
| 1303 |
|
| 1304 |
|
| 1305 |
else |
| 1306 |
{ |
| 1307 |
arglen = strlen(optarg); |
| 1308 |
if (g->valloc[opti] < arglen+1) |
| 1309 |
{ |
| 1310 |
if (g->valloc[opti] == 0) ESL_ALLOC (g->val[opti], sizeof(char) * (arglen+1)); |
| 1311 |
else ESL_RALLOC(g->val[opti], tmp, sizeof(char) * (arglen+1)); |
| 1312 |
g->valloc[opti] = arglen+1; |
| 1313 |
} |
| 1314 |
strcpy(g->val[opti], optarg); |
| 1315 |
} |
| 1316 |
} |
| 1317 |
|
| 1318 |
|
| 1319 |
|
| 1320 |
s = g->opt[opti].toggle_opts; |
| 1321 |
while ((status = process_optlist(g, &s, &togi)) != eslEOD) |
| 1322 |
{ |
| 1323 |
if (status != eslOK) ESL_EXCEPTION(eslEINVAL, "something's wrong with format of optlist: %s\n", s); |
| 1324 |
if (togi == opti) continue; |
| 1325 |
if (g->val[togi] == NULL) continue; |
| 1326 |
|
| 1327 |
if (g->setby[togi] == setby) |
| 1328 |
ESL_FAIL(eslESYNTAX, g->errbuf, |
| 1329 |
"Options %.24s and %.24s conflict, toggling each other.", |
| 1330 |
g->opt[togi].name, g->opt[opti].name); |
| 1331 |
|
| 1332 |
g->setby[togi] = setby; |
| 1333 |
if (g->valloc[togi] > 0) |
| 1334 |
{ free(g->val[togi]); g->valloc[togi] = 0; } |
| 1335 |
g->val[togi] = NULL; |
| 1336 |
} |
| 1337 |
return eslOK; |
| 1338 |
|
| 1339 |
ERROR: |
| 1340 |
return status; |
| 1341 |
} |
| 1342 |
|
| 1343 |
|
| 1344 |
|
| 1345 |
|
| 1346 |
|
| 1347 |
|
| 1348 |
|
| 1349 |
|
| 1350 |
|
| 1351 |
|
| 1352 |
static int |
| 1353 |
get_optidx_exactly(const ESL_GETOPTS *g, char *optname, int *ret_opti) |
| 1354 |
{ |
| 1355 |
int i; |
| 1356 |
|
| 1357 |
for (i = 0; i < g->nopts; i++) |
| 1358 |
if (strcmp(optname, g->opt[i].name) == 0) { *ret_opti = i; return eslOK; } |
| 1359 |
*ret_opti = -1; |
| 1360 |
return eslENOTFOUND; |
| 1361 |
} |
| 1362 |
|
| 1363 |
|
| 1364 |
|
| 1365 |
|
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
|
| 1376 |
static int |
| 1377 |
get_optidx_abbrev(ESL_GETOPTS *g, char *optname, int n, int *ret_opti) |
| 1378 |
{ |
| 1379 |
int nabbrev = 0; |
| 1380 |
int nexact = 0; |
| 1381 |
int i; |
| 1382 |
|
| 1383 |
if (n == 0) |
| 1384 |
n = strlen(optname); |
| 1385 |
|
| 1386 |
for (i = 0; i < g->nopts; i++) |
| 1387 |
if (strncmp(g->opt[i].name, optname, n) == 0) |
| 1388 |
{ |
| 1389 |
nabbrev++; |
| 1390 |
*ret_opti = i; |
| 1391 |
if (n == strlen(g->opt[i].name)) { nexact++; break; } |
| 1392 |
} |
| 1393 |
if (nexact != 1 && nabbrev > 1) return eslEAMBIGUOUS; |
| 1394 |
if (nabbrev == 0) return eslENOTFOUND; |
| 1395 |
return eslOK; |
| 1396 |
} |
| 1397 |
|
| 1398 |
|
| 1399 |
|
| 1400 |
|
| 1401 |
|
| 1402 |
|
| 1403 |
|
| 1404 |
|
| 1405 |
|
| 1406 |
|
| 1407 |
|
| 1408 |
|
| 1409 |
|
| 1410 |
|
| 1411 |
|
| 1412 |
|
| 1413 |
|
| 1414 |
|
| 1415 |
|
| 1416 |
|
| 1417 |
|
| 1418 |
|
| 1419 |
|
| 1420 |
static int |
| 1421 |
esl_getopts(ESL_GETOPTS *g, int *ret_opti, char **ret_optarg) |
| 1422 |
{ |
| 1423 |
*ret_optarg = NULL; |
| 1424 |
|
| 1425 |
|
| 1426 |
|
| 1427 |
|
| 1428 |
if (g->optstring == NULL && |
| 1429 |
(g->optind >= g->argc || g->argv[g->optind][0] != '-' || strcmp(g->argv[g->optind], "-") == 0)) |
| 1430 |
return eslEOD; |
| 1431 |
|
| 1432 |
|
| 1433 |
|
| 1434 |
|
| 1435 |
if (g->optstring == NULL && |
| 1436 |
strcmp(g->argv[g->optind], "--") == 0) |
| 1437 |
{ |
| 1438 |
g->optind++; |
| 1439 |
return eslEOD; |
| 1440 |
} |
| 1441 |
|
| 1442 |
|
| 1443 |
|
| 1444 |
|
| 1445 |
|
| 1446 |
|
| 1447 |
|
| 1448 |
|
| 1449 |
|
| 1450 |
|
| 1451 |
|
| 1452 |
|
| 1453 |
if (g->optstring == NULL && strncmp(g->argv[g->optind], "--", 2) == 0) |
| 1454 |
return process_longopt(g, ret_opti, ret_optarg); |
| 1455 |
else |
| 1456 |
return process_stdopt(g, ret_opti, ret_optarg); |
| 1457 |
} |
| 1458 |
|
| 1459 |
|
| 1460 |
|
| 1461 |
|
| 1462 |
|
| 1463 |
|
| 1464 |
|
| 1465 |
|
| 1466 |
|
| 1467 |
|
| 1468 |
|
| 1469 |
|
| 1470 |
|
| 1471 |
|
| 1472 |
|
| 1473 |
|
| 1474 |
|
| 1475 |
|
| 1476 |
|
| 1477 |
|
| 1478 |
|
| 1479 |
|
| 1480 |
|
| 1481 |
|
| 1482 |
|
| 1483 |
|
| 1484 |
static int |
| 1485 |
process_longopt(ESL_GETOPTS *g, int *ret_opti, char **ret_optarg) |
| 1486 |
{ |
| 1487 |
int opti; |
| 1488 |
char *argptr; |
| 1489 |
int n; |
| 1490 |
int status; |
| 1491 |
|
| 1492 |
|
| 1493 |
|
| 1494 |
if ((argptr = strchr(g->argv[g->optind], '=')) != NULL) |
| 1495 |
{ n = argptr - g->argv[g->optind]; argptr++; } |
| 1496 |
else |
| 1497 |
{ n = strlen(g->argv[g->optind]); } |
| 1498 |
|
| 1499 |
|
| 1500 |
|
| 1501 |
|
| 1502 |
|
| 1503 |
status = get_optidx_abbrev(g, g->argv[g->optind], n, &opti); |
| 1504 |
if (status == eslEAMBIGUOUS) |
| 1505 |
ESL_FAIL(eslESYNTAX, g->errbuf, "Abbreviated option \"%.24s\" is ambiguous.", g->argv[g->optind]); |
| 1506 |
else if (status == eslENOTFOUND) |
| 1507 |
ESL_FAIL(eslESYNTAX, g->errbuf, "No such option \"%.24s\".", g->argv[g->optind]); |
| 1508 |
else if (status != eslOK) |
| 1509 |
ESL_EXCEPTION(eslEINCONCEIVABLE, "Something went wrong with option \"%.24s\".", g->argv[g->optind]); |
| 1510 |
|
| 1511 |
*ret_opti = opti; |
| 1512 |
g->optind++; |
| 1513 |
|
| 1514 |
|
| 1515 |
|
| 1516 |
if (g->opt[opti].type != eslARG_NONE) |
| 1517 |
{ |
| 1518 |
if (argptr != NULL) |
| 1519 |
*ret_optarg = argptr; |
| 1520 |
else if (g->optind >= g->argc) |
| 1521 |
ESL_FAIL(eslESYNTAX, g->errbuf, "Option %.24s requires an argument.", g->opt[opti].name); |
| 1522 |
else { |
| 1523 |
*ret_optarg = g->argv[g->optind++]; |
| 1524 |
|
| 1525 |
|
| 1526 |
if ((g->opt[opti].type == eslARG_STRING || g->opt[opti].type == eslARG_INFILE || g->opt[opti].type == eslARG_OUTFILE) |
| 1527 |
&& **ret_optarg == '-') |
| 1528 |
ESL_FAIL(eslESYNTAX, g->errbuf, |
| 1529 |
"Arg looks like option? Use %.24s=%.24s if you really mean it.", |
| 1530 |
g->opt[opti].name, *ret_optarg); |
| 1531 |
} |
| 1532 |
} |
| 1533 |
else |
| 1534 |
{ |
| 1535 |
if (argptr != NULL) |
| 1536 |
ESL_FAIL(eslESYNTAX, g->errbuf, "Option %.24s does not take an argument.", g->opt[opti].name); |
| 1537 |
*ret_optarg = NULL; |
| 1538 |
} |
| 1539 |
return eslOK; |
| 1540 |
} |
| 1541 |
|
| 1542 |
|
| 1543 |
|
| 1544 |
|
| 1545 |
|
| 1546 |
|
| 1547 |
|
| 1548 |
|
| 1549 |
|
| 1550 |
|
| 1551 |
|
| 1552 |
|
| 1553 |
|
| 1554 |
|
| 1555 |
|
| 1556 |
|
| 1557 |
|
| 1558 |
|
| 1559 |
|
| 1560 |
|
| 1561 |
|
| 1562 |
|
| 1563 |
|
| 1564 |
|
| 1565 |
|
| 1566 |
|
| 1567 |
|
| 1568 |
|
| 1569 |
|
| 1570 |
|
| 1571 |
|
| 1572 |
|
| 1573 |
|
| 1574 |
|
| 1575 |
|
| 1576 |
|
| 1577 |
|
| 1578 |
|
| 1579 |
static int |
| 1580 |
process_stdopt(ESL_GETOPTS *g, int *ret_opti, char **ret_optarg) |
| 1581 |
{ |
| 1582 |
int opti; |
| 1583 |
|
| 1584 |
|
| 1585 |
|
| 1586 |
|
| 1587 |
if (g->optstring == NULL) |
| 1588 |
g->optstring = g->argv[g->optind++]+1; |
| 1589 |
|
| 1590 |
|
| 1591 |
|
| 1592 |
for (opti = 0; opti < g->nopts; opti++) |
| 1593 |
if (*(g->optstring) == g->opt[opti].name[1]) break; |
| 1594 |
if (opti == g->nopts) |
| 1595 |
ESL_FAIL(eslESYNTAX, g->errbuf, "No such option \"-%c\".", *(g->optstring)); |
| 1596 |
*ret_opti = opti; |
| 1597 |
|
| 1598 |
|
| 1599 |
if (g->opt[opti].type != eslARG_NONE) |
| 1600 |
{ |
| 1601 |
if (*(g->optstring+1) != '\0') |
| 1602 |
*ret_optarg = g->optstring+1; |
| 1603 |
else if (g->optind < g->argc) { |
| 1604 |
*ret_optarg = g->argv[g->optind++]; |
| 1605 |
|
| 1606 |
|
| 1607 |
if ((g->opt[opti].type == eslARG_STRING || g->opt[opti].type == eslARG_INFILE || g->opt[opti].type == eslARG_OUTFILE) |
| 1608 |
&& **ret_optarg == '-') |
| 1609 |
ESL_FAIL(eslESYNTAX, g->errbuf, |
| 1610 |
"Arg looks like option? Use %.24s%.24s if you really mean it.", |
| 1611 |
g->opt[opti].name, *ret_optarg); |
| 1612 |
} else |
| 1613 |
ESL_FAIL(eslESYNTAX, g->errbuf, "Option %.24s requires an argument", g->opt[opti].name); |
| 1614 |
|
| 1615 |
g->optstring = NULL; |
| 1616 |
} |
| 1617 |
else |
| 1618 |
{ |
| 1619 |
*ret_optarg = NULL; |
| 1620 |
if (*(g->optstring+1) != '\0') |
| 1621 |
g->optstring++; |
| 1622 |
else |
| 1623 |
g->optstring = NULL; |
| 1624 |
} |
| 1625 |
return eslOK; |
| 1626 |
} |
| 1627 |
|
| 1628 |
|
| 1629 |
|
| 1630 |
|
| 1631 |
|
| 1632 |
|
| 1633 |
|
| 1634 |
|
| 1635 |
|
| 1636 |
|
| 1637 |
|
| 1638 |
|
| 1639 |
|
| 1640 |
|
| 1641 |
|
| 1642 |
|
| 1643 |
|
| 1644 |
|
| 1645 |
|
| 1646 |
|
| 1647 |
|
| 1648 |
|
| 1649 |
|
| 1650 |
|
| 1651 |
|
| 1652 |
|
| 1653 |
|
| 1654 |
static int |
| 1655 |
verify_type_and_range(ESL_GETOPTS *g, int i, char *val, int setby) |
| 1656 |
{ |
| 1657 |
char *where = NULL; |
| 1658 |
|
| 1659 |
if (setby == eslARG_SETBY_DEFAULT) where = "as default"; |
| 1660 |
else if (setby == eslARG_SETBY_CMDLINE) where = "on cmdline"; |
| 1661 |
else if (setby == eslARG_SETBY_ENV) where = "in env"; |
| 1662 |
else if (setby >= eslARG_SETBY_CFGFILE) where = "in cfgfile"; |
| 1663 |
|
| 1664 |
|
| 1665 |
|
| 1666 |
|
| 1667 |
|
| 1668 |
if (setby == eslARG_SETBY_DEFAULT && val == NULL) |
| 1669 |
return eslOK; |
| 1670 |
|
| 1671 |
switch (g->opt[i].type) { |
| 1672 |
case eslARG_NONE: |
| 1673 |
|
| 1674 |
|
| 1675 |
|
| 1676 |
break; |
| 1677 |
|
| 1678 |
case eslARG_INT: |
| 1679 |
if (! esl_str_IsInteger(val)) |
| 1680 |
ESL_FAIL(eslESYNTAX, g->errbuf, |
| 1681 |
"Option %.24s takes integer arg; got %.24s %s", |
| 1682 |
g->opt[i].name, val, where); |
| 1683 |
|
| 1684 |
if (verify_integer_range(val, g->opt[i].range) != eslOK) |
| 1685 |
ESL_FAIL(eslESYNTAX, g->errbuf, |
| 1686 |
"option %.24s takes integer arg in range %.24s; got %.24s %s", |
| 1687 |
g->opt[i].name, g->opt[i].range, val, where); |
| 1688 |
break; |
| 1689 |
|
| 1690 |
case eslARG_REAL: |
| 1691 |
if (! esl_str_IsReal(val)) |
| 1692 |
ESL_FAIL(eslESYNTAX, g->errbuf, |
| 1693 |
"Option %.24s takes real-valued arg; got %.24s %s", |
| 1694 |
g->opt[i].name, val, where); |
| 1695 |
|
| 1696 |
if (verify_real_range(val, g->opt[i].range) != eslOK) |
| 1697 |
ESL_FAIL(eslESYNTAX, g->errbuf, |
| 1698 |
"Option %.24s takes real-valued arg in range %.24s; got %.24s %s", |
| 1699 |
g->opt[i].name, g->opt[i].range, val, where); |
| 1700 |
break; |
| 1701 |
|
| 1702 |
case eslARG_CHAR: |
| 1703 |
if (strlen(val) > 1) |
| 1704 |
ESL_FAIL(eslESYNTAX, g->errbuf, |
| 1705 |
"Option %.24s takes char arg; got %.24s %s", |
| 1706 |
g->opt[i].name, val, where); |
| 1707 |
|
| 1708 |
if (verify_char_range(val, g->opt[i].range) != eslOK) |
| 1709 |
ESL_FAIL(eslESYNTAX, g->errbuf, |
| 1710 |
"Option %.24s takes char arg in range %.24s; got %.24s %s", |
| 1711 |
g->opt[i].name, g->opt[i].range, val, where); |
| 1712 |
break; |
| 1713 |
|
| 1714 |
case eslARG_STRING: |
| 1715 |
case eslARG_INFILE: |
| 1716 |
case eslARG_OUTFILE: |
| 1717 |
if (g->opt[i].range != NULL) |
| 1718 |
ESL_EXCEPTION(eslEINVAL, "option %s takes a string arg that can't be range checked", g->opt[i].name); |
| 1719 |
break; |
| 1720 |
|
| 1721 |
default: |
| 1722 |
ESL_EXCEPTION(eslEINVAL, "no such argument type"); |
| 1723 |
} |
| 1724 |
|
| 1725 |
return eslOK; |
| 1726 |
} |
| 1727 |
|
| 1728 |
|
| 1729 |
|
| 1730 |
|
| 1731 |
|
| 1732 |
|
| 1733 |
|
| 1734 |
|
| 1735 |
|
| 1736 |
|
| 1737 |
|
| 1738 |
|
| 1739 |
|
| 1740 |
|
| 1741 |
|
| 1742 |
|
| 1743 |
|
| 1744 |
|
| 1745 |
|
| 1746 |
|
| 1747 |
|
| 1748 |
|
| 1749 |
|
| 1750 |
|
| 1751 |
|
| 1752 |
static int |
| 1753 |
verify_integer_range(char *arg, char *range) |
| 1754 |
{ |
| 1755 |
int n; |
| 1756 |
int upper, lower; |
| 1757 |
char *up, *lp; |
| 1758 |
int geq, leq; |
| 1759 |
|
| 1760 |
if (range == NULL) return eslOK; |
| 1761 |
n = atoi(arg); |
| 1762 |
|
| 1763 |
if (parse_rangestring(range, 'n', &lp, &geq, &up, &leq) != eslOK) return eslEINVAL; |
| 1764 |
|
| 1765 |
if (lp != NULL) { |
| 1766 |
lower = atoi(lp); |
| 1767 |
if ((geq && ! (n >= lower)) || (!geq && !(n > lower))) return eslERANGE; |
| 1768 |
} |
| 1769 |
|
| 1770 |
if (up != NULL) { |
| 1771 |
upper = atoi(up); |
| 1772 |
if ((leq && ! (n <= upper)) || (!leq && !(n < upper))) return eslERANGE; |
| 1773 |
} |
| 1774 |
return eslOK; |
| 1775 |
} |
| 1776 |
|
| 1777 |
|
| 1778 |
|
| 1779 |
|
| 1780 |
|
| 1781 |
|
| 1782 |
|
| 1783 |
|
| 1784 |
|
| 1785 |
|
| 1786 |
|
| 1787 |
|
| 1788 |
|
| 1789 |
|
| 1790 |
static int |
| 1791 |
verify_real_range(char *arg, char *range) |
| 1792 |
{ |
| 1793 |
double x; |
| 1794 |
double upper, lower; |
| 1795 |
char *up, *lp; |
| 1796 |
int geq, leq; |
| 1797 |
|
| 1798 |
if (range == NULL) return eslOK; |
| 1799 |
x = atof(arg); |
| 1800 |
|
| 1801 |
if (parse_rangestring(range, 'x', &lp, &geq, &up, &leq) != eslOK) |
| 1802 |
return eslEINVAL; |
| 1803 |
|
| 1804 |
if (lp != NULL) |
| 1805 |
{ |
| 1806 |
lower = atof(lp); |
| 1807 |
if ((geq && ! (x >= lower)) || (!geq && !(x > lower))) |
| 1808 |
return eslERANGE; |
| 1809 |
} |
| 1810 |
|
| 1811 |
if (up != NULL) |
| 1812 |
{ |
| 1813 |
upper = atof(up); |
| 1814 |
if ((leq && ! (x <= upper)) || (!leq && !(x < upper))) |
| 1815 |
return eslERANGE; |
| 1816 |
} |
| 1817 |
return eslOK; |
| 1818 |
} |
| 1819 |
|
| 1820 |
|
| 1821 |
|
| 1822 |
|
| 1823 |
|
| 1824 |
|
| 1825 |
|
| 1826 |
|
| 1827 |
|
| 1828 |
|
| 1829 |
|
| 1830 |
|
| 1831 |
|
| 1832 |
|
| 1833 |
|
| 1834 |
|
| 1835 |
|
| 1836 |
static int |
| 1837 |
verify_char_range(char *arg, char *range) |
| 1838 |
{ |
| 1839 |
char c; |
| 1840 |
char *upper, *lower; |
| 1841 |
int geq, leq; |
| 1842 |
|
| 1843 |
if (range == NULL) return eslOK; |
| 1844 |
c = *arg; |
| 1845 |
|
| 1846 |
if (parse_rangestring(range, 'c', &lower, &geq, &upper, &leq) != eslOK) |
| 1847 |
return eslEINVAL; |
| 1848 |
|
| 1849 |
if (lower != NULL) |
| 1850 |
{ |
| 1851 |
if ((geq && ! (c >= *lower)) || (!geq && !(c > *lower))) |
| 1852 |
return eslERANGE; |
| 1853 |
} |
| 1854 |
|
| 1855 |
if (upper != NULL) |
| 1856 |
{ |
| 1857 |
if ((leq && ! (c <= *upper)) || (!leq && !(c < *upper))) |
| 1858 |
return eslERANGE; |
| 1859 |
} |
| 1860 |
return eslOK; |
| 1861 |
} |
| 1862 |
|
| 1863 |
|
| 1864 |
|
| 1865 |
|
| 1866 |
|
| 1867 |
|
| 1868 |
|
| 1869 |
|
| 1870 |
|
| 1871 |
|
| 1872 |
|
| 1873 |
|
| 1874 |
|
| 1875 |
|
| 1876 |
|
| 1877 |
|
| 1878 |
|
| 1879 |
|
| 1880 |
|
| 1881 |
|
| 1882 |
|
| 1883 |
static int |
| 1884 |
parse_rangestring(char *range, char c, char **ret_lowerp, int *ret_geq, char **ret_upperp, int *ret_leq) |
| 1885 |
{ |
| 1886 |
char *ptr; |
| 1887 |
|
| 1888 |
*ret_geq = *ret_leq = FALSE; |
| 1889 |
*ret_lowerp = *ret_upperp = NULL; |
| 1890 |
|
| 1891 |
if ((ptr = strchr(range, c)) == NULL) return eslEINVAL; |
| 1892 |
if (ptr == range) |
| 1893 |
{ |
| 1894 |
if (ptr[1] == '>') |
| 1895 |
{ |
| 1896 |
if (ptr[2] == '=') { *ret_geq = TRUE; *ret_lowerp = ptr+3; } |
| 1897 |
else *ret_lowerp = ptr+2; |
| 1898 |
} |
| 1899 |
else if (ptr[1] == '<') |
| 1900 |
{ |
| 1901 |
if (ptr[2] == '=') { *ret_leq = TRUE; *ret_upperp = ptr+3; } |
| 1902 |
else *ret_upperp = ptr+2; |
| 1903 |
} |
| 1904 |
else return eslEINVAL; |
| 1905 |
} |
| 1906 |
else |
| 1907 |
{ |
| 1908 |
if (*(ptr+1) != '<') return eslEINVAL; |
| 1909 |
if (*(ptr+2) == '=') { *ret_leq = TRUE; *ret_upperp = ptr+3; } else *ret_upperp = ptr+2; |
| 1910 |
|
| 1911 |
ptr--; |
| 1912 |
if (*ptr == '=') { *ret_geq = TRUE; ptr--; } |
| 1913 |
if (*ptr != '<') return eslEINVAL; |
| 1914 |
*ret_lowerp = range; |
| 1915 |
} |
| 1916 |
return eslOK; |
| 1917 |
} |
| 1918 |
|
| 1919 |
|
| 1920 |
|
| 1921 |
|
| 1922 |
|
| 1923 |
|
| 1924 |
|
| 1925 |
|
| 1926 |
|
| 1927 |
|
| 1928 |
|
| 1929 |
|
| 1930 |
|
| 1931 |
|
| 1932 |
|
| 1933 |
|
| 1934 |
|
| 1935 |
|
| 1936 |
|
| 1937 |
|
| 1938 |
|
| 1939 |
|
| 1940 |
|
| 1941 |
|
| 1942 |
|
| 1943 |
|
| 1944 |
|
| 1945 |
static int |
| 1946 |
process_optlist(ESL_GETOPTS *g, char **ret_s, int *ret_opti) |
| 1947 |
{ |
| 1948 |
char *s; |
| 1949 |
int i; |
| 1950 |
int n; |
| 1951 |
|
| 1952 |
if ((s = *ret_s) == NULL) return eslEOD; |
| 1953 |
if (*s == '\0') return eslEOD; |
| 1954 |
|
| 1955 |
n = strcspn(s, ","); |
| 1956 |
|
| 1957 |
|
| 1958 |
|
| 1959 |
|
| 1960 |
|
| 1961 |
|
| 1962 |
for (i = 0; i < g->nopts; i++) |
| 1963 |
if (strncmp(g->opt[i].name, s, n) == 0) break; |
| 1964 |
if (i == g->nopts) return eslEINVAL; |
| 1965 |
|
| 1966 |
*ret_opti = i; |
| 1967 |
if (s[n] == ',') *ret_s = s+n+1; |
| 1968 |
else *ret_s = NULL; |
| 1969 |
|
| 1970 |
return eslOK; |
| 1971 |
} |
| 1972 |
|
| 1973 |
|
| 1974 |
|
| 1975 |
|
| 1976 |
|
| 1977 |
|
| 1978 |
|
| 1979 |
|
| 1980 |
|
| 1981 |
#ifdef eslGETOPTS_TESTDRIVE |
| 1982 |
|
| 1983 |
|
| 1984 |
#include <stdlib.h> |
| 1985 |
#include <stdio.h> |
| 1986 |
|
| 1987 |
#include "easel.h" |
| 1988 |
#include "esl_getopts.h" |
| 1989 |
|
| 1990 |
|
| 1991 |
#define BGROUP "-b,--no-b" |
| 1992 |
static ESL_OPTIONS options[] = { |
| 1993 |
|
| 1994 |
{ "-a", eslARG_NONE, FALSE,"FOOTEST",NULL, NULL, NULL, NULL, "toggle a on", 1 }, |
| 1995 |
{ "-b", eslARG_NONE, FALSE, NULL, NULL, BGROUP, NULL, NULL, "toggle b on", 1 }, |
| 1996 |
{ "--no-b", eslARG_NONE,"TRUE", NULL, NULL, BGROUP, NULL, NULL, "toggle b off", 1 }, |
| 1997 |
{ "-c", eslARG_CHAR, "x", NULL,"a<=c<=z",NULL, NULL, NULL, "character arg", 2 }, |
| 1998 |
{ "--d1", eslARG_NONE,"TRUE", NULL, NULL, "--d2", NULL, NULL, "toggle d1 on, d2 off", 2 }, |
| 1999 |
{ "--d2", eslARG_NONE, FALSE, NULL, NULL, "--d1", NULL, NULL, "toggle d2 on, d1 off", 2 }, |
| 2000 |
{ "-n", eslARG_INT, "0", NULL,"0<=n<10",NULL, NULL, NULL, "integer arg", 2 }, |
| 2001 |
{ "-x", eslARG_REAL, "0.8", NULL, "0<x<1", NULL, NULL, NULL, "real-value arg", 2 }, |
| 2002 |
{ "--lowx", eslARG_REAL, "1.0", NULL, "x>0", NULL, NULL, NULL, "real arg with lower bound", 2 }, |
| 2003 |
{ "--hix", eslARG_REAL, "0.9", NULL, "x<1", NULL, NULL, NULL, "real arg with upper bound", 2 }, |
| 2004 |
{ "--lown", eslARG_INT, "42", NULL, "n>0", NULL,"-a,-b", NULL, "int arg with lower bound", 2 }, |
| 2005 |
{ "--hin", eslARG_INT, "-1", NULL, "n<0", NULL, NULL,"--no-b","int arg with upper bound", 2 }, |
| 2006 |
{ "--host", eslARG_STRING, "","HOSTTEST",NULL, NULL, NULL, NULL, "string arg with env var", 3 }, |
| 2007 |
{ "--multi",eslARG_STRING,NULL, NULL, NULL, NULL, NULL, NULL, "test quoted configfile arg",3 }, |
| 2008 |
{ "--mul", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "test long opt abbreviation",3 }, |
| 2009 |
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 2010 |
}; |
| 2011 |
|
| 2012 |
|
| 2013 |
int |
| 2014 |
main(void) |
| 2015 |
{ |
| 2016 |
ESL_GETOPTS *go, *go2; |
| 2017 |
char file1[32] = "esltmpXXXXXX"; |
| 2018 |
char file2[32] = "esltmpXXXXXX"; |
| 2019 |
char *errmsg = "getopts unit test failure"; |
| 2020 |
FILE *f1, *f2; |
| 2021 |
int i; |
| 2022 |
|
| 2023 |
|
| 2024 |
int argc = 11; |
| 2025 |
char *argv[] = { "progname", "-bc", "y", "--d1", "-n9", "--hix=0.0", "--lown", "43", "--mul", "arg1", "2005"}; |
| 2026 |
|
| 2027 |
|
| 2028 |
if (esl_tmpfile_named(file1, &f1) != eslOK) esl_fatal("failed to create named tmpfile 1"); |
| 2029 |
fprintf(f1, "# Test config file #1\n"); |
| 2030 |
fprintf(f1, "#\n"); |
| 2031 |
fprintf(f1, "-b\n"); |
| 2032 |
fprintf(f1, "--d2\n"); |
| 2033 |
fprintf(f1, "-n 3\n"); |
| 2034 |
fprintf(f1, "-x 0.5\n"); |
| 2035 |
fprintf(f1, "--multi \"one two three\"\n"); |
| 2036 |
fclose(f1); |
| 2037 |
|
| 2038 |
|
| 2039 |
if (esl_tmpfile_named(file2, &f2) != eslOK) esl_fatal("failed to create named tmpfile 2"); |
| 2040 |
fprintf(f2, "# Test config file #2\n"); |
| 2041 |
fprintf(f2, "#\n"); |
| 2042 |
fprintf(f2, "--no-b\n"); |
| 2043 |
fprintf(f2, "--hin -33\n"); |
| 2044 |
fprintf(f2, "--host www.nytimes.com\n"); |
| 2045 |
fclose(f2); |
| 2046 |
|
| 2047 |
|
| 2048 |
|
| 2049 |
|
| 2050 |
setenv("FOOTEST", "", 1); |
| 2051 |
setenv("HOSTTEST", "wasp.cryptogenomicon.org", 1); |
| 2052 |
|
| 2053 |
|
| 2054 |
|
| 2055 |
if ((f1 = fopen(file1, "r")) == NULL) esl_fatal("getopts fopen() 1 failed"); |
| 2056 |
if ((f2 = fopen(file2, "r")) == NULL) esl_fatal("getopts fopen() 2 failed"); |
| 2057 |
|
| 2058 |
go = esl_getopts_Create(options); |
| 2059 |
if (esl_opt_ProcessConfigfile(go, file1, f1) != eslOK) esl_fatal("getopts failed to process config file 1\n%s\n", go->errbuf); |
| 2060 |
if (esl_opt_ProcessConfigfile(go, file2, f2) != eslOK) esl_fatal("getopts failed to process config file 2\n%s\n", go->errbuf); |
| 2061 |
if (esl_opt_ProcessEnvironment(go) != eslOK) esl_fatal("getopts failed to process environment:\n%s\n", go->errbuf); |
| 2062 |
if (esl_opt_ProcessCmdline(go, argc, argv) != eslOK) esl_fatal("getopts failed to process command line:\n%s\n", go->errbuf); |
| 2063 |
if (esl_opt_VerifyConfig(go) != eslOK) esl_fatal("getopts config fails validation:\n%s\n", go->errbuf); |
| 2064 |
|
| 2065 |
fclose(f1); |
| 2066 |
fclose(f2); |
| 2067 |
|
| 2068 |
if (esl_opt_GetBoolean(go, "-a") != TRUE) esl_fatal("getopts failed on -a"); |
| 2069 |
if (esl_opt_GetBoolean(go, "-b") != TRUE) esl_fatal("getopts failed on -b"); |
| 2070 |
if (esl_opt_GetBoolean(go, "--no-b") != FALSE) esl_fatal("getopts failed on --no-b"); |
| 2071 |
if (esl_opt_GetChar (go, "-c") != 'y') esl_fatal("getopts failed on -c"); |
| 2072 |
if (esl_opt_GetInteger(go, "-n") != 9) esl_fatal("getopts failed on -n"); |
| 2073 |
if (esl_opt_GetReal (go, "-x") != 0.5) esl_fatal("getopts failed on -x"); |
| 2074 |
if (esl_opt_GetReal (go, "--lowx") != 1.0) esl_fatal("getopts failed on --lowx"); |
| 2075 |
if (esl_opt_GetReal (go, "--hix") != 0.0) esl_fatal("getopts failed on --hix"); |
| 2076 |
if (esl_opt_GetInteger(go, "--lown") != 43) esl_fatal("getopts failed on --lown"); |
| 2077 |
if (esl_opt_GetInteger(go, "--hin") != -33) esl_fatal("getopts failed on --hin"); |
| 2078 |
if (esl_opt_GetBoolean(go, "--mul") != TRUE) esl_fatal("getopts failed on --mul"); |
| 2079 |
if (strcmp(esl_opt_GetString(go, "--host"), "wasp.cryptogenomicon.org") != 0) |
| 2080 |
esl_fatal("getopts failed on --host"); |
| 2081 |
if (strcmp(esl_opt_GetString(go, "--multi"), "one two three") != 0) |
| 2082 |
esl_fatal("config file didn't handle quoted argument"); |
| 2083 |
|
| 2084 |
|
| 2085 |
|
| 2086 |
|
| 2087 |
|
| 2088 |
|
| 2089 |
|
| 2090 |
|
| 2091 |
|
| 2092 |
|
| 2093 |
|
| 2094 |
|
| 2095 |
|
| 2096 |
|
| 2097 |
|
| 2098 |
if (! esl_opt_IsDefault(go, "--d1")) esl_fatal(errmsg); |
| 2099 |
if (! esl_opt_IsOn (go, "--d1")) esl_fatal(errmsg); |
| 2100 |
if ( esl_opt_IsUsed (go, "--d1")) esl_fatal(errmsg); |
| 2101 |
if (! esl_opt_IsDefault(go, "--d2")) esl_fatal(errmsg); |
| 2102 |
if ( esl_opt_IsOn (go, "--d2")) esl_fatal(errmsg); |
| 2103 |
if ( esl_opt_IsUsed (go, "--d2")) esl_fatal(errmsg); |
| 2104 |
if ( esl_opt_IsDefault(go, "-a")) esl_fatal(errmsg); |
| 2105 |
if (! esl_opt_IsOn (go, "-a")) esl_fatal(errmsg); |
| 2106 |
if (! esl_opt_IsUsed (go, "-a")) esl_fatal(errmsg); |
| 2107 |
if ( esl_opt_IsDefault(go, "-b")) esl_fatal(errmsg); |
| 2108 |
if (! esl_opt_IsOn (go, "-b")) esl_fatal(errmsg); |
| 2109 |
if (! esl_opt_IsUsed (go, "-b")) esl_fatal(errmsg); |
| 2110 |
if ( esl_opt_IsDefault(go, "--no-b")) esl_fatal(errmsg); |
| 2111 |
if ( esl_opt_IsOn (go, "--no-b")) esl_fatal(errmsg); |
| 2112 |
if ( esl_opt_IsUsed (go, "--no-b")) esl_fatal(errmsg); |
| 2113 |
|
| 2114 |
for (i = 0; i < go->nopts; i++) |
| 2115 |
{ |
| 2116 |
if ( esl_opt_IsDefault(go, go->opt[i].name) && esl_opt_IsOn(go, go->opt[i].name) && esl_opt_IsUsed(go, go->opt[i].name)) esl_fatal(errmsg); |
| 2117 |
if ( esl_opt_IsDefault(go, go->opt[i].name) && ! esl_opt_IsOn(go, go->opt[i].name) && esl_opt_IsUsed(go, go->opt[i].name)) esl_fatal(errmsg); |
| 2118 |
if (! esl_opt_IsDefault(go, go->opt[i].name) && esl_opt_IsOn(go, go->opt[i].name) && ! esl_opt_IsUsed(go, go->opt[i].name)) esl_fatal(errmsg); |
| 2119 |
if (! esl_opt_IsDefault(go, go->opt[i].name) && ! esl_opt_IsOn(go, go->opt[i].name) && esl_opt_IsUsed(go, go->opt[i].name)) esl_fatal(errmsg); |
| 2120 |
} |
| 2121 |
|
| 2122 |
|
| 2123 |
char *cmdline = esl_getopts_CreateOptsLine(go); |
| 2124 |
#if 0 |
| 2125 |
int mismatch; |
| 2126 |
if (mismatch = strcmp(cmdline, " -a -b -c y --d1 -n 9 -x 0.5 --hix 0.0 --lown 43 --hin -33 --host \"wasp.cryptogenomicon.org\" --multi \"one two three\" --mul")) |
| 2127 |
esl_fatal("esl_getopts_CreateOptsLine test failed at position %d", mismatch); |
| 2128 |
#endif |
| 2129 |
char *spoofline; |
| 2130 |
spoofline = (char *) malloc(strlen(cmdline)+ 8); |
| 2131 |
if(spoofline == NULL) esl_fatal(errmsg); |
| 2132 |
strcpy(spoofline, "getopts"); |
| 2133 |
strcat(spoofline, cmdline); |
| 2134 |
|
| 2135 |
|
| 2136 |
go2 = esl_getopts_Create(options); |
| 2137 |
if (esl_opt_ProcessSpoof(go2, spoofline) != eslOK) esl_fatal("errmsg"); |
| 2138 |
if (esl_opt_VerifyConfig(go2) != eslOK) esl_fatal("errmsg"); |
| 2139 |
|
| 2140 |
|
| 2141 |
if (esl_opt_GetBoolean(go2, "-a") != TRUE) esl_fatal("getopts failed on -a"); |
| 2142 |
if (esl_opt_GetBoolean(go2, "-b") != TRUE) esl_fatal("getopts failed on -b"); |
| 2143 |
if (esl_opt_GetBoolean(go2, "--no-b") != FALSE) esl_fatal("getopts failed on --no-b"); |
| 2144 |
if (esl_opt_GetChar (go2, "-c") != 'y') esl_fatal("getopts failed on -c"); |
| 2145 |
if (esl_opt_GetInteger(go2, "-n") != 9) esl_fatal("getopts failed on -n"); |
| 2146 |
if (esl_opt_GetReal (go2, "-x") != 0.5) esl_fatal("getopts failed on -x"); |
| 2147 |
if (esl_opt_GetReal (go2, "--lowx") != 1.0) esl_fatal("getopts failed on --lowx"); |
| 2148 |
if (esl_opt_GetReal (go2, "--hix") != 0.0) esl_fatal("getopts failed on --hix"); |
| 2149 |
if (esl_opt_GetInteger(go2, "--lown") != 43) esl_fatal("getopts failed on --lown"); |
| 2150 |
if (esl_opt_GetInteger(go2, "--hin") != -33) esl_fatal("getopts failed on --hin"); |
| 2151 |
if (esl_opt_GetBoolean(go2, "--mul") != TRUE) esl_fatal("getopts failed on --mul"); |
| 2152 |
if (strcmp(esl_opt_GetString(go2, "--host"), "wasp.cryptogenomicon.org") != 0) |
| 2153 |
esl_fatal("getopts failed on --host"); |
| 2154 |
if (strcmp(esl_opt_GetString(go2, "--multi"), "one two three") != 0) |
| 2155 |
esl_fatal("config file didn't handle quoted argument"); |
| 2156 |
if (! esl_opt_IsDefault(go2, "--d1")) esl_fatal(errmsg); |
| 2157 |
if (! esl_opt_IsOn (go2, "--d1")) esl_fatal(errmsg); |
| 2158 |
if ( esl_opt_IsUsed (go2, "--d1")) esl_fatal(errmsg); |
| 2159 |
if (! esl_opt_IsDefault(go2, "--d2")) esl_fatal(errmsg); |
| 2160 |
if ( esl_opt_IsOn (go2, "--d2")) esl_fatal(errmsg); |
| 2161 |
if ( esl_opt_IsUsed (go2, "--d2")) esl_fatal(errmsg); |
| 2162 |
if ( esl_opt_IsDefault(go2, "-a")) esl_fatal(errmsg); |
| 2163 |
if (! esl_opt_IsOn (go2, "-a")) esl_fatal(errmsg); |
| 2164 |
if (! esl_opt_IsUsed (go2, "-a")) esl_fatal(errmsg); |
| 2165 |
if ( esl_opt_IsDefault(go2, "-b")) esl_fatal(errmsg); |
| 2166 |
if (! esl_opt_IsOn (go2, "-b")) esl_fatal(errmsg); |
| 2167 |
if (! esl_opt_IsUsed (go2, "-b")) esl_fatal(errmsg); |
| 2168 |
if ( esl_opt_IsDefault(go2, "--no-b")) esl_fatal(errmsg); |
| 2169 |
if ( esl_opt_IsOn (go2, "--no-b")) esl_fatal(errmsg); |
| 2170 |
if ( esl_opt_IsUsed (go2, "--no-b")) esl_fatal(errmsg); |
| 2171 |
|
| 2172 |
for (i = 0; i < go->nopts; i++) |
| 2173 |
{ |
| 2174 |
if ( esl_opt_IsDefault(go2, go2->opt[i].name) && esl_opt_IsOn(go2, go2->opt[i].name) && esl_opt_IsUsed(go2, go2->opt[i].name)) esl_fatal(errmsg); |
| 2175 |
if ( esl_opt_IsDefault(go2, go2->opt[i].name) && ! esl_opt_IsOn(go2, go2->opt[i].name) && esl_opt_IsUsed(go2, go2->opt[i].name)) esl_fatal(errmsg); |
| 2176 |
if (! esl_opt_IsDefault(go2, go2->opt[i].name) && esl_opt_IsOn(go2, go2->opt[i].name) && ! esl_opt_IsUsed(go2, go2->opt[i].name)) esl_fatal(errmsg); |
| 2177 |
if (! esl_opt_IsDefault(go2, go2->opt[i].name) && ! esl_opt_IsOn(go2, go2->opt[i].name) && esl_opt_IsUsed(go2, go2->opt[i].name)) esl_fatal(errmsg); |
| 2178 |
} |
| 2179 |
free(cmdline); |
| 2180 |
free(spoofline); |
| 2181 |
|
| 2182 |
|
| 2183 |
if (esl_opt_ArgNumber(go) != 2) esl_fatal("getopts failed with wrong arg number"); |
| 2184 |
|
| 2185 |
if (strcmp("arg1", esl_opt_GetArg(go, 1)) != 0) esl_fatal("getopts failed on argument 1"); |
| 2186 |
if (strcmp("2005", esl_opt_GetArg(go, 2)) != 0) esl_fatal("getopts failed on argument 2"); |
| 2187 |
|
| 2188 |
esl_getopts_Destroy(go); |
| 2189 |
esl_getopts_Destroy(go2); |
| 2190 |
remove(file1); |
| 2191 |
remove(file2); |
| 2192 |
exit(0); |
| 2193 |
} |
| 2194 |
|
| 2195 |
#endif |
| 2196 |
|
| 2197 |
|
| 2198 |
|
| 2199 |
|
| 2200 |
|
| 2201 |
|
| 2202 |
|
| 2203 |
|
| 2204 |
|
| 2205 |
|
| 2206 |
|
| 2207 |
#ifdef eslGETOPTS_EXAMPLE |
| 2208 |
|
| 2209 |
#include <stdio.h> |
| 2210 |
#include "easel.h" |
| 2211 |
#include "esl_getopts.h" |
| 2212 |
|
| 2213 |
static ESL_OPTIONS options[] = { |
| 2214 |
|
| 2215 |
{ "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show help and usage", 0}, |
| 2216 |
{ "-a", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "a boolean switch", 0}, |
| 2217 |
{ "-b", eslARG_NONE,"default", NULL, NULL, NULL, NULL, NULL, "another boolean switch", 0}, |
| 2218 |
{ "-n", eslARG_INT, "0", NULL, NULL, NULL, NULL, NULL, "an integer argument", 0}, |
| 2219 |
{ "-s", eslARG_STRING, "hi!", NULL, NULL, NULL, NULL, NULL, "a string argument", 0}, |
| 2220 |
{ "-x", eslARG_REAL, "1.0", NULL, NULL, NULL, NULL, NULL, "a real-valued argument", 0}, |
| 2221 |
{ "--file", eslARG_STRING, NULL, NULL, NULL, NULL, NULL, NULL, "long option, filename arg", 0}, |
| 2222 |
{ "--char", eslARG_CHAR, "", NULL, NULL, NULL, NULL, NULL, "long option, char arg", 0}, |
| 2223 |
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 2224 |
}; |
| 2225 |
static char usage[] = "Usage: ./example [-options] <arg>"; |
| 2226 |
|
| 2227 |
int |
| 2228 |
main(int argc, char **argv) |
| 2229 |
{ |
| 2230 |
ESL_GETOPTS *go; |
| 2231 |
char *arg; |
| 2232 |
|
| 2233 |
if ((go = esl_getopts_Create(options)) == NULL) esl_fatal("Bad options structure\n"); |
| 2234 |
if (esl_opt_ProcessCmdline(go, argc, argv) != eslOK) esl_fatal("Failed to parse command line: %s\n", go->errbuf); |
| 2235 |
if (esl_opt_VerifyConfig(go) != eslOK) esl_fatal("Failed to parse command line: %s\n", go->errbuf); |
| 2236 |
|
| 2237 |
if (esl_opt_GetBoolean(go, "-h") == TRUE) { |
| 2238 |
printf("%s\n where options are:", usage); |
| 2239 |
esl_opt_DisplayHelp(stdout, go, 0, 2, 80); |
| 2240 |
return 0; |
| 2241 |
} |
| 2242 |
|
| 2243 |
if (esl_opt_ArgNumber(go) != 1) esl_fatal("Incorrect number of command line arguments.\n%s\n", usage); |
| 2244 |
arg = esl_opt_GetArg(go, 1); |
| 2245 |
|
| 2246 |
printf("Option -a: %s\n", esl_opt_GetBoolean(go, "-a") ? "on" : "off"); |
| 2247 |
printf("Option -b: %s\n", esl_opt_GetBoolean(go, "-b") ? "on" : "off"); |
| 2248 |
printf("Option -n: %d\n", esl_opt_GetInteger(go, "-n")); |
| 2249 |
printf("Option -s: %s\n", esl_opt_GetString( go, "-s")); |
| 2250 |
printf("Option -x: %f\n", esl_opt_GetReal( go, "-x")); |
| 2251 |
if (esl_opt_IsUsed(go, "--file")) printf("Option --file: %s\n", esl_opt_GetString(go, "--file")); |
| 2252 |
else printf("Option --file: (not set)\n"); |
| 2253 |
printf("Option --char: %c\n", esl_opt_GetChar(go, "--char")); |
| 2254 |
printf("Cmdline arg: %s\n", arg); |
| 2255 |
|
| 2256 |
esl_getopts_Destroy(go); |
| 2257 |
return 0; |
| 2258 |
} |
| 2259 |
|
| 2260 |
#endif |
| 2261 |
|
| 2262 |
|
| 2263 |
|
| 2264 |
|
| 2265 |
|
| 2266 |
|
| 2267 |
#ifdef eslGETOPTS_EXAMPLE2 |
| 2268 |
|
| 2269 |
#include <stdio.h> |
| 2270 |
#include "easel.h" |
| 2271 |
#include "esl_getopts.h" |
| 2272 |
|
| 2273 |
static ESL_OPTIONS options[] = { |
| 2274 |
|
| 2275 |
{ "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show help and usage", 0}, |
| 2276 |
{ "-a", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "a boolean switch", 0}, |
| 2277 |
{ "-n", eslARG_INT, "42",NULL, NULL, NULL, NULL, NULL, "an integer", 0}, |
| 2278 |
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 2279 |
}; |
| 2280 |
static char banner[] = "example of using simplest getopts creation"; |
| 2281 |
static char usage[] = "[-options] <arg1> <arg2>"; |
| 2282 |
|
| 2283 |
int |
| 2284 |
main(int argc, char **argv) |
| 2285 |
{ |
| 2286 |
ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 2, argc, argv, banner, usage); |
| 2287 |
char *arg1 = esl_opt_GetArg(go, 1); |
| 2288 |
char *arg2 = esl_opt_GetArg(go, 2); |
| 2289 |
int a = esl_opt_GetBoolean(go, "-a"); |
| 2290 |
int n = esl_opt_GetInteger(go, "-n"); |
| 2291 |
|
| 2292 |
printf("arg 1: %s\n", arg1); |
| 2293 |
printf("arg 2: %s\n", arg2); |
| 2294 |
printf("option a: %s\n", (a ? "true" : "false")); |
| 2295 |
printf("option n: %d\n", n); |
| 2296 |
|
| 2297 |
esl_getopts_Destroy(go); |
| 2298 |
return 0; |
| 2299 |
} |
| 2300 |
|
| 2301 |
#endif |
| 2302 |
|
| 2303 |
|
| 2304 |
|
| 2305 |
|
| 2306 |
|