?? z77.html
字號(hào):
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html> <head> <title> Argument Parsing with popt </title> <meta name="GENERATOR" content= "Modular DocBook HTML Stylesheet Version 1.45"> <link rel="HOME" title="GTK+ / Gnome Application Development" href="ggad.html"> <link rel="UP" title="Gnome Application Basics" href= "cha-startup.html"> <link rel="PREVIOUS" title="Internationalization" href= "sec-i18n.html"> <link rel="NEXT" title="Saving Configuration Information" href= "z79.html"> </head> <body bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink= "#840084" alink="#0000FF"> <div class="NAVHEADER"> <table width="100%" border="0" bgcolor="#ffffff" cellpadding= "1" cellspacing="0"> <tr> <th colspan="4" align="center"> <font color="#000000" size="2">GTK+ / Gnome Application Development</font> </th> </tr> <tr> <td width="25%" bgcolor="#ffffff" align="left"> <a href="sec-i18n.html"><font color="#0000ff" size="2"> <b><<< Previous</b></font></a> </td> <td width="25%" colspan="2" bgcolor="#ffffff" align= "center"> <font color="#0000ff" size="2"><b><a href="ggad.html"> <font color="#0000ff" size="2"><b> Home</b></font></a></b></font> </td> <td width="25%" bgcolor="#ffffff" align="right"> <a href="z79.html"><font color="#0000ff" size="2"><b> Next >>></b></font></a> </td> </tr> </table> </div> <div class="SECT1"> <h1 class="SECT1"> <a name="Z77">Argument Parsing with <tt class= "APPLICATION">popt</tt></a> </h1> <p> Gnome uses a powerful option-parsing library called <tt class="APPLICATION">popt</tt>. <tt class="APPLICATION"> popt</tt> handles all the default Gnome options---to see the default options, pass the <tt class="APPLICATION"> --help</tt> option to any Gnome application. You can add a "<tt class="APPLICATION">popt</tt> table" with your custom options. To do so, replace <tt class="FUNCTION"> gnome_init()</tt> with the <tt class="FUNCTION"> gnome_init_with_popt_table()</tt> variant (<a href= "z77.html#FL-INITWITHPOPT">Figure 3</a>). </p> <div class="FIGURE"> <a name="FL-INITWITHPOPT"></a> <div class="FUNCSYNOPSIS"> <a name="FL-INITWITHPOPT.SYNOPSIS"></a> <table border="0" bgcolor="#E0E0E0" width="100%"> <tr> <td><pre class="FUNCSYNOPSISINFO">#include <libgnomeui/gnome-init.h></pre> </td> </tr> </table> <p> <code><code class="FUNCDEF">int <tt class="FUNCTION"> gnome_init_with_popt_table</tt></code>(const char* <tt class="PARAMETER"><i>app_id</i></tt>, const char* <tt class="PARAMETER"><i>app_version</i></tt>, int <tt class="PARAMETER"><i>argc</i></tt>, char** <tt class= "PARAMETER"><i>argv</i></tt>, const struct poptOption* <tt class="PARAMETER"><i>options</i></tt>, int <tt class="PARAMETER"><i>flags</i></tt>, poptContext* <tt class="PARAMETER"><i>return_ctx</i></tt>);</code> </p> </div> <p> <b>Figure 3. Init with Argument Parsing</b> </p> </div> <p> A <tt class="APPLICATION">popt</tt> table is simply an array of <span class="STRUCTNAME">struct poptOption</span>, defined as follows: </p> <table border="0" bgcolor="#E0E0E0" width="100%"> <tr> <td><pre class="PROGRAMLISTING"> struct poptOption { const char* longName; char shortName; int argInfo; void* arg; int val; char* descrip; char* argDescrip; }; </pre> </td> </tr> </table> <p> The first two components are the long and short names for the option; for example, <span class="STRUCTNAME"> "help"</span> and <span class="STRUCTNAME">'h'</span> would correspond to command-line options <tt class="APPLICATION"> --help</tt> and <tt class="APPLICATION">-h</tt>. These can be <span class="STRUCTNAME">NULL</span> and <tt class= "APPLICATION">'\0'</tt> respectively, if you want only one name for the option. </p> <p> The <span class="STRUCTNAME">arginfo</span> member tells what kind of table entry this is. Here are the possible values: </p> <ul> <li> <p> <span class="STRUCTNAME">POPT_ARG_NONE</span> means the option is a simple switch, it takes no argument. </p> </li> <li> <p> <span class="STRUCTNAME">POPT_ARG_STRING</span> means the option takes a string argument, as in <tt class= "APPLICATION">--geometry="300x300+50+100"</tt>. </p> </li> <li> <p> <span class="STRUCTNAME">POPT_ARG_INT</span> means the option takes an <span class="STRUCTNAME">int</span> argument, as in <tt class="APPLICATION"> --columns=10</tt>. </p> </li> <li> <p> <span class="STRUCTNAME">POPT_ARG_LONG</span> means the option takes a <span class="STRUCTNAME">long</span> argument. </p> </li> <li> <p> <span class="STRUCTNAME">POPT_ARG_INCLUDE_TABLE</span> means that this <span class="STRUCTNAME">struct poptOption</span> does not specify an option, but rather another <tt class="APPLICATION">popt</tt> table to be included. </p> </li> <li> <p> <span class="STRUCTNAME">POPT_ARG_CALLBACK</span> means that this <span class="STRUCTNAME">struct poptOption</span> does not specify an option, but rather a callback function to be used for parsing options in this table. This kind of entry should be at the beginning of your table. </p> </li> <li> <p> <span class="STRUCTNAME">POPT_ARG_INTL_DOMAIN</span> means that this <span class="STRUCTNAME">struct poptOption</span> specifies the translation domain for this table and any subtables. </p> </li> </ul> <p> The meaning of <span class="STRUCTNAME">arg</span> depends on the <span class="STRUCTNAME">arginfo</span> member. For options that take an argument, <span class="STRUCTNAME"> arg</span> should point to a variable of the argument type. <tt class="APPLICATION">popt</tt> will fill the pointed-to variable with the argument. For <span class="STRUCTNAME"> POPT_ARG_NONE</span>, <span class="STRUCTNAME">*arg</span> is set to <span class="STRUCTNAME">TRUE</span> if the option is found on the command line. In all cases, <span class="STRUCTNAME">arg</span> may be <span class= "STRUCTNAME">NULL</span>, causing <tt class="APPLICATION"> popt</tt> to ignore it. </p> <p> For <span class="STRUCTNAME">POPT_ARG_INCLUDE_TABLE</span>, <span class="STRUCTNAME">arg</span> points to the table to include; for <span class="STRUCTNAME"> POPT_ARG_CALLBACK</span>, it points to the callback to invoke; for <span class="STRUCTNAME"> POPT_ARG_INTL_DOMAIN</span> it should be the translation domain string. </p> <p> The <span class="STRUCTNAME">val</span> member serves as an identifier for each option. Typically it isn't that useful in Gnome applications, but if you use a callback it will be available in the callback. If you aren't going to use it, set it to <span class="STRUCTNAME">0</span>. </p> <p> The final two members are used to automatically generate output for the <tt class="APPLICATION">--help</tt> option. <span class="STRUCTNAME">descrip</span> describes an option; <span class="STRUCTNAME">argDescrip</span> describes the argument to that option, if applicable. For example, the help for the <tt class="APPLICATION"> --display</tt> option looks like this: </p> <table border="0" bgcolor="#E0E0E0" width="100%"> <tr> <td><pre class="PROGRAMLISTING"> --display=DISPLAY X display to use </pre> </td> </tr> </table> <p> Here <span class="STRUCTNAME">argDescrip</span> is "<tt class="APPLICATION">DISPLAY</tt>" and <span class= "STRUCTNAME">descrip</span> is "<tt class="APPLICATION">X display to use</tt>." Remember to mark these two strings for translation. </p> <p> <span class="STRUCTNAME">descrip</span> has a slightly different meaning for <span class="STRUCTNAME"> POPT_ARG_INCLUDE_TABLE</span>; in this case it titles a "group" of options in the help output. For example, "<tt class="APPLICATION">Help options</tt>" in the following output: </p> <table border="0" bgcolor="#E0E0E0" width="100%"> <tr> <td><pre class="PROGRAMLISTING"> Help options -?, --help Show this help message --usage Display brief usage message </pre> </td> </tr> </table> <p> If you place an entry of type <span class="STRUCTNAME"> POPT_ARG_CALLBACK</span> at the beginning of a <tt class= "APPLICATION">popt</tt> table, a user-defined callback will be invoked with information about each option found on the command line. Here is the type your callback is expected to have: </p> <table border="0" bgcolor="#E0E0E0" width="100%"> <tr> <td><pre class="PROGRAMLISTING"> typedef void (*poptCallbackType)(poptContext con, enum poptCallbackReason reason, const struct poptOption* opt, const char* arg, void* data); </pre> </td> </tr> </table> <p> The opaque <span class="STRUCTNAME">poptContext</span> object contains all of <tt class="APPLICATION">popt</tt>'s state. This makes it possible to use <tt class= "APPLICATION">popt</tt> more than once in the same program, or parse more than one set of options simultaneously. You can also extract information about the current parsing state from the <span class="STRUCTNAME">poptContext</span>, using functions provided by <tt class="APPLICATION"> popt</tt>. </p> <p> Possible <span class="STRUCTNAME">poptCallbackReason</span> values are: </p> <ul> <li> <p> <span class="STRUCTNAME"> POPT_CALLBACK_REASON_PRE</span> </p> </li> <li> <p> <span class="STRUCTNAME"> POPT_CALLBACK_REASON_POST</span> </p> </li> <li> <p> <span class="STRUCTNAME"> POPT_CALLBACK_REASON_OPTION</span> </p> </li> </ul> <p> Your callback is called once for each option found on the command line with <span class="STRUCTNAME"> POPT_CALLBACK_REASON_OPTION</span> as the <span class= "STRUCTNAME">reason</span> argument. If you request, it can also be called before and after argument parsing. In these cases <span class="STRUCTNAME">reason</span> will be <span class="STRUCTNAME">POPT_CALLBACK_REASON_PRE</span> or <span class="STRUCTNAME">POPT_CALLBACK_REASON_POST</span>. To specify that you want your callback to be called before or
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -