Product SiteDocumentation Site

Running a popt example

Pulling this all together, you can use the popt1.c program, in Listing 16-3, as an example for using popt to process command-line options.
Listing 16-3: popt1.c
/* Processes command-line options. */
#include <stdio.h>
#include <stdlib.h>
#include <popt.h>
/* Data values for the options. */
static int intVal = 55;
static int print = 0;
static char* stringVal;
void callback(poptContext context,
enum poptCallbackReason reason,
const struct poptOption * option,
const char * arg,
const void * data)
{
switch(reason)
{
case POPT_CALLBACK_REASON_PRE:
printf("\t Callback in pre setting\n"); break;
case POPT_CALLBACK_REASON_POST:
printf("\t Callback in post setting\n"); break;
case POPT_CALLBACK_REASON_OPTION:
printf("\t Callback in option setting\n"); break;
}
}
/* Set up a table of options. */
static struct poptOption optionsTable[] = {
{ (const) "int", (char) 'i', POPT_ARG_INT, (void*) &intVal, 0,
(const) "follow with an integer value", (const) "2, 4, 8, or 16" },
{ "callback", '\0', POPT_ARG_CALLBACK|POPT_ARGFLAG_DOC_HIDDEN,
&callback, 0, NULL, NULL },
{ (const) "file", (char) 'f', POPT_ARG_STRING, (void*) &stringVal, 0,
(const) "follow with a file name", NULL },
{ (const) "print", (char) 'p', POPT_ARG_NONE, &print, 0,
(const) "send output to the printer", NULL },
POPT_AUTOALIAS
POPT_AUTOHELP
POPT_TABLEEND
};
int main(int argc, char *argv[]) {
poptContext context = poptGetContext(
(const char*) "popt1",
argc,
argv,
(const struct poptOption* ) &optionsTable,
0);
int option = poptGetNextOpt(context);
printf("option = %d\n", option);
/* Print out option values. */
printf("After processing, options have values:\n");
printf("\t intVal holds %d\n", intVal);
printf("\t print flag holds %d\n", print);
printf("\t stringVal holds [%s]\n", stringVal);
poptFreeContext(context);
exit(0);
}
This example defines a callback but otherwise uses the simplest case for processing the command-line options. This program lets the popt library simply set the values into the option table. In most cases, you should avoid more complex command-line processing.
To compile popt programs, you just need the popt library. For example:
gcc -I/usr/include/rpm -o popt1 popt1.c -lpopt
When you run this program, try out the different options. For example, when you set all the options, you’ll see output like the following:
$ ./popt1 -i 42 --print -f filename1
Callback in option setting
Callback in option setting
Callback in post setting
option = -1
After processing, options have values:
intVal holds 42
print flag holds 1
stringVal holds [filename1]
This command used two short options and one long. You can mix and match short and long options, as shown following:
$ ./popt1 --int 42 -p --file filename1
Callback in option setting
Callback in option setting
Callback in post setting
option = -1
After processing, options have values:
intVal holds 42
print flag holds 1
stringVal holds [filename1]
This example used a short option for print, -p, and long options for the other two options. The popt library also provides handy help and usage messages, using the option table macro POPT_AUTOALIAS. To get a help message, use --help or -?:
$ ./popt1 --help
Usage: popt1 [OPTION...]
-i, --int=2, 4, 8, or 16 follow with an integer value
-f, --file=STRING follow with a file name
-p, --print send output to the printer
Options implemented via popt alias/exec:
Help options:
-?, --help Show this help message
--usage Display brief usage message
Notice how the help descriptions from the options table are used.
Note
With some shells, especially the tcsh shell, you need to wrap a -? In single quotes. For example:
$ ./popt1 '-?'
The usage message is shorter, and you also get it for free:
$ ./popt1 --usage
Usage: popt1 [-i|--int 2, 4, 8, or 16] [-f|--file STRING] [-p|--print]
[-?|--help] [--usage]
All in all, the popt library provides a handy library for processing command-line options and aliases, covered in Chapter 20, Customizing RPM Behavior.