⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 glib-commandline-option-parser.html

📁 glid编写实例
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<html xmlns:cf="http://docbook.sourceforge.net/xmlns/chunkfast/1.0"><head><meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"><title>Commandline option parser</title><meta name="generator" content="DocBook XSL Stylesheets V1.69.0"><link rel="start" href="index.html" title="GLib Reference Manual"><link rel="up" href="glib-utilities.html" title="GLib Utilities"><link rel="prev" href="glib-Shell-related-Utilities.html" title="Shell-related Utilities"><link rel="next" href="glib-Glob-style-pattern-matching.html" title="Glob-style pattern matching"><meta name="generator" content="GTK-Doc V1.4 (XML mode)"><link rel="stylesheet" href="style.css" type="text/css"><link rel="chapter" href="glib.html" title="GLib Overview"><link rel="chapter" href="glib-fundamentals.html" title="GLib Fundamentals"><link rel="chapter" href="glib-core.html" title="GLib Core Application Support"><link rel="chapter" href="glib-utilities.html" title="GLib Utilities"><link rel="chapter" href="glib-data-types.html" title="GLib Data Types"><link rel="chapter" href="tools.html" title="GLib Tools"><link rel="index" href="ix01.html" title="Index"><link rel="index" href="ix02.html" title="Index of deprecated symbols"><link rel="index" href="ix03.html" title="Index of new symbols in 2.2"><link rel="index" href="ix04.html" title="Index of new symbols in 2.4"><link rel="index" href="ix05.html" title="Index of new symbols in 2.6"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><table class="navigation" width="100%" summary="Navigation header" cellpadding="2" cellspacing="2"><tr valign="middle"><td><a accesskey="p" href="glib-Shell-related-Utilities.html"><img src="left.png" width="24" height="24" border="0" alt="Prev"></a></td><td><a accesskey="u" href="glib-utilities.html"><img src="up.png" width="24" height="24" border="0" alt="Up"></a></td><td><a accesskey="h" href="index.html"><img src="home.png" width="24" height="24" border="0" alt="Home"></a></td><th width="100%" align="center">GLib Reference Manual</th><td><a accesskey="n" href="glib-Glob-style-pattern-matching.html"><img src="right.png" width="24" height="24" border="0" alt="Next"></a></td></tr></table><div class="refentry" lang="en"><a name="glib-Commandline-option-parser"></a><div class="titlepage"></div><div class="refnamediv"><table width="100%"><tr><td valign="top"><h2><span class="refentrytitle">Commandline option parser</span></h2><p>Commandline option parser &#8212; parses commandline options</p></td><td valign="top" align="right"></td></tr></table></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">#include &lt;glib.h&gt;enum        <a href="glib-Commandline-option-parser.html#GOptionError">GOptionError</a>;#define     <a href="glib-Commandline-option-parser.html#G-OPTION-ERROR:CAPS">G_OPTION_ERROR</a><a href="glib-Basic-Types.html#gboolean">gboolean</a>    (<a href="glib-Commandline-option-parser.html#GOptionArgFunc">*GOptionArgFunc</a>)               (const <a href="glib-Basic-Types.html#gchar">gchar</a> *option_name,                                             const <a href="glib-Basic-Types.html#gchar">gchar</a> *value,                                             <a href="glib-Basic-Types.html#gpointer">gpointer</a> data,                                             <a href="glib-Error-Reporting.html#GError">GError</a> **error);            <a href="glib-Commandline-option-parser.html#GOptionContext">GOptionContext</a>;<a href="glib-Commandline-option-parser.html#GOptionContext">GOptionContext</a>* <a href="glib-Commandline-option-parser.html#g-option-context-new">g_option_context_new</a>        (const <a href="glib-Basic-Types.html#gchar">gchar</a> *parameter_string);void        <a href="glib-Commandline-option-parser.html#g-option-context-free">g_option_context_free</a>           (<a href="glib-Commandline-option-parser.html#GOptionContext">GOptionContext</a> *context);<a href="glib-Basic-Types.html#gboolean">gboolean</a>    <a href="glib-Commandline-option-parser.html#g-option-context-parse">g_option_context_parse</a>          (<a href="glib-Commandline-option-parser.html#GOptionContext">GOptionContext</a> *context,                                             <a href="glib-Basic-Types.html#gint">gint</a> *argc,                                             <a href="glib-Basic-Types.html#gchar">gchar</a> ***argv,                                             <a href="glib-Error-Reporting.html#GError">GError</a> **error);void        <a href="glib-Commandline-option-parser.html#g-option-context-set-help-enabled">g_option_context_set_help_enabled</a>                                            (<a href="glib-Commandline-option-parser.html#GOptionContext">GOptionContext</a> *context,                                             <a href="glib-Basic-Types.html#gboolean">gboolean</a> help_enabled);<a href="glib-Basic-Types.html#gboolean">gboolean</a>    <a href="glib-Commandline-option-parser.html#g-option-context-get-help-enabled">g_option_context_get_help_enabled</a>                                            (<a href="glib-Commandline-option-parser.html#GOptionContext">GOptionContext</a> *context);void        <a href="glib-Commandline-option-parser.html#g-option-context-set-ignore-unknown-options">g_option_context_set_ignore_unknown_options</a>                                            (<a href="glib-Commandline-option-parser.html#GOptionContext">GOptionContext</a> *context,                                             <a href="glib-Basic-Types.html#gboolean">gboolean</a> ignore_unknown);<a href="glib-Basic-Types.html#gboolean">gboolean</a>    <a href="glib-Commandline-option-parser.html#g-option-context-get-ignore-unknown-options">g_option_context_get_ignore_unknown_options</a>                                            (<a href="glib-Commandline-option-parser.html#GOptionContext">GOptionContext</a> *context);enum        <a href="glib-Commandline-option-parser.html#GOptionArg">GOptionArg</a>;enum        <a href="glib-Commandline-option-parser.html#GOptionFlags">GOptionFlags</a>;#define     <a href="glib-Commandline-option-parser.html#G-OPTION-REMAINING:CAPS">G_OPTION_REMAINING</a>            <a href="glib-Commandline-option-parser.html#GOptionEntry">GOptionEntry</a>;void        <a href="glib-Commandline-option-parser.html#g-option-context-add-main-entries">g_option_context_add_main_entries</a>                                            (<a href="glib-Commandline-option-parser.html#GOptionContext">GOptionContext</a> *context,                                             const <a href="glib-Commandline-option-parser.html#GOptionEntry">GOptionEntry</a> *entries,                                             const <a href="glib-Basic-Types.html#gchar">gchar</a> *translation_domain);            <a href="glib-Commandline-option-parser.html#GOptionGroup">GOptionGroup</a>;void        <a href="glib-Commandline-option-parser.html#g-option-context-add-group">g_option_context_add_group</a>      (<a href="glib-Commandline-option-parser.html#GOptionContext">GOptionContext</a> *context,                                             <a href="glib-Commandline-option-parser.html#GOptionGroup">GOptionGroup</a> *group);void        <a href="glib-Commandline-option-parser.html#g-option-context-set-main-group">g_option_context_set_main_group</a> (<a href="glib-Commandline-option-parser.html#GOptionContext">GOptionContext</a> *context,                                             <a href="glib-Commandline-option-parser.html#GOptionGroup">GOptionGroup</a> *group);<a href="glib-Commandline-option-parser.html#GOptionGroup">GOptionGroup</a>* <a href="glib-Commandline-option-parser.html#g-option-context-get-main-group">g_option_context_get_main_group</a>                                            (<a href="glib-Commandline-option-parser.html#GOptionContext">GOptionContext</a> *context);<a href="glib-Commandline-option-parser.html#GOptionGroup">GOptionGroup</a>* <a href="glib-Commandline-option-parser.html#g-option-group-new">g_option_group_new</a>            (const <a href="glib-Basic-Types.html#gchar">gchar</a> *name,                                             const <a href="glib-Basic-Types.html#gchar">gchar</a> *description,                                             const <a href="glib-Basic-Types.html#gchar">gchar</a> *help_description,                                             <a href="glib-Basic-Types.html#gpointer">gpointer</a> user_data,                                             <a href="glib-Datasets.html#GDestroyNotify">GDestroyNotify</a> destroy);void        <a href="glib-Commandline-option-parser.html#g-option-group-free">g_option_group_free</a>             (<a href="glib-Commandline-option-parser.html#GOptionGroup">GOptionGroup</a> *group);void        <a href="glib-Commandline-option-parser.html#g-option-group-add-entries">g_option_group_add_entries</a>      (<a href="glib-Commandline-option-parser.html#GOptionGroup">GOptionGroup</a> *group,                                             const <a href="glib-Commandline-option-parser.html#GOptionEntry">GOptionEntry</a> *entries);<a href="glib-Basic-Types.html#gboolean">gboolean</a>    (<a href="glib-Commandline-option-parser.html#GOptionParseFunc">*GOptionParseFunc</a>)             (<a href="glib-Commandline-option-parser.html#GOptionContext">GOptionContext</a> *context,                                             <a href="glib-Commandline-option-parser.html#GOptionGroup">GOptionGroup</a> *group,                                             <a href="glib-Basic-Types.html#gpointer">gpointer</a> data,                                             <a href="glib-Error-Reporting.html#GError">GError</a> **error);void        <a href="glib-Commandline-option-parser.html#g-option-group-set-parse-hooks">g_option_group_set_parse_hooks</a>  (<a href="glib-Commandline-option-parser.html#GOptionGroup">GOptionGroup</a> *group,                                             <a href="glib-Commandline-option-parser.html#GOptionParseFunc">GOptionParseFunc</a> pre_parse_func,                                             <a href="glib-Commandline-option-parser.html#GOptionParseFunc">GOptionParseFunc</a> post_parse_func);void        (<a href="glib-Commandline-option-parser.html#GOptionErrorFunc">*GOptionErrorFunc</a>)             (<a href="glib-Commandline-option-parser.html#GOptionContext">GOptionContext</a> *context,                                             <a href="glib-Commandline-option-parser.html#GOptionGroup">GOptionGroup</a> *group,                                             <a href="glib-Basic-Types.html#gpointer">gpointer</a> data,                                             <a href="glib-Error-Reporting.html#GError">GError</a> **error);void        <a href="glib-Commandline-option-parser.html#g-option-group-set-error-hook">g_option_group_set_error_hook</a>   (<a href="glib-Commandline-option-parser.html#GOptionGroup">GOptionGroup</a> *group,                                             <a href="glib-Commandline-option-parser.html#GOptionErrorFunc">GOptionErrorFunc</a> error_func);const <a href="glib-Basic-Types.html#gchar">gchar</a>* (<a href="glib-Commandline-option-parser.html#GTranslateFunc">*GTranslateFunc</a>)              (const <a href="glib-Basic-Types.html#gchar">gchar</a> *str,                                             <a href="glib-Basic-Types.html#gpointer">gpointer</a> data);void        <a href="glib-Commandline-option-parser.html#g-option-group-set-translate-func">g_option_group_set_translate_func</a>                                            (<a href="glib-Commandline-option-parser.html#GOptionGroup">GOptionGroup</a> *group,                                             <a href="glib-Commandline-option-parser.html#GTranslateFunc">GTranslateFunc</a> func,                                             <a href="glib-Basic-Types.html#gpointer">gpointer</a> data,                                             <a href="glib-Datasets.html#GDestroyNotify">GDestroyNotify</a> destroy_notify);void        <a href="glib-Commandline-option-parser.html#g-option-group-set-translation-domain">g_option_group_set_translation_domain</a>                                            (<a href="glib-Commandline-option-parser.html#GOptionGroup">GOptionGroup</a> *group,                                             const <a href="glib-Basic-Types.html#gchar">gchar</a> *domain);</pre></div><div class="refsect1" lang="en"><a name="id3063223"></a><h2>Description</h2><p>The GOption commandline parser is intended to be a simpler replacement for thepopt library. It supports short and long commandline options, as shown in the following example:</p><p><code class="literal">testtreemodel -r 1 --max-size 20 --rand --display=:1.0 -vb -- file1 file2</code></p><p>The example demonstrates a number of features of the GOption commandline parser</p><div class="itemizedlist"><ul type="disc"><li><p>  Options can be single letters, prefixed by a single dash. Multiple  short options can be grouped behind a single dash.</p></li><li><p>  Long options are prefixed by two consecutive dashes.</p></li><li><p>  Options can have an extra argument, which can be a number, a string or a filename.  For long options, the extra argument can be appended with an equals sign after the  option name.</p></li><li><p>  Non-option arguments are returned to the application as rest arguments.</p></li><li><p>  An argument consisting solely of two dashes turns off further parsing, any remaining  arguments (even those starting with a dash) are returned to the application as rest   arguments.</p></li></ul></div><p></p><p>Another important feature of GOption is that it can automatically generate nicelyformatted help output. Unless it is explicitly turned off with <a href="glib-Commandline-option-parser.html#g-option-context-set-help-enabled"><code class="function">g_option_context_set_help_enabled()</code></a>, GOption will recognize the <code class="option">--help</code>, <code class="option">-?</code>, <code class="option">--help-all</code>and <code class="option">--help-</code><em class="replaceable"><code>groupname</code></em> options (where <em class="replaceable"><code>groupname</code></em> is the name of a <a href="glib-Commandline-option-parser.html#GOptionGroup"><span class="type">GOptionGroup</span></a>) and write a text similar to the one shown in the following example to stdout.</p><div class="informalexample"><pre class="screen">Usage:  testtreemodel [OPTION...] - test tree model performanceHelp Options:  -?, --help               Show help options  --help-all               Show all help options  --help-gtk               Show GTK+ OptionsApplication Options:  -r, --repeats=N          Average over N repetitions  -m, --max-size=M         Test up to 2^M items  --display=DISPLAY        X display to use  -v, --verbose            Be verbose  -b, --beep               Beep when done     --rand                   Randomize the data</pre></div><p>GOption groups options in <a href="glib-Commandline-option-parser.html#GOptionGroup"><span class="type">GOptionGroup</span></a>s, which makes it easy toincorporate options from multiple sources. The intended use for this isto let applications collect option groups from the libraries it uses,add them to their <a href="glib-Commandline-option-parser.html#GOptionContext"><span class="type">GOptionContext</span></a>, and parse all options by a single callto <a href="glib-Commandline-option-parser.html#g-option-context-parse"><code class="function">g_option_context_parse()</code></a>. See <ahref="../gtk/gtk-General.html#gtk-get-option-group"><code class="function">gtk_get_option_group()</code></a> for an example.</p><p>If an option is declared to be of type string or filename, GOption takescare of converting it to the right encoding; strings are returned in UTF-8,filenames are returned in the GLib filename encoding.</p><p>Here is a complete example of setting up GOption to parse the examplecommandline above and produce the example help output.</p><div class="informalexample"><pre class="programlisting">static gint repeats = 2;static gint max_size = 8;static gboolean verbose = FALSE;static gboolean beep = FALSE;static gboolean rand = FALSE;static GOptionEntry entries[] = {  { "repeats", 'r', 0, G_OPTION_ARG_INT, &amp;repeats, "Average over N repetitions", "N" },  { "max-size", 'm', 0, G_OPTION_ARG_INT, &amp;max_size, "Test up to 2^M items", "M" },  { "verbose", 'v', 0, G_OPTION_ARG_NONE, &amp;verbose, "Be verbose", NULL },  { "beep", 'b', 0, G_OPTION_ARG_NONE, &amp;beep, "Beep when done", NULL },  { "rand", 0, 0, G_OPTION_ARG_NONE, &amp;rand, "Randomize the data", NULL },  { NULL }};int main (int argc, char *argv[]){  GError *error = NULL;  context = g_option_context_new ("- test tree model performance");  g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);  g_option_context_add_group (context, gtk_get_option_group (TRUE));  g_option_context_parse (context, &amp;argc, &amp;argv, &amp;error);    /* ... */}</pre></div></div><div class="refsect1" lang="en"><a name="id3063435"></a><h2>Details</h2><div class="refsect2" lang="en"><a name="id3063440"></a><h3><a name="GOptionError"></a>enum GOptionError</h3><a class="indexterm" name="id3063451"></a><pre class="programlisting">typedef enum{  G_OPTION_ERROR_UNKNOWN_OPTION,  G_OPTION_ERROR_BAD_VALUE,  G_OPTION_ERROR_FAILED} GOptionError;</pre><p>Error codes returned by option parsing.</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><code class="literal">G_OPTION_ERROR_UNKNOWN_OPTION</code></span></td><td>An option was not known to the parser.  This error will only be reported, if the parser hasn't been instructed  to ignore unknown options, see <a href="glib-Commandline-option-parser.html#g-option-context-set-ignore-unknown-options"><code class="function">g_option_context_set_ignore_unknown_options()</code></a>.</td></tr><tr><td><span class="term"><code class="literal">G_OPTION_ERROR_BAD_VALUE</code></span></td><td>A value couldn't be parsed.</td></tr><tr><td><span class="term"><code class="literal">G_OPTION_ERROR_FAILED</code></span></td><td>A <a href="glib-Commandline-option-parser.html#GOptionArgFunc"><span class="type">GOptionArgFunc</span></a> callback failed.</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3063541"></a><h3><a name="G-OPTION-ERROR:CAPS"></a>G_OPTION_ERROR</h3><a class="indexterm" name="id3063552"></a><pre class="programlisting">#define G_OPTION_ERROR (g_option_error_quark ())</pre>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -