1POPT(3)                    Linux Programmer's Manual                   POPT(3)
2
3
4

NAME

6       popt - Parse command line options
7

SYNOPSIS

9       #include <popt.h>
10
11       poptContext poptGetContext(const char * name, int argc,
12                                  const char ** argv,
13                                  const struct poptOption * options,
14                                  int flags);
15
16       void poptFreeContext(poptContext con);
17
18       void poptResetContext(poptContext con);
19
20       int poptGetNextOpt(poptContext con);
21
22       const char * poptGetOptArg(poptContext con);
23
24       const char * poptGetArg(poptContext con);
25
26       const char * poptPeekArg(poptContext con);
27
28       const char ** poptGetArgs(poptContext con);
29
30       const char *const poptStrerror(const int error);
31
32       const char * poptBadOption(poptContext con, int flags);
33
34       int poptReadDefaultConfig(poptContext con, int flags);
35
36       int poptReadConfigFile(poptContext con, char * fn);
37
38       int poptAddAlias(poptContext con, struct poptAlias alias,
39                        int flags);
40
41       int poptParseArgvString(char * s, int *  argcPtr,
42                               const char *** argvPtr);
43
44       int poptDupArgv(int argc, const char ** argv, int * argcPtr,
45                               const char *** argvPtr);
46
47       int poptStuffArgs(poptContext con, const char ** argv);
48
49

DESCRIPTION

51       The  popt  library exists essentially for parsing command-line options.
52       It is found superior in many ways when compared  to  parsing  the  argv
53       array  by hand or using the getopt functions getopt() and getopt_long()
54       [see getopt(3)].  Some specific advantages of popt  are:  it  does  not
55       utilize global variables, thus enabling multiple passes in parsing argv
56       ; it can parse an arbitrary  array  of  argv-style  elements,  allowing
57       parsing of command-line-strings from any source; it provides a standard
58       method of option aliasing (to be discussed at length  below.);  it  can
59       exec external option filters; and, finally, it can automatically gener‐
60       ate help and usage messages for the application.
61
62       Like getopt_long(), the popt library  supports  short  and  long  style
63       options.  Recall that a short option consists of a - character followed
64       by a single alphanumeric character.  A long option, common in GNU util‐
65       ities,  consists  of  two  - characters followed by a string made up of
66       letters, numbers and hyphens.  Long options are optionally  allowed  to
67       begin  with  a  single -, primarily to allow command-line compatibility
68       between popt applications and X toolkit applications.  Either  type  of
69       option  may  be  followed  by  an  argument.  A space separates a short
70       option from its arguments; either a space or  an  =  separates  a  long
71       option from an argument.
72
73       The  popt library is highly portable and should work on any POSIX plat‐
74       form.  The latest version is distributed with rpm and is always  avail‐
75       able from: ftp://ftp.rpm.org/pub/rpm/dist.
76
77       It  may  be  redistributed under the X consortium license, see the file
78       COPYING in the popt source distribution for details.
79

BASIC POPT USAGE

81   1. THE OPTION TABLE
82       Applications  provide  popt  with  information  on  their  command-line
83       options by means of an "option table," i.e., an array of struct poptOp‐
84       tion structures:
85
86       #include <popt.h>
87
88       struct poptOption {
89           const char * longName; /* may be NULL */
90           char shortName;        /* may be '\0' */
91           int argInfo;
92           void * arg;            /* depends on argInfo */
93           int val;               /* 0 means don't return, just update flag */
94           char * descrip;        /* description for autohelp -- may be NULL */
95           char * argDescrip;     /* argument description for autohelp */
96       };
97
98       Each member of the table defines a single option that may be passed  to
99       the  program.   Long  and  short options are considered a single option
100       that may occur in two different forms.  The first two members, longName
101       and  shortName,  define  the  names  of the option; the first is a long
102       name, while the latter is a single character.
103
104       The argInfo member tells popt what type of argument is  expected  after
105       the  argument.  If no option is expected, POPT_ARG_NONE should be used.
106       The rest of the valid values are shown in the following table:
107
108
109       Value             Description                        arg Type
110       POPT_ARG_NONE     No argument expected               int
111       POPT_ARG_STRING   No type checking to be performed   char *
112       POPT_ARG_INT      An integer argument is expected    int
113       POPT_ARG_LONG     A long integer is expected         long
114       POPT_ARG_VAL      Integer value taken from val       int
115       POPT_ARG_FLOAT    An float argument is expected      float
116       POPT_ARG_DOUBLE   A double argument is expected      double
117
118       For numeric values, if the argInfo value is bitwise or'd  with  one  of
119       POPT_ARGFLAG_OR,  POPT_ARGFLAG_AND,  or  POPT_ARGFLAG_XOR, the value is
120       saved by performing an OR, AND, or XOR.  If the argInfo value  is  bit‐
121       wise  or'd with POPT_ARGFLAG_NOT, the value will be negated before sav‐
122       ing. For  the  common  operations  of  setting  and/or  clearing  bits,
123       POPT_BIT_SET and POPT_BIT_CLR have the appropriate flags set to perform
124       bit operations.
125
126       If the argInfo value is bitwise  or'd  with  POPT_ARGFLAG_ONEDASH,  the
127       long argument may be given with a single - instead of two. For example,
128       if --longopt is an  option  with  POPT_ARGFLAG_ONEDASH,  is  specified,
129       -longopt is accepted as well.
130
131       The  next  element,  arg,  allows  popt to automatically update program
132       variables when the option is used. If arg is NULL, it  is  ignored  and
133       popt  takes no special action.  Otherwise it should point to a variable
134       of the type indicated in the right-most column of the table above.
135
136       If the option takes no argument (argInfo is POPT_ARG_NONE),  the  vari‐
137       able  pointed to by arg is set to 1 when the option is used.  (Inciden‐
138       tally, it will perhaps not escape the attention of  hunt-and-peck  typ‐
139       ists that the value of POPT_ARG_NONE is 0.)  If the option does take an
140       argument, the variable that arg points to is  updated  to  reflect  the
141       value  of  the  argument.  Any string is acceptable for POPT_ARG_STRING
142       arguments,  but  POPT_ARG_INT,   POPT_ARG_LONG,   POPT_ARG_FLOAT,   and
143       POPT_ARG_DOUBLE  are  converted  to  the appropriate type, and an error
144       returned if the conversion fails.
145
146       POPT_ARG_VAL causes arg to be set to the (integer) value  of  val  when
147       the  argument  is found.  This is most often useful for mutually-exclu‐
148       sive arguments in cases where it is not an error for multiple arguments
149       to  occur  and  where  you want the last argument specified to win; for
150       example, "rm -i -f".  POPT_ARG_VAL causes the parsing function  not  to
151       return a value, since the value of val has already been used.
152
153       If  the  argInfo  value is bitwise or'd with POPT_ARGFLAG_OPTIONAL, the
154       argument to the long option may be omitted. If the long option is  used
155       without  an argument, a default value of zero or NULL will be saved (if
156       the arg pointer is present), otherwise behavior will be identical to  a
157       long option with argument.
158
159       The  next  option,  val,  is  the  value popt's parsing function should
160       return when the option is encountered.  If it is 0, the  parsing  func‐
161       tion  does  not  return  a value, instead parsing the next command-line
162       argument.
163
164       The last two options, descrip and argDescrip are only required if auto‐
165       matic help messages are desired (automatic usage messages can be gener‐
166       ated without them). descrip is a text description of the  argument  and
167       argdescrip  is  a  short  summary  of  the type of arguments the option
168       expects, or NULL if the option doesn't require any arguments.
169
170       If popt should automatically provide --usage and --help (-?)   options,
171       one  line  in  the table should be the macro POPT_AUTOHELP.  This macro
172       includes another option table (via POPT_ARG_INCLUDE_TABLE;  see  below)
173       in  the  main one which provides the table entries for these arguments.
174       When --usage or --help are passed to programs which use popt's automat‐
175       ical  help,  popt displays the appropriate message on stderr as soon as
176       it finds the option, and exits the program with a return code of 0.  If
177       you  want  to  use popt's automatic help generation in a different way,
178       you need to explicitly add the option entries to your  programs  option
179       table instead of using POPT_AUTOHELP.
180
181       If  the argInfo value is bitwise or'd with POPT_ARGFLAG_DOC_HIDDEN, the
182       argument will not be shown in help output.
183
184       If the argInfo value is bitwise  or'd  with  POPT_ARGFLAG_SHOW_DEFAULT,
185       the inital value of the arg will be shown in help output.
186
187       The final structure in the table should have all the pointer values set
188       to NULL and all the arithmetic values set to 0, marking the end of  the
189       table. The macro POPT_TABLEEND is provided to do that.
190
191       There  are  two types of option table entries which do not specify com‐
192       mand line options. When either of these types of entries are used,  the
193       longName element must be NULL and the shortName element must be '\0'.
194
195       The  first  of these special entry types allows the application to nest
196       another option table in the current one; such nesting may extend  quite
197       deeply  (the actual depth is limited by the program's stack). Including
198       other option tables allows a library to provide a standard set of  com‐
199       mand-line options to every program which uses it (this is often done in
200       graphical programming toolkits, for  example).  To  do  this,  set  the
201       argInfo  field  to POPT_ARG_INCLUDE_TABLE and the arg field to point to
202       the table which is being included.  If  automatic  help  generation  is
203       being  used,  the descrip field should contain a overall description of
204       the option table being included.
205
206       The other special option table entry type tells popt to call a function
207       (a callback) when any option in that table is found. This is especially
208       usefull when included option tables are  being  used,  as  the  program
209       which  provides  the top-level option table doesn't need to be aware of
210       the other options which are provided by  the  included  table.  When  a
211       callback  is set for a table, the parsing function never returns infor‐
212       mation on an option in the table. Instead, options information must  be
213       retained  via the callback or by having popt set a variable through the
214       option's arg field.  Option callbacks should match the following proto‐
215       type:
216
217       void poptCallbackType(poptContext con,
218                             const struct poptOption * opt,
219                             const char * arg, void * data);
220
221       The  first parameter is the context which is being parsed (see the next
222       section for information on contexts), opt points to  the  option  which
223       triggered  this  callback,  and  arg  is the option's argument.  If the
224       option does not take an argument, arg is NULL.   The  final  parameter,
225       data  is  taken  from the descrip field of the option table entry which
226       defined the callback. As descrip is a  pointer,  this  allows  callback
227       functions to be passed an arbitrary set of data (though a typecast will
228       have to be used).
229
230       The option table entry which defines  a  callback  has  an  argInfo  of
231       POPT_ARG_CALLBACK,  an arg which points to the callback function, and a
232       descrip field which specifies an arbitrary pointer to be passed to  the
233       callback.
234
235   2. CREATING A CONTEXT
236       popt  can  interleave  the  parsing  of  multiple command-line sets. It
237       allows this by keeping all the state information for a  particular  set
238       of  command-line  arguments  in a poptContext data structure, an opaque
239       type that should not be modified outside the popt library.
240
241       New popt contexts are created by poptGetContext():
242
243       poptContext poptGetContext(const char * name, int argc,
244                                  const char ** argv,
245                                  const struct poptOption * options,
246                                  int flags);
247
248       The first parameter, name, is used only for alias  handling  (discussed
249       later).  It  should  be  the  name of the application whose options are
250       being parsed, or should be NULL if no option aliasing is  desired.  The
251       next  two  arguments specify the command-line arguments to parse. These
252       are generally passed to poptGetContext() exactly as they were passed to
253       the  program's main() function. The options parameter points to the ta‐
254       ble of command-line options, which was described in the  previous  sec‐
255       tion. The final parameter, flags, can take one of three values:
256
257       Value                        Description
258       POPT_CONTEXT_NO_EXEC         Ignore exec expansions
259       POPT_CONTEXT_KEEP_FIRST      Do not ignore argv[0]
260       POPT_CONTEXT_POSIXMEHARDER   Options cannot follow arguments
261
262       A poptContext keeps track of which options have already been parsed and
263       which remain, among other things. If a program wishes to restart option
264       processing of a set of arguments, it can reset the poptContext by pass‐
265       ing the context as the sole argument to poptResetContext().
266
267       When argument processing is complete, the process should free the popt‐
268       Context  as  it  contains  dynamically  allocated components. The popt‐
269       FreeContext() function takes a poptContext as  its  sole  argument  and
270       frees the resources the context is using.
271
272       Here  are  the  prototypes  of both poptResetContext() and poptFreeCon‐
273       text():
274
275       #include <popt.h>
276       void poptFreeContext(poptContext con);
277       void poptResetContext(poptContext con);
278
279
280   3. PARSING THE COMMAND LINE
281       After an application has created a poptContext, it  may  begin  parsing
282       arguments. poptGetNextOpt() performs the actual argument parsing.
283
284       #include <popt.h>
285       int poptGetNextOpt(poptContext con);
286
287       Taking  the context as its sole argument, this function parses the next
288       command-line argument found. After finding the  next  argument  in  the
289       option table, the function fills in the object pointed to by the option
290       table entry's arg pointer if it is not NULL. If the val entry  for  the
291       option is non-0, the function then returns that value. Otherwise, popt‐
292       GetNextOpt() continues on to the next argument.
293
294       poptGetNextOpt() returns -1 when the final argument  has  been  parsed,
295       and  other negative values when errors occur. This makes it a good idea
296       to keep the val elements in the options table greater than 0.
297
298       If all of the command-line options are handled  through  arg  pointers,
299       command-line parsing is reduced to the following line of code:
300
301       rc = poptGetNextOpt(poptcon);
302
303       Many  applications require more complex command-line parsing than this,
304       however, and use the following structure:
305
306       while ((rc = poptGetNextOpt(poptcon)) > 0) {
307            switch (rc) {
308                 /* specific arguments are handled here */
309            }
310       }
311
312       When returned options are handled, the application needs  to  know  the
313       value  of any arguments that were specified after the option. There are
314       two ways to discover them. One is to ask popt to  fill  in  a  variable
315       with  the  value of the option through the option table's arg elements.
316       The other is to use poptGetOptArg():
317
318       #include <popt.h>
319       const char * poptGetOptArg(poptContext con);
320
321       This function returns the argument given for the final option  returned
322       by poptGetNextOpt(), or it returns NULL if no argument was specified.
323
324
325   4. LEFTOVER ARGUMENTS
326       Many  applications  take an arbitrary number of command-line arguments,
327       such as a list of file names. When popt  encounters  an  argument  that
328       does  not begin with a -, it assumes it is such an argument and adds it
329       to a list of leftover arguments. Three functions allow applications  to
330       access such arguments:
331
332       const char * poptGetArg(poptContext con);
333              This function returns the next leftover argument and marks it as
334              processed.
335
336       const char * poptPeekArg(poptContext con);
337              The next leftover argument is returned but not  marked  as  pro‐
338              cessed.  This allows an application to look ahead into the argu‐
339              ment list, without modifying the list.
340
341       const char ** poptGetArgs(poptContext con);
342              All the leftover arguments are returned in a manner identical to
343              argv.   The  final element in the returned array points to NULL,
344              indicating the end of the arguments.
345
346
347   5. AUTOMATIC HELP MESSAGES
348       The  popt  library  can  automatically  generate  help  messages  which
349       describe  the  options  a  program accepts. There are two types of help
350       messages which can be generated. Usage messages are  a  short  messages
351       which  lists  valid  options, but does not describe them. Help messages
352       describe each option on one (or more) lines, resulting in a longer, but
353       more  useful,  message.  Whenever automatic help messages are used, the
354       descrip and argDescrip  fields  struct  poptOption  members  should  be
355       filled in for each option.
356
357       The  POPT_AUTOHELP  macro  makes it easy to add --usage and --help mes‐
358       sages to your program, and is described in part 1 of this man page.  If
359       more control is needed over your help messages, the following two func‐
360       tions are available:
361
362       #include <popt.h>
363       void poptPrintHelp(poptContext con, FILE * f, int flags);
364       void poptPrintUsage(poptContext con, FILE * f, int flags);
365
366       poptPrintHelp() displays the standard help message to  the  stdio  file
367       descriptor  f,  while  poptPrintUsage() displays the shorter usage mes‐
368       sage. Both functions currently ignore the flags argument; it  is  there
369       to allow future changes.
370
371

ERROR HANDLING

373       All of the popt functions that can return errors return integers.  When
374       an error occurs, a negative error code is returned. The following table
375       summarizes the error codes that occur:
376
377            Error                      Description
378       POPT_ERROR_NOARG       Argument missing for an option.
379       POPT_ERROR_BADOPT      Option's argument couldn't be parsed.
380       POPT_ERROR_OPTSTOODEEP Option aliasing nested too deeply.
381       POPT_ERROR_BADQUOTE    Quotations do not match.
382       POPT_ERROR_BADNUMBER   Option couldn't be converted to number.
383       POPT_ERROR_OVERFLOW    A given number was too big or small.
384
385       Here is a more detailed discussion of each error:
386
387
388       POPT_ERROR_NOARG
389              An option that requires an argument was specified on the command
390              line, but no argument was given. This can be  returned  only  by
391              poptGetNextOpt().
392
393
394       POPT_ERROR_BADOPT
395              An  option was specified in argv but is not in the option table.
396              This error can be returned only from poptGetNextOpt().
397
398
399       POPT_ERROR_OPTSTOODEEP
400              A set of option aliases is nested too  deeply.  Currently,  popt
401              follows  options  only  10 levels to prevent infinite recursion.
402              Only poptGetNextOpt() can return this error.
403
404
405       POPT_ERROR_BADQUOTE
406              A parsed string has a quotation mismatch (such as a single  quo‐
407              tation  mark).  poptParseArgvString(),  poptReadConfigFile(), or
408              poptReadDefaultConfig() can return this error.
409
410
411       POPT_ERROR_BADNUMBER
412              A conversion from a string to a number (int or long) failed  due
413              to the string containing nonnumeric characters. This occurs when
414              poptGetNextOpt() is processing an argument of type POPT_ARG_INT,
415              POPT_ARG_LONG, POPT_ARG_FLOAT, or POPT_ARG_DOUBLE.
416
417
418       POPT_ERROR_OVERFLOW
419              A  string-to-number conversion failed because the number was too
420              large or too small. Like POPT_ERROR_BADNUMBER,  this  error  can
421              occur  only  when  poptGetNextOpt() is processing an argument of
422              type    POPT_ARG_INT,    POPT_ARG_LONG,    POPT_ARG_FLOAT,    or
423              POPT_ARG_DOUBLE.
424
425
426       POPT_ERROR_ERRNO
427              A  system  call returned with an error, and errno still contains
428              the error from the system call.  Both  poptReadConfigFile()  and
429              poptReadDefaultConfig() can return this error.
430
431
432       Two functions are available to make it easy for applications to provide
433       good error messages.
434
435
436              const char *const poptStrerror(const int error);
437              This function takes a popt  error  code  and  returns  a  string
438              describing the error, just as with the standard strerror() func‐
439              tion.
440
441
442              const char * poptBadOption(poptContext con, int flags);
443              If an error  occurred  during  poptGetNextOpt(),  this  function
444              returns  the option that caused the error. If the flags argument
445              is  set  to  POPT_BADOPTION_NOALIAS,  the  outermost  option  is
446              returned.  Otherwise,  flags should be 0, and the option that is
447              returned may have been specified through an alias.
448
449       These two functions make popt error handling trivial for most  applica‐
450       tions.  When  an error is detected from most of the functions, an error
451       message is printed along with the  error  string  from  poptStrerror().
452       When an error occurs during argument parsing, code similiar to the fol‐
453       lowing displays a useful error message:
454
455       fprintf(stderr, "%s: %s\n",
456               poptBadOption(optCon, POPT_BADOPTION_NOALIAS),
457               poptStrerror(rc));
458
459

OPTION ALIASING

461       One of the primary benefits of using popt over getopt() is the  ability
462       to  use  option  aliasing. This lets the user specify options that popt
463       expands into other options when they are  specified.  If  the  standard
464       grep  program  made  use  of popt, users could add a --text option that
465       expanded to -i -n -E -2 to let them more  easily  find  information  in
466       text files.
467
468
469   1. SPECIFYING ALIASES
470       Aliases  are  normally specified in two places: /etc/popt and the .popt
471       file in the user's home directory (found through the  HOME  environment
472       variable).  Both  files  have  the  same format, an arbitrary number of
473       lines formatted like this:
474
475       appname alias newoption expansion
476
477       The appname is the name of the application, which must be the  same  as
478       the name parameter passed to poptGetContext(). This allows each file to
479       specify aliases for multiple programs. The alias keyword specifies that
480       an  alias  is being defined; currently popt configuration files support
481       only aliases, but other abilities may be added in the future. The  next
482       option  is  the  option  that should be aliased, and it may be either a
483       short or a long option. The rest of the line  specifies  the  expansion
484       for  the alias. It is parsed similarly to a shell command, which allows
485       \, ", and ' to be used for quoting. If a backslash is the final charac‐
486       ter  on  a  line,  the next line in the file is assumed to be a logical
487       continuation of the line containing the backslash, just as in shell.
488
489       The following entry would add a --text option to the grep  command,  as
490       suggested at the beginning of this section.
491
492       grep alias --text -i -n -E -2
493
494   2. ENABLING ALIASES
495       An  application  must  enable  alias expansion for a poptContext before
496       calling poptGetNextArg() for the first time. There are three  functions
497       that define aliases for a context:
498
499
500              int poptReadDefaultConfig(poptContext con, int flags);
501              This function reads aliases from /etc/popt and the .popt file in
502              the user's home directory. Currently, flags should be  NULL,  as
503              it is provided only for future expansion.
504
505
506              int poptReadConfigFile(poptContext con, char * fn);
507              The file specified by fn is opened and parsed as a popt configu‐
508              ration file. This allows programs to use  program-specific  con‐
509              figuration files.
510
511
512              int poptAddAlias(poptContext con, struct poptAlias alias,
513                               int flags);
514              Occasionally,  processes  want to specify aliases without having
515              to read them from a configuration file. This function adds a new
516              alias  to  a  context.  The flags argument should be 0, as it is
517              currently reserved for future expansion. The new alias is speci‐
518              fied as a struct poptAlias, which is defined as:
519
520              struct poptAlias {
521                   const char * longName; /* may be NULL */
522                   char shortName; /* may be '\0' */
523                   int argc;
524                   const char ** argv; /* must be free()able */
525              };
526
527              The  first  two  elements,  longName  and shortName, specify the
528              option that is aliased. The final two, argc and argv, define the
529              expansion to use when the aliases option is encountered.
530

PARSING ARGUMENT STRINGS

532       Although  popt  is  usually  used for parsing arguments already divided
533       into an argv-style array, some programs need to parse strings that  are
534       formatted  identically  to command lines. To facilitate this, popt pro‐
535       vides a function that parses a string into an array of  strings,  using
536       rules similiar to normal shell parsing.
537
538       #include <popt.h>
539       int poptParseArgvString(char * s, int * argcPtr,
540                               char *** argvPtr);
541       int poptDupArgv(int argc, const char ** argv, int * argcPtr,
542                               const char *** argvPtr);
543
544       The string s is parsed into an argv-style array. The integer pointed to
545       by the argcPtr parameter contains the number of  elements  parsed,  and
546       the  final  argvPtr parameter contains the address of the newly created
547       array.  The routine poptDupArgv() can be used to  make  a  copy  of  an
548       existing argument array.
549
550       The  argvPtr created by poptParseArgvString() or poptDupArgv() is suit‐
551       able to pass directly to poptGetContext().  Both routines return a sin‐
552       gle  dynamically  allocated  contiguous  block of storage and should be
553       free()ed when the application is finished with the storage.
554

HANDLING EXTRA ARGUMENTS

556       Some applications implement the equivalent of option aliasing but  need
557       to  do so through special logic. The poptStuffArgs() function allows an
558       application to insert new arguments into the current poptContext.
559
560       #include <popt.h>
561       int poptStuffArgs(poptContext con, const char ** argv);
562
563       The passed argv must have a NULL pointer as  its  final  element.  When
564       poptGetNextOpt()  is next called, the "stuffed" arguments are the first
565       to be parsed. popt returns to the normal arguments once all the stuffed
566       arguments have been exhausted.
567

EXAMPLE

569       The  following  example  is a simplified version of the program "robin"
570       which appears in Chapter 15 of the text cited below.   Robin  has  been
571       stripped   of  everything  but  its  argument-parsing  logic,  slightly
572       reworked, and renamed "parse." It may prove useful in  illustrating  at
573       least some of the features of the extremely rich popt library.
574
575       #include <popt.h>
576       #include <stdio.h>
577
578       void usage(poptContext optCon, int exitcode, char *error, char *addl) {
579           poptPrintUsage(optCon, stderr, 0);
580           if (error) fprintf(stderr, "%s: %s0, error, addl);
581           exit(exitcode);
582       }
583
584       int main(int argc, char *argv[]) {
585          char    c;            /* used for argument parsing */
586          int     i = 0;        /* used for tracking options */
587          char    *portname;
588          int     speed = 0;    /* used in argument parsing to set speed */
589          int     raw = 0;      /* raw mode? */
590          int     j;
591          char    buf[BUFSIZ+1];
592          poptContext optCon;   /* context for parsing command-line options */
593
594          struct poptOption optionsTable[] = {
595                                     { "bps", 'b', POPT_ARG_INT, &speed, 0,
596                                                                   "signaling rate in bits-per-second", "BPS" },
597                                     { "crnl", 'c', 0, 0, 'c',
598                                                                   "expand cr characters to cr/lf sequences" },
599                                     { "hwflow", 'h', 0, 0, 'h',
600                                                                   "use hardware (RTS/CTS) flow control" },
601                                     { "noflow", 'n', 0, 0, 'n',
602                                                                   "use no flow control" },
603                                     { "raw", 'r', 0, &raw, 0,
604                                                                   "don't perform any character conversions" },
605                                     { "swflow", 's', 0, 0, 's',
606                                                                   "use software (XON/XOF) flow control" } ,
607                                     POPT_AUTOHELP
608                                     { NULL, 0, 0, NULL, 0 }
609          };
610
611          optCon = poptGetContext(NULL, argc, argv, optionsTable, 0);
612          poptSetOtherOptionHelp(optCon, "[OPTIONS]* <port>");
613
614          if (argc < 2) {
615                                 poptPrintUsage(optCon, stderr, 0);
616                                 exit(1);
617          }
618
619          /* Now do options processing, get portname */
620          while ((c = poptGetNextOpt(optCon)) >= 0) {
621             switch (c) {
622                case 'c':
623                   buf[i++] = 'c';
624                   break;
625                case 'h':
626                   buf[i++] = 'h';
627                   break;
628                case 's':
629                   buf[i++] = 's';
630                   break;
631                case 'n':
632                   buf[i++] = 'n';
633                   break;
634             }
635          }
636          portname = poptGetArg(optCon);
637          if((portname == NULL) || !(poptPeekArg(optCon) == NULL))
638             usage(optCon, 1, "Specify a single port", ".e.g., /dev/cua0");
639
640          if (c < -1) {
641             /* an error occurred during option processing */
642             fprintf(stderr, "%s: %s\n",
643                     poptBadOption(optCon, POPT_BADOPTION_NOALIAS),
644                     poptStrerror(c));
645             return 1;
646          }
647
648          /* Print out options, portname chosen */
649          printf("Options  chosen: ");
650          for(j = 0; j < i ; j++)
651             printf("-%c ", buf[j]);
652          if(raw) printf("-r ");
653          if(speed) printf("-b %d ", speed);
654          printf("\nPortname chosen: %s\n", portname);
655
656          poptFreeContext(optCon);
657          exit(0);
658       }
659
660       RPM,  a  popular  Linux  package management program, makes heavy use of
661       popt's features. Many of its  command-line  arguments  are  implemented
662       through  popt  aliases,  which makes RPM an excellent example of how to
663       take advantage of the popt library. For more information  on  RPM,  see
664       http://www.rpm.org.  The  popt  source  code distribution includes test
665       program(s) which use all of the features of the popt libraries in vari‐
666       ous ways. If a feature isn't working for you, the popt test code is the
667       first place to look.
668

BUGS

670       None presently known.
671

AUTHOR

673       Erik W. Troan <ewt@redhat.com>
674
675       This man page is derived in part from Linux Application Development  by
676       Michael  K.  Johnson  and  Erik W. Troan, Copyright (c) 1998 by Addison
677       Wesley Longman, Inc., and included in the popt documentation  with  the
678       permission of the Publisher and the appreciation of the Authors.
679
680       Thanks to Robert Lynch for his extensive work on this man page.
681

SEE ALSO

683       getopt(3)
684
685       Linux  Application Development, by Michael K. Johnson and Erik W. Troan
686       (Addison-Wesley, 1998; ISBN 0-201-30821-5), Chapter 24.
687
688       popt.ps is a Postscript version of the above cited book chapter. It can
689       be   found   in   the   source   archive   for   popt   available   at:
690       ftp://ftp.rpm.org/pub/rpm.
691
692
693
694                                 June 30, 1998                         POPT(3)
Impressum