📄 i386-pc-mingw32-popt.3.html
字号:
abilities may be added in the future. The next option is the option that
should be aliased, and it may be either a short or a long option. The rest
of the line specifies the expansion for the alias. It is parsed similarly
to a shell command, which allows \, ", and ' to be used for quoting. If a
backslash is the final character on a line, the next line in the file
is assumed to be a logical continuation of the line containing the backslash,
just as in shell. <P>
The following entry would add a <B>--text</B> option to the grep
command, as suggested at the beginning of this section. <P>
<B>grep alias --text
-i -n -E -2</B>
<H3><A NAME="sect12" HREF="#toc12">2. Enabling Aliases</A></H3>
An application must enable alias expansion for
a <B>poptContext</B> before calling <B>poptGetNextArg()</B> for the first time. There
are three functions that define aliases for a context: <BR>
<PRE></PRE>
<DL>
<DT><B>int poptReadDefaultConfig(poptContext </B><I>con</I><B>, int </B><I>flags</I><B>);</B>This function reads
aliases from <I>/etc/popt</I> and the <I></I> </DT>
<DD><B>.popt</B> file in the user's home directory. Currently,
<I>flags</I> should be <B>NULL</B>, as it is provided only for future expansion. </DD>
</DL>
<P>
<BR>
<PRE></PRE>
<DL>
<DT><B>int poptReadConfigFile(poptContext </B><I>con</I><B>, char * </B><I>fn</I><B>);</B>The file specified by
<I>fn</I> is opened and parsed as a popt <I></I> </DT>
<DD>configuration file. This allows programs
to use program-specific configuration files. </DD>
</DL>
<P>
<BR>
<PRE></PRE>
<DL>
<DT><B>int poptAddAlias(poptContext </B><I>con</I><B>, struct poptAlias </B><I>alias</I><B>,</B><B>
int </B><I>flags</I><B>);</B>Occasionally, processes want to specify aliases without having
to </DT>
<DD>read them from a configuration file. This function adds a new alias to
a context. The <I>flags</I> argument should be 0, as it is <I></I> currently reserved
for future expansion. The new alias is specified as a <B>struct poptAlias</B>,
which is defined as: <P>
<BR>
<PRE>struct poptAlias {
const char * longName; /* may be NULL */
char shortName; /* may be '\0' */
int argc;
const char ** argv; /* must be free()able */
};
</PRE><P>
The first two elements, <I>longName</I> and <I>shortName</I>, specify <I></I> the option that
is aliased. The final two, <I>argc</I> and <I>argv</I>,<I></I> define the expansion to use when
the aliases option is encountered. </DD>
</DL>
<P>
<H2><A NAME="sect13" HREF="#toc13">Parsing Argument Strings</A></H2>
Although popt
is usually used for parsing arguments already divided into an <I>argv</I>-style
array, some programs need to parse strings that <I></I> are formatted identically
to command lines. To facilitate this, popt provides a function that parses
a string into an array of strings, using rules similiar to normal shell
parsing. <P>
<BR>
<PRE>#include <popt.h>int poptParseArgvString(char * s, int * argcPtr,
char *** argvPtr);int poptDupArgv(int argc, const char **
argv, int * argcPtr, const char *** argvPtr);</PRE><P>
</B>The
string s is parsed into an <I>argv</I>-style array. The integer <I></I> pointed to by the
<I>argcPtr</I> parameter contains the number of elements <I></I> parsed, and the final
<I>argvPtr</I> parameter contains the address of the<I></I> newly created array. The routine
<B>poptDupArgv()</B> can be used to make a copy of an existing argument array.
<P>
The <I>argvPtr</I><I></I> created by <B>poptParseArgvString()</B> or <B>poptDupArgv()</B> is suitable
to pass directly to <B>poptGetContext()</B>. Both routines return a single dynamically
allocated contiguous block of storage and should be <B>free()</B>ed when the application
is finished with the storage.
<H2><A NAME="sect14" HREF="#toc14">Handling Extra Arguments</A></H2>
Some applications
implement the equivalent of option aliasing but need to do so through special
logic. The <B>poptStuffArgs()</B> function allows an application to insert new
arguments into the current <B>poptContext</B>. <P>
<BR>
<PRE>#include <popt.h>int poptStuffArgs(poptContext con, const char ** argv);</PRE><P>
</B>The
passed <I>argv</I><I></I> must have a <B>NULL</B> pointer as its final element. When <B>poptGetNextOpt()</B>
is next called, the "stuffed" arguments are the first to be parsed. popt
returns to the normal arguments once all the stuffed arguments have been
exhausted.
<H2><A NAME="sect15" HREF="#toc15">Example</A></H2>
The following example is a simplified version of the program
"robin" which appears in Chapter 15 of the text cited below. Robin has
been stripped of everything but its argument-parsing logic, slightly reworked,
and renamed "parse." It may prove useful in illustrating at least some
of the features of the extremely rich popt library. <P>
<BR>
<PRE>#include <popt.h>
#include <stdio.h>
void usage(poptContext optCon, int exitcode, char *error, char *addl) {
poptPrintUsage(optCon, stderr, 0);
if (error) fprintf(stderr, "%s: %s0, error, addl);
exit(exitcode);
}
int main(int argc, char *argv[]) {
char c; /* used for argument parsing */
int i = 0; /* used for tracking options */
char *portname;
int speed = 0; /* used in argument parsing to set speed */
int raw = 0; /* raw mode? */
int j;
char buf[BUFSIZ+1];
poptContext optCon; /* context for parsing command-line options */
struct poptOption optionsTable[] = {
<tt> </tt> <tt> </tt> { "bps", 'b', POPT_ARG_INT, &speed, 0,
<tt> </tt> <tt> </tt> <tt> </tt> <tt> </tt> "signaling rate in bits-per-second", "BPS" },
<tt> </tt> <tt> </tt> { "crnl", 'c', 0, 0, 'c',
<tt> </tt> <tt> </tt> <tt> </tt> <tt> </tt> "expand cr characters to cr/lf sequences" },
<tt> </tt> <tt> </tt> { "hwflow", 'h', 0, 0, 'h',
<tt> </tt> <tt> </tt> <tt> </tt> <tt> </tt> "use hardware (RTS/CTS) flow control" },
<tt> </tt> <tt> </tt> { "noflow", 'n', 0, 0, 'n',
<tt> </tt> <tt> </tt> <tt> </tt> <tt> </tt> "use no flow control" },
<tt> </tt> <tt> </tt> { "raw", 'r', 0, &raw, 0,
<tt> </tt> <tt> </tt> <tt> </tt> <tt> </tt> "don't perform any character conversions" },
<tt> </tt> <tt> </tt> { "swflow", 's', 0, 0, 's',
<tt> </tt> <tt> </tt> <tt> </tt> <tt> </tt> "use software (XON/XOF) flow control" } ,
<tt> </tt> <tt> </tt> POPT_AUTOHELP
<tt> </tt> <tt> </tt> { NULL, 0, 0, NULL, 0 }
};
optCon = poptGetContext(NULL, argc, argv, optionsTable, 0);
poptSetOtherOptionHelp(optCon, "[OPTIONS]* <port>");
if (argc < 2) {
<tt> </tt> <tt> </tt> poptPrintUsage(optCon, stderr, 0);
<tt> </tt> <tt> </tt> exit(1);
}
/* Now do options processing, get portname */
while ((c = poptGetNextOpt(optCon)) >= 0) {
switch (c) {
case 'c':
buf[i++] = 'c';
break;
case 'h':
buf[i++] = 'h';
break;
case 's':
buf[i++] = 's';
break;
case 'n':
buf[i++] = 'n';
break;
}
}
portname = poptGetArg(optCon);
if((portname == NULL) || !(poptPeekArg(optCon) == NULL))
usage(optCon, 1, "Specify a single port", ".e.g., /dev/cua0");
if (c < -1) {
/* an error occurred during option processing */
fprintf(stderr, "%s: %s\n",
poptBadOption(optCon, POPT_BADOPTION_NOALIAS),
poptStrerror(c));
return 1;
}
/* Print out options, portname chosen */
printf("Options chosen: ");
for(j = 0; j < i ; j++)
printf("-%c ", buf[j]);
if(raw) printf("-r ");
if(speed) printf("-b %d ", speed);
printf("\nPortname chosen: %s\n", portname);
poptFreeContext(optCon);
exit(0);
}
</PRE><P>
RPM, a popular Linux package management program, makes heavy use of popt's
features. Many of its command-line arguments are implemented through popt
aliases, which makes RPM an excellent example of how to take advantage
of the popt library. For more information on RPM, see <A HREF="http://www.rpm.org.">http://www.rpm.org.</A>
The
popt source code distribution includes test program(s) which use all of
the features of the popt libraries in various ways. If a feature isn't working
for you, the popt test code is the first place to look.
<H2><A NAME="sect16" HREF="#toc16">Bugs</A></H2>
None presently
known.
<H2><A NAME="sect17" HREF="#toc17">Author</A></H2>
Erik W. Troan <ewt@redhat.com> <P>
This man page is derived in part
from <I>Linux Application Development</I> by Michael K. Johnson and Erik W. Troan,
Copyright (c) 1998 by Addison Wesley Longman, Inc., and included in the
popt documentation with the permission of the Publisher and the appreciation
of the Authors. <P>
Thanks to Robert Lynch for his extensive work on this man
page.
<H2><A NAME="sect18" HREF="#toc18">See Also</A></H2>
<B><A HREF="getopt.3.html">getopt</B>(3)</A>
<P>
<I>Linux Application Development</I>, by Michael K. Johnson
and Erik W. Troan (Addison-Wesley, 1998; ISBN 0-201-30821-5), Chapter 24. <P>
<B>popt.ps</B>
is a Postscript version of the above cited book chapter. It can be found
in the source archive for popt available at: ftp://ftp.rpm.org/pub/rpm. <P>
<HR><P>
<A NAME="toc"><B>Table of Contents</B></A><P>
<UL>
<LI><A NAME="toc0" HREF="#sect0">Name</A></LI>
<LI><A NAME="toc1" HREF="#sect1">Synopsis</A></LI>
<LI><A NAME="toc2" HREF="#sect2">Description</A></LI>
<LI><A NAME="toc3" HREF="#sect3">Basic Popt Usage</A></LI>
<UL>
<LI><A NAME="toc4" HREF="#sect4">1. the Option Table</A></LI>
<LI><A NAME="toc5" HREF="#sect5">2. Creating a Context</A></LI>
<LI><A NAME="toc6" HREF="#sect6">3. Parsing the Command Line</A></LI>
<LI><A NAME="toc7" HREF="#sect7">4. Leftover Arguments</A></LI>
<LI><A NAME="toc8" HREF="#sect8">5. Automatic Help Messages</A></LI>
</UL>
<LI><A NAME="toc9" HREF="#sect9">Error Handling</A></LI>
<LI><A NAME="toc10" HREF="#sect10">Option Aliasing</A></LI>
<UL>
<LI><A NAME="toc11" HREF="#sect11">1. Specifying Aliases</A></LI>
<LI><A NAME="toc12" HREF="#sect12">2. Enabling Aliases</A></LI>
</UL>
<LI><A NAME="toc13" HREF="#sect13">Parsing Argument Strings</A></LI>
<LI><A NAME="toc14" HREF="#sect14">Handling Extra Arguments</A></LI>
<LI><A NAME="toc15" HREF="#sect15">Example</A></LI>
<LI><A NAME="toc16" HREF="#sect16">Bugs</A></LI>
<LI><A NAME="toc17" HREF="#sect17">Author</A></LI>
<LI><A NAME="toc18" HREF="#sect18">See Also</A></LI>
</UL>
</BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -