Product SiteDocumentation Site

15.2.2.6. Walking Through the Command-Line Options

In normal circumstances, poptGetNextOpt parses all the options and returns –1. If your needs are simple, you can use the pointers to the variables passed in the options table, described previously. If you need some special processing for options not handled by popt, that is, options of type POPT_ARG_NONE, then poptGetNextOpt returns the single-character option.
In this case, you can call poptGetNextOpt in a while loop. For example:
while ((option = poptGetNextOpt(context) ) {
/* Do something... */
}
Inside your while loop, you can call poptGetOptArg to get the value of the argument:
char * poptGetOptArg(poptContext context);
You can restart the processing of the options by calling poptResetContext:
void poptResetContext(poptContext context);
The popt system is just looking for arguments that start with a dash, -. In most command-line applications, you may have a number of extra arguments at the end, such as a list of file names. The popt library doesn’t process these, but can provide them to you.
Call poptGetArg to return the next extra argument:
char * poptGetArg(poptContext context);
Keep calling this function until it returns NULL.
Call poptPeekArg to look at the next argument but not mark it as being processed:
char * poptPeekArg(poptContext context);
Or, you can get the whole list of extra arguments by calling poptGetArgs:
char ** poptGetArgs(poptContext context);