Section 26.4. Error Handling


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:


An option that requires an argument was specified on the command line, but no argument was given. This can be returned only by poptGetNextOpt().


An option was specified in argv but is not in the option table. This error can be returned only from poptGetNextOpt().


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.


A parsed string has a quotation mismatch (such as a single quotation mark). poptParseArgvString(), poptReadConfigFile(), or poptReadDefaultConfig() can return this error.


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.


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.


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 Errors




An argument is missing for an option.


An option's argument could not be parsed.


Option aliasing is nested too deeply.


Quotations do not match.


An option could not be converted to a number.


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)); 


    Linux Application Development
    Linux Application Development (paperback) (2nd Edition)
    ISBN: 0321563220
    EAN: 2147483647
    Year: 2003
    Pages: 168

    Similar book on Amazon © 2008-2017.
    If you may any questions please contact us: