26.4. Error Handling All of the popt functions that can return errors return integers. When an error occurs, a negative error code is returned. Table 26.2 summarizes the error codes that occur. Here is a more detailed discussion of each error: POPT_ERROR_NOARG An option that requires an argument was specified on the command line, but no argument was given. This can be returned only by poptGetNextOpt(). POPT_ERROR_BADOPT An option was specified in argv but is not in the option table. This error can be returned only from poptGetNextOpt(). POPT_ERROR_OPTSTOODEEP A set of option aliases is nested too deeply. Currently, popt follows options only 10 levels deep to prevent infinite recursion. Only poptGetNextOpt() can return this error. POPT_ERROR_BADQUOTE A parsed string has a quotation mismatch (such as a single quotation mark). poptParseArgvString(), poptReadConfigFile(), or poptReadDefaultConfig() can return this error. POPT_ERROR_BADNUMBER A conversion from a string to a number (int or long) failed due to the string's containing nonnumeric characters. This occurs when poptGetNextOpt() processes an argument of type POPT_ARG_INT or POPT_ARG_LONG. POPT_ERROR_OVERFLOW A string-to-number conversion failed because the number was too large or too small. Like POPT_ERROR_BADNUMBER, this error can occur only when poptGetNextOpt() processes an argument of type POPT_ARG_INT or POPT_ARG_LONG. POPT_ERROR_ERRNO A system call returned with an error, and errno still contains the error from the system call. Both poptReadConfigFile() and poptReadDefaultConfig() can return this error. Table 26.2. popt ErrorsError | Description |
---|
POPT_ERROR_NOARG | An argument is missing for an option. | POPT_ERROR_BADOPT | An option's argument could not be parsed. | POPT_ERROR_OPTSTOODEEP | Option aliasing is nested too deeply. | POPT_ERROR_BADQUOTE | Quotations do not match. | POPT_ERROR_BADNUMBER | An option could not be converted to a number. | POPT_ERROR_OVERFLOW | A given number was too big or too small. |
Two functions are available to make it easy for applications to provide good error messages. const char * poptStrerror(const int error); This function takes a popt error code and returns a string describing the error, just as with the standard strerror() function. char * poptBadOption(poptContext con, int flags); If an error occurred during poptGetNextOpt(), this function returns the option that caused the error. If the flags argument is set to POPT_BADOPTION_NOALIAS, the outermost option is returned. Otherwise, flags should be zero, and the option that is returned may have been specified through an a lias. These two functions make popt error handling trivial for most applications. When an error is detected from most of the functions, an error message is printed along with the error string from poptStrerror(). When an error occurs during argument parsing, code similar to the following displays a useful error message: fprintf(stderr, "%s: %s\n", poptBadOption(optCon, POPT_BADOPTION_NOALIAS), poptStrerror(rc)); |