Home | History | Annotate | Download | only in man3
      1 <!DOCTYPE REFENTRY PUBLIC "-//Sun Microsystems//DTD DocBook V3.0-Based SolBook Subset V2.0//EN" [
      2 <!--ArborText, Inc., 1988-1999, v.4002-->
      3 <!--ARC : LSARC 2001/201 GNOME 2.X on Solaris-->
      4 <!ENTITY suncopy "Copyright (c) 2003, Sun Microsystems, Inc. 
      5 Copyright (c) 1998 Addison Wesley Longman, Inc.
      6  All Rights Reserved.">
      7 <!ENTITY cmd "libpopt">
      8 <!ENTITY % commonents SYSTEM "smancommon.ent">
      9 %commonents;
     10 <!ENTITY % booktitles SYSTEM "booktitles.ent">
     11 %booktitles;
     12 ]>
     13 <?Pub UDT _bookmark _target>
     14 <?Pub EntList brvbar bull cross dash diam diams frac12 frac13 frac14 hellip
     15  laquo lArr loz mdash nabla ndash para pound rArr raquo sect yen lt gt minus
     16  equals bsol sol amp>
     17 <?Pub Inc>
     18 <refentry id="libpopt-3">
     19 <!-- %Z%%M% %I% %E% SMI; -->
     20 <refmeta><refentrytitle>&cmd;</refentrytitle><manvolnum>3</manvolnum>
     21 <refmiscinfo class="date">31 May 2004</refmiscinfo>
     22 <refmiscinfo class="sectdesc">&man3;</refmiscinfo>
     23 <refmiscinfo class="software">&release;</refmiscinfo>
     24 <refmiscinfo class="arch">generic</refmiscinfo>
     25 <refmiscinfo class="copyright">&suncopy;</refmiscinfo>
     26 </refmeta>
     27 <indexterm><primary>&cmd;</primary></indexterm><indexterm><primary>parse
     28 command-line options</primary></indexterm>
     29 <refnamediv id="libpopt-3-name"><refname>&cmd;</refname><refpurpose>parse
     30 command-line options</refpurpose></refnamediv>
     31 <refsect1 id="libpopt-3-synp"><title>&synp-tt;</title>
     32 <programlisting>#include &lt;popt.h>
     33 
     34        poptContext poptGetContext(const char * name, int argc,
     35                                   const char ** argv,
     36                                   const struct poptOption * options,
     37                                   int flags);
     38 
     39        void poptFreeContext(poptContext con);
     40 
     41        void poptResetContext(poptContext con);
     42 
     43        int poptGetNextOpt(poptContext con);
     44 
     45        const char * poptGetOptArg(poptContext con);
     46 
     47        const char * poptGetArg(poptContext con);
     48 
     49        const char * poptPeekArg(poptContext con);
     50 
     51        const char ** poptGetArgs(poptContext con);
     52 
     53        const char *const poptStrerror(const int error);
     54 
     55        const char * poptBadOption(poptContext con, int flags);
     56 
     57        int poptReadDefaultConfig(poptContext con, int flags);
     58 
     59        int poptReadConfigFile(poptContext con, char * fn);
     60 
     61        int poptAddAlias(poptContext con, struct poptAlias alias,
     62                         int flags);
     63 
     64        int poptParseArgvString(char * s, int *  argcPtr,
     65                                const char *** argvPtr);
     66 
     67        int poptDupArgv(int argc, const char ** argv, int * argcPtr,
     68                                const char *** argvPtr);
     69 
     70        int poptStuffArgs(poptContext con, const char ** argv);</programlisting>
     71 </refsect1>
     72 <refsect1 id="libpopt-3-desc"><title>&desc-tt;</title>
     73 <para>The <filename>popt</filename> library parses command-line options. The <filename>
     74 popt</filename> library provides an alternative to parsing the <literal>argv
     75 </literal> array by hand, or using the <citerefentry><refentrytitle>getopt
     76 </refentrytitle><manvolnum>3</manvolnum></citerefentry> functions <literal>
     77 getopt()</literal> and <literal>getopt_long()</literal>. </para>
     78 <para>The <filename>popt</filename> library has the following advantages:
     79 </para>
     80 <itemizedlist>
     81 <listitem><para><filename>popt</filename> does not use global variables, thus
     82 enabling multiple passes in parsing <literal>argv</literal>.</para></listitem>
     83 <listitem><para><filename>popt</filename> can parse an arbitrary array of <literal>
     84 argv</literal>-style elements, allowing parsing of command-line strings from
     85 any source.</para></listitem>
     86 <listitem><para><filename>popt</filename> provides a standard method of option
     87 aliasing. This feature is discussed in detail below.</para></listitem>
     88 <listitem><para><filename>popt</filename> can exec external option filters.
     89 </para></listitem>
     90 <listitem><para><filename>popt</filename> can automatically generate help
     91 and usage messages for the application.</para></listitem>
     92 </itemizedlist>
     93 <para>The <filename>popt</filename> library supports short and long options.
     94 A short option consists of a hyphen followed by a single alphanumeric character.
     95 A long option, common in GNU utilities, consists of two hyphens followed by
     96 a string composed of letters, numbers, and hyphens. Long options can optionally
     97 begin with a single hyphen, primarily to allow command-line compatibility
     98 between <filename>popt</filename> applications and X toolkit applications.
     99 Either type of option can be followed by an argument. A space separates a
    100 short option from its argument. Either a space or an equals sign separates
    101 a long option from an argument.</para>
    102 <para>The <filename>popt</filename> library is highly portable and should
    103 work on any POSIX platform. The latest version is distributed with <literal>
    104 rpm</literal> and is available from: <filename>ftp://ftp.rpm.org/pub/rpm/dist
    105 </filename>.</para>
    106 <para>The <filename>popt</filename> library may be redistributed under the
    107 X consortium license, see the file <filename>COPYING</filename> in the <filename>
    108 popt</filename> source distribution for details.</para>
    109 </refsect1>
    110 <refsect1 id="libpopt-3-exde"><title>&exde-tt;</title>
    111 <refsect2 id="libpopt-3-exde-optb">
    112 <title>Option Tables</title>
    113 <para>Each application provides <filename>popt</filename> with information
    114 about the command-line options for the application, by means of an option
    115 table. An option table is an array of <literal>struct poptOption</literal>
    116 structures, with the following format:</para>
    117 <programlisting>#include &lt;popt.h>
    118 
    119 struct poptOption {
    120     const char * longName; /* may be NULL */
    121     char shortName;        /* may be '\0' */
    122     int argInfo;
    123     void * arg;            /* depends on argInfo */
    124     int val;               /* 0 means do not return, just update flag */
    125     char * descrip;        /* description for autohelp -- may be NULL */
    126     char * argDescrip;     /* argument description for autohelp */
    127 };</programlisting>
    128 <refsect3 id="libpopt-3-exde-optb-memb">
    129 <title>Option Table Members</title>
    130 <para>Each member of the table defines a single option that may be passed
    131 to the program. Long and short options are considered to be a single option
    132 that can occur in two different forms. The option table members are as follows:
    133 </para>
    134 <variablelist termlength="medium">
    135 <varlistentry><term><literal>longName</literal></term><listitem><para>Defines
    136 the name of the option in a long name.</para>
    137 </listitem></varlistentry>
    138 <varlistentry><term><literal>shortName</literal></term><listitem><para>Defines
    139 the name of the option in a single character.</para>
    140 </listitem></varlistentry>
    141 <varlistentry><term><literal>argInfo</literal></term><listitem><para>Tells <filename>
    142 popt</filename> what type of argument is expected after the option. Valid
    143 values are as follows: </para>
    144 <variablelist termlength="narrow">
    145 <varlistentry><term>POPT_ARG_DOUBLE</term><listitem><para>Double argument
    146 expected, <literal>arg</literal> type: <literal>double</literal></para>
    147 </listitem></varlistentry>
    148 <varlistentry><term>POPT_ARG_FLOAT</term><listitem><para>Float argument expected, <literal>
    149 arg</literal> type: <literal>float</literal></para>
    150 </listitem></varlistentry>
    151 <varlistentry><term>POPT_ARG_INT</term><listitem><para>Integer argument expected, <literal>
    152 arg</literal> type: <literal>int</literal></para>
    153 </listitem></varlistentry>
    154 <varlistentry><term>POPT_ARG_LONG</term><listitem><para>Long integer expected, <literal>
    155 arg</literal> type: <literal>long</literal></para>
    156 </listitem></varlistentry>
    157 <varlistentry><term>POPT_ARG_NONE</term><listitem><para>No argument expected, <literal>
    158 arg</literal> type: <literal>int</literal></para>
    159 </listitem></varlistentry>
    160 <varlistentry><term>POPT_ARG_STRING</term><listitem><para>No type checking
    161 to be performed, <literal>arg</literal> type: <literal>char *</literal></para>
    162 </listitem></varlistentry>
    163 <varlistentry><term>POPT_ARG_VAL</term><listitem><para>Integer value taken
    164 from val, <literal>arg</literal> type: <literal>int</literal></para>
    165 </listitem></varlistentry>
    166 </variablelist><para>For numeric values, if the <literal>argInfo</literal>
    167 value is bitwise or'd with one of POPT_ARGFLAG_OR, POPT_ARGFLAG_AND, or POPT_ARGFLAG_XOR,
    168 the value is saved by performing an OR, AND, or XOR. If the <literal>argInfo
    169 </literal> value is bitwise or'd with POPT_ARGFLAG_NOT, the value is negated
    170 before saving. For the common operations of setting or clearing bits, POPT_BIT_SET
    171 and POPT_BIT_CLR have the appropriate flags set to perform bit operations.
    172 </para>
    173 <para>If the <literal>argInfo</literal>value is bitwise or'd with POPT_ARGFLAG_ONEDASH,
    174 the long argument may be given with a single hyphen instead of two. For example,
    175 if <option>&ndash;longopt</option> is an option with POPT_ARGFLAG_ONEDASH, <option>
    176 longopt</option> is also accepted.</para>
    177 </listitem></varlistentry>
    178 <varlistentry><term><literal>arg</literal></term><listitem><para>Allows <filename>
    179 popt</filename> to automatically update program variables. If <literal>arg
    180 </literal> is NULL, <filename>popt</filename> ignores <literal>arg</literal>
    181 and takes no special action. Otherwise, <literal>arg</literal> points to a
    182 variable of the appropriate type, as follows: <itemizedlist>
    183 <listitem><para>If <literal>argInfo</literal> is POPT_ARG_NONE, the variable
    184 pointed to by <literal>arg</literal> is set to 1 when the option is used. 
    185 </para>
    186 </listitem>
    187 <listitem><para>If the option takes an argument, the variable pointed to by <literal>
    188 arg</literal> is updated to reflect the value of the argument. Any string
    189 is acceptable for POPT_ARG_STRING arguments. POPT_ARG_INT, POPT_ARG_LONG,
    190 POPT_ARG_FLOAT, and POPT_ARG_DOUBLE arguments are converted to the appropriate
    191 type, and an error returned if the conversion fails.</para>
    192 </listitem>
    193 </itemizedlist></para>
    194 <para>POPT_ARG_VAL causes <literal>arg</literal> to be set to the integer
    195 value of <literal>val</literal> when the argument is found. This is useful
    196 for mutually-exclusive arguments in cases where it is not an error for multiple
    197 arguments to occur and where you want the last argument specified to take
    198 precedence, for example, <command>rm -i -f</command>.  POPT_ARG_VAL causes
    199 the parsing function not to return a value, because the value of <literal>
    200 val</literal> has already been used.</para>
    201 <para>If the <literal>argInfo</literal> value is bitwise or'd with POPT_ARGFLAG_OPTIONAL,
    202 the argument to the long option may be omitted. If the long option is used
    203 without an argument, a default value of zero or NULL is saved if the <literal>
    204 arg</literal> pointer is present. Otherwise, the behavior is identical to
    205 that of a long option with an argument.</para>
    206 </listitem></varlistentry>
    207 <varlistentry><term><literal>val</literal></term><listitem><para>The value
    208 returned by the <filename>popt</filename> parsing function when the option
    209 is encountered. If <literal>val</literal> is 0, the parsing function does
    210 not return a value. Instead, <filename>popt</filename> parses the next command-line
    211 argument.</para>
    212 </listitem></varlistentry>
    213 <varlistentry><term>descrip</term><listitem><para>Text description of the
    214 argument. Only required if automatic help messages are desired. Automatic
    215 usage messages can be generated without this argument.</para>
    216 </listitem></varlistentry>
    217 <varlistentry><term>argDescrip</term><listitem><para>Short summary of the
    218 type of arguments expected by the option, or NULL if the option does not require
    219 any arguments. Only required if automatic help messages are desired. Automatic
    220 usage messages can be generated without this argument.</para>
    221 </listitem></varlistentry>
    222 </variablelist><para>The final structure in the table should have all pointer
    223 values set to NULL and all arithmetic values set to 0, marking the end of
    224 the table. The macro POPT_TABLEEND performs these tasks.</para>
    225 </refsect3>
    226 <refsect3 id="libpopt-3-exde-optb-huse">
    227 <title>Help and Usage Output</title>
    228 <para>If <filename>popt</filename> should automatically provide <option>&minus;usage
    229 </option> and <option>&minus;help</option> options, one  line in the option
    230 table should contain the macro POPT_AUTOHELP. This macro includes another
    231 option table, via POPT_ARG_INCLUDE_TABLE, which provides the table entries
    232 for these arguments. When the <option>&minus;usage</option> or <option>&minus;help
    233 </option> option is passed to applications that use <filename>popt</filename>
    234 automatic help, <filename>popt</filename> displays the appropriate message
    235 on <literal>stderr</literal>, and exits the application with a return code
    236 of 0.  To use <filename>popt</filename> automatic help generation in a different
    237 way, you must explicitly add the option entries to the application's option
    238 table, instead of using POPT_AUTOHELP.</para>
    239 <para>If the <literal>argInfo</literal> value is bitwise or'd with POPT_ARGFLAG_DOC_HIDDEN,
    240 the argument is not shown in help output.</para>
    241 <para>If the <literal>argInfo</literal> value is bitwise or'd with POPT_ARGFLAG_SHOW_DEFAULT,
    242 the inital value of the <literal>arg</literal> is shown in help output.</para>
    243 </refsect3>
    244 <refsect3 id="libpopt-3-exde-optb-spec">
    245 <title>Special Option Table Entries</title>
    246 <para>Two types of option table entries do not specify command-line options.
    247 When either of these types of entries is used, the <literal>longName</literal>
    248 element must be NULL and the <literal>shortName</literal> element must be <literal>
    249 \\0</literal>.</para>
    250 <para>The first of these special entry types allows the application to nest
    251 another option table in the current option table. Such nesting may extend
    252 quite deeply, the actual depth is limited by the application stack. Including
    253 other option tables allows a library to provide a standard set of command-line
    254 options to every application that uses the library. This is often done in
    255 graphical programming toolkits, for example. To nest another option table,
    256 set the <literal>argInfo</literal> field to POPT_ARG_INCLUDE_TABLE and the <literal>
    257 arg</literal> field to point to the table that is being included. If automatic
    258 help generation is used, the <literal>descrip</literal> field should contain
    259 an overall description of the option table being included.</para>
    260 <para>The other special option table entry type tells <filename>popt</filename>
    261 to call a function when any option in that table is found. This callback functionality
    262 is especially useful when included option tables are used, because the application
    263 that provides the top-level option table does not need to be aware of the
    264 other options that are provided by the included table. When a callback is
    265 set for a table, the parsing function never returns information on an option
    266 in the table. Instead, option information must be retained via the callback
    267 or by having <filename>popt</filename> set a variable through the option's <literal>
    268 arg</literal> field. Option callbacks should match the following prototype:
    269 </para>
    270 <literallayout>void poptCallbackType(poptContext con,
    271                       const struct poptOption * opt,
    272                       const char * arg, void * data);
    273 
    274 </literallayout>
    275 <para>The callback uses the following parameters:</para>
    276 <variablelist termlength="narrow">
    277 <varlistentry><term><literal>con</literal></term><listitem><para>The context
    278 that is being parsed. See the next section for information on contexts.</para>
    279 </listitem></varlistentry>
    280 <varlistentry><term><literal>opt</literal></term><listitem><para>The option
    281 that triggered this callback.</para>
    282 </listitem></varlistentry>
    283 <varlistentry><term><literal>arg</literal></term><listitem><para>The argument
    284 for the <literal>opt</literal> option. If the option does not take an argument, <literal>
    285 arg</literal> is NULL.</para>
    286 </listitem></varlistentry>
    287 <varlistentry><term><literal>data</literal></term><listitem><para>Taken from
    288 the <literal>descrip</literal> field of the option table entry that defined
    289 the callback. As <literal>descrip</literal> is a pointer, this allows you
    290 to pass an arbitrary set of data to callback functions, though a typecast
    291 must be used.</para>
    292 </listitem></varlistentry>
    293 </variablelist><para>The option table entry that defines a callback has an <literal>
    294 argInfo</literal> of POPT_ARG_CALLBACK, an <literal>arg</literal> that points
    295 to the callback function, and a <literal>descrip</literal> field that specifies
    296 an arbitrary pointer to be passed to the callback.</para>
    297 </refsect3>
    298 </refsect2>
    299 <refsect2 id="libpopt-3-exde-ctxt">
    300 <title>Creating a Context</title>
    301 <para><filename>popt</filename> can interleave the parsing of multiple command-line
    302 sets. <filename>popt</filename> allows this by keeping all of the state information
    303 for a particular set of command-line arguments in a <literal>poptContext</literal>
    304 data structure, an opaque type that should not be modified outside the <filename>
    305 popt</filename> library.</para>
    306 <para>New <filename>popt</filename> contexts are created by <literal>poptGetContext()
    307 </literal>:</para>
    308 <programlisting>poptContext poptGetContext(const char * name, int argc,
    309                            const char ** argv,
    310                            const struct poptOption * options,
    311                            int flags);</programlisting>
    312 <para>The <literal>poptGetContext()</literal> function takes the following
    313 parameters:</para>
    314 <variablelist>
    315 <varlistentry><term><filename>name</filename></term><listitem><para>Used only
    316 for alias handling. <filename>name</filename> should be the name of the application
    317 whose options are being parsed, or should be NULL if no option aliasing is
    318 desired.</para>
    319 </listitem></varlistentry>
    320 <varlistentry><term><filename>argc</filename>, <filename>argv</filename></term>
    321 <listitem><para>Specifies the command-line arguments to parse. These arguments
    322 are generally passed to <literal>poptGetContext()</literal> exactly as they
    323 were passed to the application's <literal>main()</literal> function.</para>
    324 </listitem></varlistentry>
    325 <varlistentry><term><filename>options</filename></term><listitem><para>Points
    326 to the table of command-line options. See the Option Tables section above.
    327 </para>
    328 </listitem></varlistentry>
    329 <varlistentry><term><filename>flags</filename></term><listitem><para>Can take
    330 one of the following values:</para>
    331 <variablelist>
    332 <varlistentry><term>POPT_CONTEXT_NO_EXEC</term><listitem><para>Ignore <literal>
    333 exec</literal> expansions</para>
    334 </listitem></varlistentry>
    335 <varlistentry><term>POPT_CONTEXT_KEEP_FIRST</term><listitem><para>Do not ignore <literal>
    336 argv[0]</literal></para>
    337 </listitem></varlistentry>
    338 <varlistentry><term>POPT_CONTEXT_POSIXMEHARDER</term><listitem><para>Options
    339 cannot follow arguments</para>
    340 </listitem></varlistentry>
    341 </variablelist></listitem></varlistentry>
    342 </variablelist><para>A <literal>poptContext</literal> keeps track of which
    343 options have already been parsed and which remain to be parsed. If an application
    344 wishes to restart processing the options of a set of arguments, the application
    345 can reset the <literal>poptContext</literal> by passing the context as the
    346 sole argument to <literal>poptResetContext()</literal>.</para>
    347 <para>When argument processing is complete, the process should free the <literal>
    348 poptContext</literal>, as it contains dynamically allocated components. The <literal>
    349 poptFreeContext()</literal> function takes a <literal>poptContext</literal>
    350 as its sole argument  and frees the resources that the context is using.</para>
    351 <para>Here are the prototypes of both <literal>poptResetContext()</literal>
    352 and <literal>poptFreeContext()</literal>:</para>
    353 <programlisting>#include &lt;popt.h>
    354 void poptFreeContext(poptContext con);
    355 void poptResetContext(poptContext con);</programlisting>
    356 </refsect2>
    357 <refsect2 id="libpopt-3-exde-cmdl">
    358 <title>Parsing the Command Line</title>
    359 <para>After an application has created a <literal>poptContext</literal>, the <literal>
    360 poptContext</literal> may begin parsing arguments. <literal>poptGetNextOpt()
    361 </literal> performs the actual argument parsing:</para>
    362 <programlisting>#include &lt;popt.h>
    363 int poptGetNextOpt(poptContext con);</programlisting>
    364 <para>Taking the context as its sole argument, the <literal>poptGetNextOpt()
    365 </literal> function parses the next command-line argument found. When <literal>
    366 poptGetNextOpt()</literal> finds the next argument in the option table, the
    367 function populates the object pointed to by the option table entry's <literal>
    368 arg</literal> pointer, if the pointer is not NULL. If the <literal>val</literal>
    369 entry for the option is not zero, the function returns that value. Otherwise, <literal>
    370 poptGetNextOpt()</literal> continues to the next argument.</para>
    371 <para><literal>poptGetNextOpt()</literal> returns &minus;1 when the final
    372 argument has been parsed, and other negative values when errors occur. Therefore,
    373 you should ensure that the <literal>val</literal> elements in the option table
    374 are greater than 0.</para>
    375 <para>If all of the command-line options are handled through <literal>arg
    376 </literal> pointers, command-line parsing is reduced to the following line
    377 of code:</para>
    378 <programlisting>rc = poptGetNextOpt(poptcon);</programlisting>
    379 <para>Many applications require more complex command-line parsing than this,
    380 however, and use the following structure:</para>
    381 <programlisting>while ((rc = poptGetNextOpt(poptcon)) > 0) {
    382      switch (rc) {
    383           /* specific arguments are handled here */
    384      }
    385 }</programlisting>
    386 <para>When returned options are handled, the application needs to know the
    387 value of any arguments that were specified after the option. There are two
    388 ways to discover these values:</para>
    389 <itemizedlist>
    390 <listitem><para>Ask <filename>popt</filename> to populate a variable with
    391 the value of the option from the option table's <literal>arg</literal> elements.
    392 </para></listitem>
    393 <listitem><para>Use <literal>poptGetOptArg()</literal>:<programlisting>#include &lt;popt.h>
    394 const char * poptGetOptArg(poptContext con);</programlisting></para></listitem>
    395 </itemizedlist>
    396 <para>The <literal>poptGetOptArg()</literal> function returns the argument
    397 given for the final option returned by <literal>poptGetNextOpt()</literal>,
    398 or returns NULL if no argument was specified.</para>
    399 </refsect2>
    400 <refsect2 id="libpopt-3-exde-larg">
    401 <title>Leftover Arguments</title>
    402 <para>Many applications take an arbitrary number of command-line arguments,
    403 such as a list of file names. When <filename>popt</filename> encounters an
    404 argument that does not begin with a hyphen, <filename>popt</filename> assumes
    405 that this is such an argument, and adds the argument to a list of leftover
    406 arguments. Three functions allow applications to access such arguments:</para>
    407 <variablelist termlength="wholeline">
    408 <varlistentry><term><literal>const char * poptGetArg(poptContext con);</literal></term>
    409 <listitem><para>Returns the next leftover argument and marks the argument
    410 as processed.</para>
    411 </listitem></varlistentry>
    412 <varlistentry><term><literal>const char * poptPeekArg(poptContext con);</literal></term>
    413 <listitem><para>Returns the next leftover argument but does not mark the argument
    414 as processed. This allows an application to look ahead into the argument list,
    415 without modifying the list.</para>
    416 </listitem></varlistentry>
    417 <varlistentry><term><literal>const char ** poptGetArgs(poptContext con);</literal></term>
    418 <listitem><para>Returns all of the leftover arguments in a manner identical
    419 to <literal>argv</literal>. The final element in the returned array points
    420 to NULL, indicating the end of the arguments.</para>
    421 </listitem></varlistentry>
    422 </variablelist></refsect2>
    423 <refsect2 id="libpopt-3-exde-help">
    424 <title>Automatic Help Messages</title>
    425 <para>The popt library can automatically generate help messages that describe
    426 the options that an application accepts. Two types of help messages can be
    427 generated:</para>
    428 <itemizedlist>
    429 <listitem><para>Usage messages are short messages that list valid options,
    430 but do not describe the options.</para></listitem>
    431 <listitem><para>Help messages describe each option in one or more lines, resulting
    432 in a longer but more useful message.</para></listitem>
    433 </itemizedlist>
    434 <para>Whenever automatic help messages are used, the <literal>descrip</literal>
    435 and <literal>argDescrip</literal> members of the <literal>struct poptOption
    436 </literal> structure should be populated for each option.</para>
    437 <para>The POPT_AUTOHELP macro makes it easy to add usage and help messages
    438 to your application, as described earlier in this man page. If you need more
    439 control over your help messages, use the following functions:</para>
    440 <programlisting>#include &lt;popt.h>
    441 void poptPrintHelp(poptContext con, FILE * f, int flags);
    442 void poptPrintUsage(poptContext con, FILE * f, int flags);</programlisting>
    443 <para><literal>poptPrintHelp()</literal> displays the standard help message
    444 to the <literal>stdio</literal> file descriptor <literal>f</literal>, while <literal>
    445 poptPrintUsage()</literal> displays the shorter usage message. Both functions
    446 currently ignore the <literal>flags</literal> argument, which is provided
    447 for future functionality.</para>
    448 </refsect2>
    449 <refsect2 id="libpopt-3-exde-alia">
    450 <title>Option Aliasing</title>
    451 <para>One of the primary benefits of <filename>popt</filename> is the ability
    452 to use option aliasing. Option aliasing allows the user to specify options
    453 that <filename>popt</filename> expands into other options. For example. if
    454 the standard <command>grep</command> command made use of <filename>popt</filename>,
    455 users could add a <option>&minus;text</option> option that expanded to <literal>
    456 -i -n -E -2</literal>, to allow users to more easily find information in text
    457 files.</para>
    458 <refsect3 id="libpopt-3-exde-alia-spec">
    459 <title>Specifying Aliases</title>
    460 <para>Aliases are normally specified in two places: <itemizedlist>
    461 <listitem><para><filename>/etc/popt</filename></para></listitem>
    462 <listitem><para><filename>$HOME/.popt</filename></para></listitem>
    463 </itemizedlist></para>
    464 <para>Both files have the same format, that is, an arbitrary number of lines
    465 formatted as follows: <screen><replaceable>appname</replaceable> alias <replaceable>
    466 newoption</replaceable> <replaceable>expansion</replaceable></screen></para>
    467 <para>An alias specification is composed of the following elements:<variablelist>
    468 <varlistentry><term><replaceable>appname</replaceable></term><listitem><para>
    469 Specifies the name of the application, which must be the same as the <literal>
    470 name</literal> parameter passed to <literal>poptGetContext()</literal>. This
    471 allows each file to specify aliases for multiple programs.</para>
    472 </listitem></varlistentry>
    473 <varlistentry><term>alias</term><listitem><para>Specifies that an alias is
    474 being defined. Currently, <literal>popt</literal> configuration files support
    475 only aliases, but other abilities may be added in the future.</para>
    476 </listitem></varlistentry>
    477 <varlistentry><term><replaceable>newoption</replaceable></term><listitem>
    478 <para>Specifies the option that should be aliased, either a short option or
    479 a long option.</para>
    480 </listitem></varlistentry>
    481 <varlistentry><term><replaceable>expansion</replaceable></term><listitem>
    482 <para>Specifies the expansion for the alias. The expansion is parsed in a
    483 similar way to a shell command: backslashes are allowed, and single quotation
    484 marks can be used for quoting. If a backslash is the final character on a
    485 line, the next line in the file is assumed to be a logical continuation of
    486 the line containing the backslash, just as in a shell command.</para>
    487 </listitem></varlistentry>
    488 </variablelist></para>
    489 <para>For example, the following entry would add to the <command>grep</command>
    490 command the <option>&minus;text</option> option described earlier:</para>
    491 <para><computeroutput>grep alias --text -i -n -E -2</computeroutput></para>
    492 </refsect3>
    493 <refsect3 id="libpopt-3-exde-alia-enab">
    494 <title>Enabling Aliases</title>
    495 <para>An application must enable alias expansion for a <literal>poptContext
    496 </literal>, before calling <literal>poptGetNextArg()</literal> for the first
    497 time. Three functions define aliases for a context:</para>
    498 <variablelist termlength="wholeline">
    499 <varlistentry><term>int poptReadDefaultConfig(poptContext con, int flags);
    500 </term><listitem><para>Reads aliases from <filename>/etc/popt</filename> and <filename>
    501 $HOME/.popt</filename>.  The <literal>flags</literal> argument should be NULL,
    502 it is provided only for future expansion.</para>
    503 </listitem></varlistentry>
    504 <varlistentry><term>int poptReadConfigFile(poptContext con, char * fn);</term>
    505 <listitem><para>Opens the file specified by <literal>fn</literal> and parses
    506 the file as a <filename>popt</filename> configuration file. This allows applications
    507 to use application-specific configuration files.</para>
    508 </listitem></varlistentry>
    509 <varlistentry><term>int poptAddAlias(poptContext con, struct poptAlias alias,
    510 int flags);</term><listitem><para>Adds a new alias to a context. This function
    511 is useful when processes want to specify aliases without having to read them
    512 from a configuration file. The <literal>flags</literal> argument should be
    513 0, it is provided only for future expansion. The new alias is specified as
    514 a <literal>struct poptAlias</literal>, which is defined as follows:</para>
    515 <programlisting>struct poptAlias {
    516      const char * longName; /* may be NULL */
    517      char shortName; /* may be '\0' */
    518      int argc;
    519      const char ** argv; /* must be free()able */
    520 };</programlisting>
    521 <para><literal>longName</literal> and <literal>shortName</literal> specify
    522 the option that is aliased. <literal>argc</literal> and <literal>argv</literal>
    523 define the expansion to use when the aliases option is encountered.</para>
    524 </listitem></varlistentry>
    525 </variablelist></refsect3>
    526 </refsect2>
    527 <refsect2 id="libpopt-3-exde-pars">
    528 <title>Parsing Argument Strings</title>
    529 <para><filename>popt</filename> usually parses arguments that are already
    530 divided into an <literal>argv</literal>-style array. However, some applications
    531 need to parse strings that are formatted identically to command lines. To
    532 facilitate this, <literal>popt</literal> provides a function that parses a
    533 string into an array of strings, using rules similar to those of normal shell
    534 parsing:</para>
    535 <programlisting>#include &lt;popt.h>
    536 int poptParseArgvString(char * s, int * argcPtr,
    537                         char *** argvPtr);
    538 int poptDupArgv(int argc, const char ** argv, int * argcPtr,
    539                         const char *** argvPtr);</programlisting>
    540 <para>The string <literal>s</literal> is parsed into an <literal>argv</literal>-style
    541 array. The integer pointed to by the <literal>argcPtr</literal> parameter
    542 contains the number of elements parsed, and the final <literal>argvPtr</literal>
    543 parameter contains the address of the newly created array. The routine <literal>
    544 poptDupArgv()</literal> can be used to make a copy of an existing argument
    545 array.</para>
    546 <para>The <literal>argvPtr</literal> created by <literal>poptParseArgvString()
    547 </literal> or <literal>poptDupArgv()</literal> can be passed directly to <literal>
    548 poptGetContext()</literal>. Both routines return a single dynamically allocated
    549 contiguous block of storage and should be freed using <literal>free()</literal>
    550 when the application is finished with the storage.</para>
    551 </refsect2>
    552 <refsect2 id="libpopt-3-exde-earg">
    553 <title>Handling Extra Arguments</title>
    554 <para>Some applications implement the equivalent of option aliasing but do
    555 so using special logic. The <literal>poptStuffArgs()</literal> function allows
    556 an application to insert new arguments into the current <literal>poptContext
    557 </literal>:</para>
    558 <programlisting>#include &lt;popt.h>
    559 int poptStuffArgs(poptContext con, const char ** argv);</programlisting>
    560 <para>The passed <literal>argv</literal> must have a NULL pointer as its final
    561 element. When <literal>poptGetNextOpt()</literal> is next called, the "stuffed"
    562 arguments are the first to be parsed. <filename>popt</filename> returns to
    563 the normal arguments when all of the stuffed arguments have been exhausted.
    564 </para>
    565 </refsect2>
    566 </refsect1>
    567 <refsect1 id="libpopt-3-erro"><title>&erro-tt;</title>
    568 <para>All of the <filename>popt</filename> functions that can return errors
    569 return integers. When an error occurs, a negative error code is returned.
    570 The following error codes can occur:</para>
    571 <variablelist>
    572 <varlistentry><term>POPT_ERROR_BADNUMBER</term><listitem><para>A string-to-number
    573 conversion failed because the string contains nonnumeric characters. This
    574 occurs when <literal>poptGetNextOpt()</literal> is processing an argument
    575 of type POPT_ARG_INT, POPT_ARG_LONG, POPT_ARG_FLOAT, or POPT_ARG_DOUBLE.</para>
    576 </listitem></varlistentry>
    577 <varlistentry><term>POPT_ERROR_BADOPT</term><listitem><para>An option was
    578 specified in <literal>argv</literal> but is not in the option table. This
    579 error can be returned only from <literal>poptGetNextOpt()</literal>.</para>
    580 </listitem></varlistentry>
    581 <varlistentry><term>POPT_ERROR_BADQUOTE</term><listitem><para>A parsed string
    582 has a quotation mismatch, for example, a single quotation mark. <literal>
    583 poptParseArgvString()</literal>, <literal>poptReadConfigFile()</literal>,
    584 or <literal>poptReadDefaultConfig()</literal> can return this error.</para>
    585 </listitem></varlistentry>
    586 <varlistentry><term>POPT_ERROR_ERRNO</term><listitem><para>A system call returned
    587 with an error, and <literal>errno</literal> still contains the error from
    588 the system call. Both <literal>poptReadConfigFile()</literal> and <literal>
    589 poptReadDefaultConfig()</literal> can return this error.</para>
    590 </listitem></varlistentry>
    591 <varlistentry><term>POPT_ERROR_NOARG</term><listitem><para>An option that
    592 requires an argument was specified on the command line, but no argument was
    593 given. This error can be returned only by <literal>poptGetNextOpt()</literal>.
    594 </para>
    595 </listitem></varlistentry>
    596 <varlistentry><term>POPT_ERROR_OPTSTOODEEP</term><listitem><para>A set of
    597 option aliases is nested too deeply.  Currently, <filename>popt</filename>
    598 follows options to only 10 levels, to prevent infinite recursion. Only <literal>
    599 poptGetNextOpt()</literal> can return this error.</para>
    600 </listitem></varlistentry>
    601 <varlistentry><term>POPT_ERROR_OVERFLOW</term><listitem><para>A string-to-number
    602 conversion failed because the number is too large or too small. This error
    603 can occur only when  <literal>poptGetNextOpt()</literal> is processing an
    604 argument of type POPT_ARG_INT, POPT_ARG_LONG, POPT_ARG_FLOAT, or POPT_ARG_DOUBLE.
    605 </para>
    606 </listitem></varlistentry>
    607 </variablelist><para>Two functions allow applications to provide good error
    608 messages:</para>
    609 <variablelist termlength="wholeline">
    610 <varlistentry><term>const char *const poptStrerror(const int error);</term>
    611 <listitem><para>Takes a <literal>popt</literal> error code and returns a string
    612 describing the error, just as with the standard <literal>strerror()</literal>
    613 function.</para>
    614 </listitem></varlistentry>
    615 <varlistentry><term>const char * poptBadOption(poptContext con, int flags);
    616 </term><listitem><para>Returns the option that caused the error, if an error
    617 occurred during <literal>poptGetNextOpt()</literal>. If the <literal>flags
    618 </literal> argument is set to POPT_BADOPTION_NOALIAS, the outermost option
    619 is returned. Otherwise, <literal>flags</literal> should be 0, and the option
    620 that is returned may have been specified through an alias.</para>
    621 </listitem></varlistentry>
    622 </variablelist><para>These two functions ensure that <literal>popt</literal>
    623 error handling is trivial for most applications. When an error is detected
    624 from most of the functions, an error message is printed along with the error
    625 string from <literal>poptStrerror()</literal>. When an error occurs during
    626 argument parsing, code similar to the following displays a useful error message:
    627 </para>
    628 <programlisting>fprintf(stderr, "%s: %s\n",
    629         poptBadOption(optCon, POPT_BADOPTION_NOALIAS),
    630         poptStrerror(rc));</programlisting>
    631 </refsect1>
    632 <refsect1 id="libpopt-3-exam"><title>&exam-tt;</title>
    633 <example role="example">
    634 <title>Parse Program Created From robin Program</title>
    635 <para>The following example is a simplified version of the <literal>robin
    636 </literal> program that appears in Chapter 15 of &ldquo;Linux Application
    637 Development&rdquo; by Michael K. Johnson and Erik W. Troan (copyright 1998
    638 by Addison Wesley Longman, Inc.). The <literal>robin</literal> program has
    639 been stripped of everything but its argument-parsing logic, slightly reworked,
    640 and renamed <literal>parse</literal>. This program illustrates some of the
    641 features of the extremely rich <filename>popt</filename> library.</para>
    642 <programlisting>#include &lt;popt.h>
    643 #include &lt;stdio.h>
    644 
    645 void usage(poptContext optCon, int exitcode, char *error, char *addl) {
    646     poptPrintUsage(optCon, stderr, 0);
    647     if (error) fprintf(stderr, "%s: %s0, error, addl);
    648     exit(exitcode);
    649 }
    650 
    651 int main(int argc, char *argv[]) {
    652    char    c;            /* used for argument parsing */
    653    int     i = 0;        /* used for tracking options */
    654    char    *portname;
    655    int     speed = 0;    /* used in argument parsing to set speed */
    656    int     raw = 0;      /* raw mode? */
    657    int     j;
    658    char    buf[BUFSIZ+1];
    659    poptContext optCon;   /* context for parsing command-line options */
    660 
    661    struct poptOption optionsTable[] = {
    662                          { "bps", 'b', POPT_ARG_INT, &amp;speed, 0,
    663                               "signaling rate in bits-per-second", "BPS" },
    664                          { "crnl", 'c', 0, 0, 'c',
    665                               "expand cr characters to cr/lf sequences" },
    666                          { "hwflow", 'h', 0, 0, 'h',
    667                               "use hardware (RTS/CTS) flow control" },
    668                          { "noflow", 'n', 0, 0, 'n',
    669                               "use no flow control" },
    670                          { "raw", 'r', 0, &amp;raw, 0,
    671                               "don't perform any character conversions" },
    672                          { "swflow", 's', 0, 0, 's',
    673                                "use software (XON/XOF) flow control" } ,
    674                          POPT_AUTOHELP
    675                          { NULL, 0, 0, NULL, 0 }
    676    };
    677 
    678    optCon = poptGetContext(NULL, argc, argv, optionsTable, 0);
    679    poptSetOtherOptionHelp(optCon, "[OPTIONS]* &lt;port>");
    680 
    681    if (argc &lt; 2) {
    682                           poptPrintUsage(optCon, stderr, 0);
    683                           exit(1);
    684    }
    685 
    686    /* Now do options processing, get portname */
    687    while ((c = poptGetNextOpt(optCon)) >= 0) {
    688       switch (c) {
    689          case 'c':
    690             buf[i++] = 'c';
    691             break;
    692          case 'h':
    693             buf[i++] = 'h';
    694             break;
    695          case 's':
    696             buf[i++] = 's';
    697             break;
    698          case 'n':
    699             buf[i++] = 'n';
    700             break;
    701       }
    702   }
    703   portname = poptGetArg(optCon);
    704   if((portname == NULL) || !(poptPeekArg(optCon) == NULL))
    705      usage(optCon, 1, "Specify a single port", ".e.g., /dev/cua0");
    706 
    707   if (c &lt; -1) {
    708      /* an error occurred during option processing */
    709      fprintf(stderr, "%s: %s\n",
    710              poptBadOption(optCon, POPT_BADOPTION_NOALIAS),
    711              poptStrerror(c));
    712      return 1;
    713   }
    714 
    715   /* Print out options, portname chosen */
    716   printf("Options  chosen: ");
    717   for(j = 0; j &lt; i ; j++)
    718      printf("-%c ", buf[j]);
    719   if(raw) printf("-r ");
    720   if(speed) printf("-b %d ", speed);
    721   printf("\nPortname chosen: %s\n", portname);
    722 
    723   poptFreeContext(optCon);
    724   exit(0);
    725 }
    726 
    727 </programlisting>
    728 <para>RPM, a popular Linux package management application, uses several <filename>
    729 popt</filename> features. Many RPM command-line arguments are implemented
    730 using <filename>popt</filename> aliases, which makes RPM an excellent example
    731 of how to take advantage of the <filename>popt</filename> library. For more
    732 information about RPM, see <literal>http://www.rpm.org</literal>. The <filename>
    733 popt</filename> source code distribution includes test programs that use all
    734 of the features of the <filename>popt</filename> libraries in various ways.
    735 If a <filename>popt</filename> feature does not work for you, check the <filename>
    736 popt</filename> test code.</para>
    737 </example>
    738 </refsect1>
    739 <refsect1 id="libpopt-3-file"><title>&file-tt;</title>
    740 <para>The following files are used by this library:</para>
    741 <variablelist termlength="medium">
    742 <varlistentry><term><filename>/usr/lib/&cmd;.so</filename> </term><listitem>
    743 <para>Command Line Parser API shared library</para>
    744 </listitem></varlistentry>
    745 </variablelist></refsect1>
    746 <refsect1 id="libpopt-3-attr"><title>&attr-tt;</title>
    747 <para>See <olink targetdocent="REFMAN5" localinfo="attributes-5"><citerefentry>
    748 <refentrytitle>attributes</refentrytitle><manvolnum>5</manvolnum></citerefentry></olink>
    749 for descriptions of the following attributes:</para>
    750 <informaltable frame="all">
    751 <tgroup cols="2" colsep="1" rowsep="1"><colspec colname="COLSPEC0" colwidth="1*">
    752 <colspec colname="COLSPEC1" colwidth="1*">
    753 <thead>
    754 <row><entry align="center" valign="middle">ATTRIBUTE TYPE</entry><entry align="center"
    755 valign="middle">ATTRIBUTE VALUE</entry></row>
    756 </thead>
    757 <tbody>
    758 <row><entry><para>Availability</para></entry><entry><para>SUNWlibpopt</para></entry>
    759 </row>
    760 <row><entry colname="COLSPEC0"><para>Interface stability</para></entry><entry
    761 colname="COLSPEC1"><para>Volatile</para></entry></row>
    762 </tbody>
    763 </tgroup>
    764 </informaltable>
    765 </refsect1>
    766 <refsect1 id="libpopt-3-also"><title>&also-tt;</title>
    767 <!--Reference to another man page-->
    768 <!--Reference to a Help manual-->
    769 <!--Reference to a book.-->
    770 <para>
    771 <citerefentry><refentrytitle>getopt</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
    772 <citerefentry><refentrytitle>attributes</refentrytitle><manvolnum>5</manvolnum></citerefentry>
    773 </para>
    774 </refsect1>
    775 <refsect1 id="libpopt-3-note"><title>&note-tt;</title>
    776 <para>Updated by Erwann Chenede, Sun Microsystems Inc., 2003. Written by Erik
    777 W. Troan (ewt (a] redhat.com), Michael K. Johnson, and Robert Lynch.</para>
    778 </refsect1>
    779 </refentry>
    780