1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 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 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 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 536 537 538 539 540 541 542 543 544 545 546 547 548 549 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 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
|
//--------------------------------------------------------------------
// Free GetOpt 1.0
//
// Copyright 2000 by Christopher J. Madsen
//
// Process command line arguments
//
// Free GetOpt is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of
// the License, or (at your option) any later version.
//
// Free GetOpt is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// As a special exception, if you link Free GetOpt with other files to
// produce an executable, this does not by itself cause the resulting
// executable to be covered by the GNU General Public License. Your
// use of that executable is in no way restricted on account of linking
// the Free GetOpt code into it. However, if you link a modified
// version of Free GetOpt to your executable and distribute the
// executable, you must make your modifications to Free GetOpt publicly
// available as machine-readable source code.
//
// This exception does not however invalidate any other reasons why
// the executable file might be covered by the GNU General Public License.
//
// This exception applies only to the code released under the name
// Free GetOpt. If you copy code from other programs into a copy of
// Free GetOpt, as the General Public License permits, the exception
// does not apply to the code that you add in this way. To avoid
// misleading anyone as to the status of such modified files, you must
// delete this exception notice from them.
//
// If you write modifications of your own for Free GetOpt, it is your
// choice whether to permit this exception to apply to your modifications.
// If you do not wish that, delete this exception notice.
//--------------------------------------------------------------------
#ifndef GETOPT_NO_STDIO
#include <stdio.h>
#endif
#include <stdlib.h>
#include <string.h>
#include "GetOpt.hpp"
static const char longOptionStart[] = "--";
//====================================================================
// Standard argument callback functions:
//
// An argument callback function is called when GetOpt finds the
// option that specified it. The usual behavior is to validate the
// argument and copy it to the location specified by option->data.
// However, the function can do anything it wants. It should return
// true if it found an argument, or false if it did not.
//
// To report an error, the callback should set getopt->error to true,
// or call getopt->reportError, which does that automatically. It
// should then return false.
//
// Input:
// getopt:
// The GetOpt object which is calling the function
// option:
// The GetOpt::Option which we are processing
// asEntered:
// The option as the user entered it
// connected:
// The way the argument (if any) was connected to the option:
// nextArg: The next command-line argument (or no argument)
// withEquals: Connected to the option by an equals sign
// adjacent: Adjacent to the option (single-char option only)
// argument:
// The argument to the option (if any)
// May be NULL, which means there was no argument.
// This is because some callbacks may not care about the
// argument, but may want to do something just because the
// option was found. connected will be nextArg in this case.
// If connected is withEquals, then argument[-1] is the equals sign.
// usedChars:
// Most callback functions can ignore this parameter. Use it only
// if you want to implement the following behavior. If usedChars
// is not NULL, then we are processing an argument that was
// adjacent to a single-character option. If the function would
// like to use only some of the characters in argument and allow
// the rest to be processed as more single-character options, it
// should set *usedChars to the number of characters used.
// *usedChars is always initialized to -1, which means that the
// entire argument was used.
//
// Return Value:
// true: The argument was processed
// false: The argument was not used, or an error occurred
//
// Note:
// The standard callbacks insist that an optional argument must be
// connected to the option (ie, they return false if connected is
// nextArg and the argument was not required). You can change this
// behavior by using your own callbacks instead of the standard ones.
//
//--------------------------------------------------------------------
// Process a floating-point argument:
//
// option->data must point to a double.
bool GetOpt::isFloat(GetOpt* getopt, const Option* option,
const char* asEntered,
Connection connected, const char* argument,
int* usedChars)
{
if (!argument ||
((connected == nextArg) && !(option->flag & GetOpt::needArg)))
return false; // No argument or non-connected optional argument
char* end;
*reinterpret_cast<double*>(option->data) = strtod(argument, &end);
if (*end) {
getopt->reportError(asEntered, " requires a numeric argument");
return false;
}
return true;
} // end GetOpt::isFloat
//--------------------------------------------------------------------
// Process an integer argument:
//
// option->data must point to a long.
bool GetOpt::isLong(GetOpt* getopt, const Option* option,
const char* asEntered,
Connection connected, const char* argument,
int* usedChars)
{
if (!argument ||
((connected == nextArg) && !(option->flag & GetOpt::needArg)))
return false; // No argument or non-connected optional argument
char* end;
*reinterpret_cast<long*>(option->data) = strtol(argument, &end, 0);
if (*end) {
getopt->reportError(asEntered, " requires an integer argument");
return false;
}
return true;
} // end GetOpt::isLong
//--------------------------------------------------------------------
// Process a string argument:
//
// option->data must point to a const char*.
bool GetOpt::isString(GetOpt* getopt, const Option* option,
const char* asEntered,
Connection connected, const char* argument,
int* usedChars)
{
if (!argument ||
((connected == nextArg) && !(option->flag & GetOpt::needArg)))
return false; // No argument or non-connected optional argument
if (option->data)
*reinterpret_cast<const char**>(option->data) = argument;
return true;
} // end GetOpt::isString
//====================================================================
// Class GetOpt:
//
// Member Variables:
// error:
// true if an error has occurred during option processing
// false if everything is ok
// errorOutput:
// A function which is called to display errors
// Set to GetOpt::printError by the GetOpt constructor.
// If NULL, errors are reported only by setting error to true.
// optionStart:
// A string containing the characters that indicate options
// Set to "-" by the GetOpt constructor.
// Must contain '-' if you expect long options to work.
// This applies only to single-character options; "--" is always
// the long option indicator.
// Programs that want to accept DOS-style options should set this
// to "-/". Note that this string is not disposed of by the
// GetOpt object. It must continue to exist as long as the GetOpt
// object does. (Normally, you would set it to point to a string
// literal.)
//
// Protected Member Variables:
// optionList:
// The array of GetOpt::Option objects passed to the constructor
// argc, argv:
// The parameters passed to main (or similar)
// argi:
// The index in argv of the argument currently being processed
// chari:
// If non-zero, the index in argv[argi] of the option character
// currently being processed (for single-character option bundles).
// normalOnly:
// True means that all arguments yet to be processed are not options.
// returningAll:
// Points to the GetOpt::Option that corresponds to normal
// non-option arguments, or NULL if GetOpt is to process only
// options.
// shortOptionBuf:
// Used when processing single-character option bundles
//
//--------------------------------------------------------------------
// Constructor:
//
// Input:
// aList:
// An array of GetOpt::Option objects that define the options to
// look for. This array is not copied, and must continue to exist
// as long as the GetOpt object does.
GetOpt::GetOpt(const Option* aList)
: error(false),
#ifdef GETOPT_NO_STDIO
errorOutput(NULL), // No stdio, can't print errors
#else
errorOutput(GetOpt::printError), // Print error messages to stderr
#endif
optionStart("-"),
optionList(aList),
argc(0),
argi(0), chari(0),
argv(NULL),
normalOnly(false)
{
checkReturnAll();
} // end GetOpt::GetOpt
//--------------------------------------------------------------------
// Standard callback function for printing error messages:
//
// You should generally not call this function directly. Use
// GetOpt::reportError to report errors, which calls the errorOutput
// function.
//
// The GetOpt constructor sets errorOutput to GetOpt::printError.
//
// Input:
// option: The option the user typed
// message: The error message to display
#ifndef GETOPT_NO_STDIO
void GetOpt::printError(const char* option, const char* message)
{
fputs(option, stderr);
fputs(message, stderr);
putc('\n', stderr);
} // end GetOpt::printError
#endif // not GETOPT_NO_STDIO
//--------------------------------------------------------------------
// Prepare to process a command line:
//
// This function also goes through the option list and sets all found
// entries to notFound.
//
// Input:
// theArgc:
// The number of elements in theArgv
// theArgv:
// The program name & command line arguments.
// theArgv[0] (the program name) is not used and may be NULL.
// This array is not copied, and must exist as long as the GetOpt
// object is in use.
void GetOpt::init(int theArgc, const char** theArgv)
{
argc = theArgc;
argv = theArgv;
argi = chari = 0;
error = normalOnly = false;
const Option* op = optionList;
while (op->shortName || op->longName) {
if (op->found)
*(op->found) = notFound;
++op;
}
} // end GetOpt::init
//--------------------------------------------------------------------
// Set the returningAll member variable:
void GetOpt::checkReturnAll()
{
const Option* op = optionList;
while (op->shortName || op->longName) {
if (op->longName && !*(op->longName)) {
returningAll = op;
return;
}
++op;
}
returningAll = NULL;
} // end GetOpt::checkReturnAll
//--------------------------------------------------------------------
// Determine what Option a long option refers to:
//
// Looks first for an exact match, then for an approximate one.
//
// Input:
// option:
// The option the user typed (without the leading "--", but with
// any trailing argument attached by an '=')
//
// Returns:
// A pointer to the corresponding GetOpt::Option
// NULL if no option matched
// If more than one option might match, calls reportError and then
// returns NULL.
const GetOpt::Option* GetOpt::findLongOption(const char* option)
{
const Option* op = optionList;
const Option* possibleMatch = NULL;
bool ambiguous = false;
while (op->shortName || op->longName) {
if (op->longName) {
bool partial = false;
const char* u = option;
const char* o = op->longName;
for (;;) {
if (!*u || *u == '=') { // Reached end of user entry
if (*o || partial) {
if (possibleMatch) ambiguous = true; // 2 possible matches
possibleMatch = op;
break; // Found possible match, keep going
} else return op; // Exact match!
} else if (!*o) {
break; // Not a match
} else if (*u == *o) {
++u; ++o;
} else if (*u == '-') {
partial = true;
while (*(++o))
if (*o == '-') break;
} else
break; // Not a match
} // end forever
} // end if option has a longName
++op;
} // end while more options
// We didn't find an exact match, what about a possible one?
if (possibleMatch) {
if (ambiguous) { // More than one possible match found
reportError(argv[argi], " is ambiguous");
return NULL;
}
return possibleMatch; // Found just one possible match
} // end if possibleMatch
return NULL; // Found no matches at all
} // end GetOpt::findLongOption
//--------------------------------------------------------------------
// Determine what Option a short option refers to:
//
// Input:
// option: The option to look for
//
// Returns:
// A pointer to the corresponding GetOpt::Option
// NULL if no option matched
const GetOpt::Option* GetOpt::findShortOption(char option) const
{
const Option* op = optionList;
while (op->shortName || op->longName) {
if (op->shortName == option)
return op;
++op;
}
return NULL;
} // end GetOpt::findShortOption
//--------------------------------------------------------------------
// Find the next argument to process:
//
// If returningAll is not NULL, this returns all arguments in order.
// Otherwise, the options (and their arguments) are moved before the
// non-option arguments, but this function still returns all arguments.
//
// Output:
// option: Points to the option (after any option start characters)
// type: The type of option found
// optArg: Normal argument (not an option)
// optLong: A long option
// optShort: A single-character option
// posArg: The index of a possible argument for this option
// 0 means use argi+1
//
// Returns:
// true: Found an option or normal argument to be returned
// false: No more arguments (option & type are undefined in this case)
bool GetOpt::nextOption(const char*& option, Type& type, int& posArg)
{
posArg = 0;
if (chari) {
if (argv[argi][++chari]) {
option = argv[argi] + chari;
type = optShort;
return true;
}
chari = 0; // We've reached the end of a short option bundle
} // end if processing a short option bundle
if (++argi >= argc) return false; // No more options
const char* arg = argv[argi];
if (!normalOnly) {
if (*arg && strchr(optionStart, *arg)) {
foundOptionStart:
if (!strncmp(longOptionStart, arg, sizeof(longOptionStart)-1)) {
option = arg+2;
type = optLong;
return true;
}
if (arg[1]) {
chari = 1;
option = arg+1;
type = optShort;
return true;
}
} // end if arg begins with option start character
if (!returningAll) { // Look for another option argument
for (int i = argi+1; i < argc; ++i) {
if (argv[i][0] && strchr(optionStart, argv[i][0])) {
// We found another option, move it before the other args:
posArg = i + 1;
arg = argv[i];
while (--i >= argi)
argv[i+1] = argv[i];
argv[argi] = arg;
goto foundOptionStart;
} // end if we found another option argument
} // end for remaining arguments
normalOnly = true; // There are no more option arguments
} // end if not returning all arguments in order
} // end if still looking for options
option = arg;
type = optArg;
return true;
} // end GetOpt::nextOption
//--------------------------------------------------------------------
// Return the next argument to process:
//
// If returningAll is not NULL, this returns all arguments in order.
// Otherwise, it returns only the options, which are moved before the
// non-option arguments, and returns false when it runs out of options.
//
// Output:
// option: Points to the GetOpt::Option selected by the user
// asEntered: The actual text the user typed
//
// Returns:
// true: Found an option or normal argument to be returned
// false: No more arguments match the option list
bool GetOpt::nextOption(const Option*& option, const char*& asEntered)
{
const char* arg;
Type type;
int posArg;
nextArg:
if (!nextOption(arg, type, posArg)) return false;
if (type == optLong && !*arg) {
normalOnly = true;
goto nextArg;
} // end if "--" by itself (no more options)
if (type == optShort) {
shortOptionBuf[0] = argv[argi][0];
shortOptionBuf[1] = *arg;
shortOptionBuf[2] = 0;
asEntered = shortOptionBuf;
option = findShortOption(*arg);
} else {
asEntered = argv[argi];
if (type == optArg)
option = returningAll;
else
option = findLongOption(arg);
}
if (!option) {
if ((type != optArg) && !error)
reportError(asEntered, " is not a recognized option");
return false;
}
if (option->found && *option->found &&
!(option->flag & GetOpt::repeatable)) {
reportError(asEntered, " cannot be repeated");
return false;
}
Found found = noArg;
if (option->function) {
int usedChars = -1;
int* mayUseChars = NULL;
Connection connect = nextArg;
if (!posArg) posArg = argi + 1;
if (type != optArg) {
if ((type == optShort) && argv[argi][chari+1]) {
mayUseChars = &usedChars;
arg = argv[argi] + chari + 1;
if (*arg == '=') {
++arg;
connect = withEquals;
} else
connect = adjacent;
} else if ((type == optLong) && (arg = strchr(arg, '='))) {
++arg; // Skip over equals
connect = withEquals;
} else if (posArg < argc)
arg = argv[posArg];
else if (option->flag & GetOpt::needArg) {
reportError(asEntered, " requires an argument");
return false;
} else
arg = NULL;
} // end if option (not normal argument)
if ((*(option->function))(this, option, asEntered, connect, arg,
mayUseChars)) {
found = withArg;
if (usedChars >= 0)
chari += usedChars;
else {
chari = 0;
if ((type != optArg) && (connect == nextArg) && arg) {
++argi;
// If we moved the option, we need to move the argument:
while (--posArg >= argi)
argv[posArg+1] = argv[posArg];
argv[argi] = arg;
} // end if used next argument
} // end else didn't use just some of the characters in a bundle
} // end if found option
else if ((option->flag & GetOpt::needArg) && !error) {
reportError(asEntered, " requires an argument");
return false;
}
} // end if option has function to call
if (option->found)
*(option->found) = found;
return true;
} // end GetOpt::nextOption
//--------------------------------------------------------------------
// Report (and possibly print) an error:
//
// This sets error to true and then calls the errorOutput function (if
// that is not NULL). The errorOutput function (normally printError)
// is expected to print both its arguments (see printError).
//
// Your callback functios should call reportError to report any errors
// they encounter. If printing error messages to stderr is not
// appropriate for your application, then set errorOutput to a
// suitable function, or to NULL to suppress error messages
// altogether.
//
// Input:
// option: The option that caused a problem
// message: The problem that was encountered
void GetOpt::reportError(const char* option, const char* message)
{
error = true;
if (errorOutput)
(*errorOutput)(option, message);
} // end GetOpt::reportError
//--------------------------------------------------------------------
// Process a command line:
//
// This is the standard entry point for GetOpt. Most programs will
// just call the constructor to set up the option list and then call
// process, relying on callbacks to store the results.
//
// Input:
// theArgc:
// The number of elements in theArgv
// theArgv:
// The program name & command line arguments.
// theArgv[0] (the program name) is not used and may be NULL.
// This array is not copied, and must exist as long as the GetOpt
// object is in use.
//
// Returns:
// The index (into theArgv) of the first argument that was not
// processed by GetOpt. If this is >= theArgc, then all arguments
// were processed. (This is the same value that would be returned
// by currentArg().)
int GetOpt::process(int theArgc, const char** theArgv)
{
const Option* option;
const char* asEntered;
init(theArgc, theArgv);
while (nextOption(option, asEntered))
;
return argi;
} // end GetOpt::process
|