?? glib-commandline-option-parser.html
字號:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><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.73.2"><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.9 (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"><link rel="index" href="ix06.html" title="Index of new symbols in 2.8"><link rel="index" href="ix07.html" title="Index of new symbols in 2.10"><link rel="index" href="ix08.html" title="Index of new symbols in 2.12"><link rel="index" href="ix09.html" title="Index of new symbols in 2.14"><link rel="index" href="ix10.html" title="Index of new symbols in 2.16"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><table class="navigation" id="top" 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><tr><td colspan="5" class="shortcuts"><nobr><a href="#id3125502" class="shortcut">Top</a>  |  <a href="#id3126579" class="shortcut">Description</a></nobr></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><a name="id3125502"></a><span class="refentrytitle">Commandline option parser</span></h2><p>Commandline option parser — parses commandline options</p></td><td valign="top" align="right"></td></tr></table></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">#include <glib.h>enum <a class="link" href="glib-Commandline-option-parser.html#GOptionError">GOptionError</a>;#define <a class="link" href="glib-Commandline-option-parser.html#G-OPTION-ERROR:CAPS">G_OPTION_ERROR</a><a class="link" href="glib-Basic-Types.html#gboolean">gboolean</a> (<a class="link" href="glib-Commandline-option-parser.html#GOptionArgFunc">*GOptionArgFunc</a>) (const <a class="link" href="glib-Basic-Types.html#gchar">gchar</a> *option_name, const <a class="link" href="glib-Basic-Types.html#gchar">gchar</a> *value, <a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> data, <a class="link" href="glib-Error-Reporting.html#GError">GError</a> **error); <a class="link" href="glib-Commandline-option-parser.html#GOptionContext">GOptionContext</a>;<a class="link" href="glib-Commandline-option-parser.html#GOptionContext">GOptionContext</a>* <a class="link" href="glib-Commandline-option-parser.html#g-option-context-new">g_option_context_new</a> (const <a class="link" href="glib-Basic-Types.html#gchar">gchar</a> *parameter_string);void <a class="link" href="glib-Commandline-option-parser.html#g-option-context-set-summary">g_option_context_set_summary</a> (<a class="link" href="glib-Commandline-option-parser.html#GOptionContext">GOptionContext</a> *context, const <a class="link" href="glib-Basic-Types.html#gchar">gchar</a> *summary);const <a class="link" href="glib-Basic-Types.html#gchar">gchar</a>* <a class="link" href="glib-Commandline-option-parser.html#g-option-context-get-summary">g_option_context_get_summary</a> (<a class="link" href="glib-Commandline-option-parser.html#GOptionContext">GOptionContext</a> *context);void <a class="link" href="glib-Commandline-option-parser.html#g-option-context-set-description">g_option_context_set_description</a> (<a class="link" href="glib-Commandline-option-parser.html#GOptionContext">GOptionContext</a> *context, const <a class="link" href="glib-Basic-Types.html#gchar">gchar</a> *description);const <a class="link" href="glib-Basic-Types.html#gchar">gchar</a>* <a class="link" href="glib-Commandline-option-parser.html#g-option-context-get-description">g_option_context_get_description</a> (<a class="link" href="glib-Commandline-option-parser.html#GOptionContext">GOptionContext</a> *context);const <a class="link" href="glib-Basic-Types.html#gchar">gchar</a>* (<a class="link" href="glib-Commandline-option-parser.html#GTranslateFunc">*GTranslateFunc</a>) (const <a class="link" href="glib-Basic-Types.html#gchar">gchar</a> *str, <a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> data);void <a class="link" href="glib-Commandline-option-parser.html#g-option-context-set-translate-func">g_option_context_set_translate_func</a> (<a class="link" href="glib-Commandline-option-parser.html#GOptionContext">GOptionContext</a> *context, <a class="link" href="glib-Commandline-option-parser.html#GTranslateFunc">GTranslateFunc</a> func, <a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> data, <a class="link" href="glib-Datasets.html#GDestroyNotify">GDestroyNotify</a> destroy_notify);void <a class="link" href="glib-Commandline-option-parser.html#g-option-context-set-translation-domain">g_option_context_set_translation_domain</a> (<a class="link" href="glib-Commandline-option-parser.html#GOptionContext">GOptionContext</a> *context, const <a class="link" href="glib-Basic-Types.html#gchar">gchar</a> *domain);void <a class="link" href="glib-Commandline-option-parser.html#g-option-context-free">g_option_context_free</a> (<a class="link" href="glib-Commandline-option-parser.html#GOptionContext">GOptionContext</a> *context);<a class="link" href="glib-Basic-Types.html#gboolean">gboolean</a> <a class="link" href="glib-Commandline-option-parser.html#g-option-context-parse">g_option_context_parse</a> (<a class="link" href="glib-Commandline-option-parser.html#GOptionContext">GOptionContext</a> *context, <a class="link" href="glib-Basic-Types.html#gint">gint</a> *argc, <a class="link" href="glib-Basic-Types.html#gchar">gchar</a> ***argv, <a class="link" href="glib-Error-Reporting.html#GError">GError</a> **error);void <a class="link" href="glib-Commandline-option-parser.html#g-option-context-set-help-enabled">g_option_context_set_help_enabled</a> (<a class="link" href="glib-Commandline-option-parser.html#GOptionContext">GOptionContext</a> *context, <a class="link" href="glib-Basic-Types.html#gboolean">gboolean</a> help_enabled);<a class="link" href="glib-Basic-Types.html#gboolean">gboolean</a> <a class="link" href="glib-Commandline-option-parser.html#g-option-context-get-help-enabled">g_option_context_get_help_enabled</a> (<a class="link" href="glib-Commandline-option-parser.html#GOptionContext">GOptionContext</a> *context);void <a class="link" href="glib-Commandline-option-parser.html#g-option-context-set-ignore-unknown-options">g_option_context_set_ignore_unknown_options</a> (<a class="link" href="glib-Commandline-option-parser.html#GOptionContext">GOptionContext</a> *context, <a class="link" href="glib-Basic-Types.html#gboolean">gboolean</a> ignore_unknown);<a class="link" href="glib-Basic-Types.html#gboolean">gboolean</a> <a class="link" href="glib-Commandline-option-parser.html#g-option-context-get-ignore-unknown-options">g_option_context_get_ignore_unknown_options</a> (<a class="link" href="glib-Commandline-option-parser.html#GOptionContext">GOptionContext</a> *context);<a class="link" href="glib-Basic-Types.html#gchar">gchar</a>* <a class="link" href="glib-Commandline-option-parser.html#g-option-context-get-help">g_option_context_get_help</a> (<a class="link" href="glib-Commandline-option-parser.html#GOptionContext">GOptionContext</a> *context, <a class="link" href="glib-Basic-Types.html#gboolean">gboolean</a> main_help, <a class="link" href="glib-Commandline-option-parser.html#GOptionGroup">GOptionGroup</a> *group);enum <a class="link" href="glib-Commandline-option-parser.html#GOptionArg">GOptionArg</a>;enum <a class="link" href="glib-Commandline-option-parser.html#GOptionFlags">GOptionFlags</a>;#define <a class="link" href="glib-Commandline-option-parser.html#G-OPTION-REMAINING:CAPS">G_OPTION_REMAINING</a> <a class="link" href="glib-Commandline-option-parser.html#GOptionEntry">GOptionEntry</a>;void <a class="link" href="glib-Commandline-option-parser.html#g-option-context-add-main-entries">g_option_context_add_main_entries</a> (<a class="link" href="glib-Commandline-option-parser.html#GOptionContext">GOptionContext</a> *context, const <a class="link" href="glib-Commandline-option-parser.html#GOptionEntry">GOptionEntry</a> *entries, const <a class="link" href="glib-Basic-Types.html#gchar">gchar</a> *translation_domain); <a class="link" href="glib-Commandline-option-parser.html#GOptionGroup">GOptionGroup</a>;void <a class="link" href="glib-Commandline-option-parser.html#g-option-context-add-group">g_option_context_add_group</a> (<a class="link" href="glib-Commandline-option-parser.html#GOptionContext">GOptionContext</a> *context, <a class="link" href="glib-Commandline-option-parser.html#GOptionGroup">GOptionGroup</a> *group);void <a class="link" href="glib-Commandline-option-parser.html#g-option-context-set-main-group">g_option_context_set_main_group</a> (<a class="link" href="glib-Commandline-option-parser.html#GOptionContext">GOptionContext</a> *context, <a class="link" href="glib-Commandline-option-parser.html#GOptionGroup">GOptionGroup</a> *group);<a class="link" href="glib-Commandline-option-parser.html#GOptionGroup">GOptionGroup</a>* <a class="link" href="glib-Commandline-option-parser.html#g-option-context-get-main-group">g_option_context_get_main_group</a> (<a class="link" href="glib-Commandline-option-parser.html#GOptionContext">GOptionContext</a> *context);<a class="link" href="glib-Commandline-option-parser.html#GOptionGroup">GOptionGroup</a>* <a class="link" href="glib-Commandline-option-parser.html#g-option-group-new">g_option_group_new</a> (const <a class="link" href="glib-Basic-Types.html#gchar">gchar</a> *name, const <a class="link" href="glib-Basic-Types.html#gchar">gchar</a> *description, const <a class="link" href="glib-Basic-Types.html#gchar">gchar</a> *help_description, <a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> user_data, <a class="link" href="glib-Datasets.html#GDestroyNotify">GDestroyNotify</a> destroy);void <a class="link" href="glib-Commandline-option-parser.html#g-option-group-free">g_option_group_free</a> (<a class="link" href="glib-Commandline-option-parser.html#GOptionGroup">GOptionGroup</a> *group);void <a class="link" href="glib-Commandline-option-parser.html#g-option-group-add-entries">g_option_group_add_entries</a> (<a class="link" href="glib-Commandline-option-parser.html#GOptionGroup">GOptionGroup</a> *group, const <a class="link" href="glib-Commandline-option-parser.html#GOptionEntry">GOptionEntry</a> *entries);<a class="link" href="glib-Basic-Types.html#gboolean">gboolean</a> (<a class="link" href="glib-Commandline-option-parser.html#GOptionParseFunc">*GOptionParseFunc</a>) (<a class="link" href="glib-Commandline-option-parser.html#GOptionContext">GOptionContext</a> *context, <a class="link" href="glib-Commandline-option-parser.html#GOptionGroup">GOptionGroup</a> *group, <a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> data, <a class="link" href="glib-Error-Reporting.html#GError">GError</a> **error);void <a class="link" href="glib-Commandline-option-parser.html#g-option-group-set-parse-hooks">g_option_group_set_parse_hooks</a> (<a class="link" href="glib-Commandline-option-parser.html#GOptionGroup">GOptionGroup</a> *group, <a class="link" href="glib-Commandline-option-parser.html#GOptionParseFunc">GOptionParseFunc</a> pre_parse_func, <a class="link" href="glib-Commandline-option-parser.html#GOptionParseFunc">GOptionParseFunc</a> post_parse_func);void (<a class="link" href="glib-Commandline-option-parser.html#GOptionErrorFunc">*GOptionErrorFunc</a>) (<a class="link" href="glib-Commandline-option-parser.html#GOptionContext">GOptionContext</a> *context, <a class="link" href="glib-Commandline-option-parser.html#GOptionGroup">GOptionGroup</a> *group, <a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> data, <a class="link" href="glib-Error-Reporting.html#GError">GError</a> **error);void <a class="link" href="glib-Commandline-option-parser.html#g-option-group-set-error-hook">g_option_group_set_error_hook</a> (<a class="link" href="glib-Commandline-option-parser.html#GOptionGroup">GOptionGroup</a> *group, <a class="link" href="glib-Commandline-option-parser.html#GOptionErrorFunc">GOptionErrorFunc</a> error_func);void <a class="link" href="glib-Commandline-option-parser.html#g-option-group-set-translate-func">g_option_group_set_translate_func</a> (<a class="link" href="glib-Commandline-option-parser.html#GOptionGroup">GOptionGroup</a> *group, <a class="link" href="glib-Commandline-option-parser.html#GTranslateFunc">GTranslateFunc</a> func, <a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> data, <a class="link" href="glib-Datasets.html#GDestroyNotify">GDestroyNotify</a> destroy_notify);void <a class="link" href="glib-Commandline-option-parser.html#g-option-group-set-translation-domain">g_option_group_set_translation_domain</a> (<a class="link" href="glib-Commandline-option-parser.html#GOptionGroup">GOptionGroup</a> *group, const <a class="link" href="glib-Basic-Types.html#gchar">gchar</a> *domain);</pre></div><div class="refsect1" lang="en"><a name="id3126579"></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 nicely formatted help output. Unless it is explicitly turned off with <a class="link" 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 class="link" 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 class="link" 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 class="link" href="glib-Commandline-option-parser.html#GOptionContext"><span class="type">GOptionContext</span></a>, and parse all options by a single callto <a class="link" href="glib-Commandline-option-parser.html#g-option-context-parse"><code class="function">g_option_context_parse()</code></a>. See <ahref="/usr/share/gtk-doc/html/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. Note that this onlyworks if <a class="link" href="glib-running.html#setlocale" title="Locale"><code class="function">setlocale()</code></a> has been called before <a class="link" href="glib-Commandline-option-parser.html#g-option-context-parse"><code class="function">g_option_context_parse()</code></a>.</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;
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -