GSimpleProxyResolver: convert docs to markdown
[glib.git] / glib / goption.h
blob3162dd289a181dfb3be2abba12f77b0762d07f34
1 /* goption.h - Option parser
3 * Copyright (C) 2004 Anders Carlsson <andersca@gnome.org>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 #ifndef __G_OPTION_H__
20 #define __G_OPTION_H__
22 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
23 #error "Only <glib.h> can be included directly."
24 #endif
26 #include <glib/gerror.h>
27 #include <glib/gquark.h>
29 G_BEGIN_DECLS
31 /**
32 * GOptionContext:
34 * A <structname>GOptionContext</structname> struct defines which options
35 * are accepted by the commandline option parser. The struct has only private
36 * fields and should not be directly accessed.
38 typedef struct _GOptionContext GOptionContext;
40 /**
41 * GOptionGroup:
43 * A <structname>GOptionGroup</structname> struct defines the options in a single
44 * group. The struct has only private fields and should not be directly accessed.
46 * All options in a group share the same translation function. Libraries which
47 * need to parse commandline options are expected to provide a function for
48 * getting a <structname>GOptionGroup</structname> holding their options, which
49 * the application can then add to its #GOptionContext.
51 typedef struct _GOptionGroup GOptionGroup;
52 typedef struct _GOptionEntry GOptionEntry;
54 /**
55 * GOptionFlags:
56 * @G_OPTION_FLAG_HIDDEN: The option doesn't appear in <option>--help</option>
57 * output.
58 * @G_OPTION_FLAG_IN_MAIN: The option appears in the main section of the
59 * <option>--help</option> output, even if it is defined in a group.
60 * @G_OPTION_FLAG_REVERSE: For options of the %G_OPTION_ARG_NONE kind, this flag
61 * indicates that the sense of the option is reversed.
62 * @G_OPTION_FLAG_NO_ARG: For options of the %G_OPTION_ARG_CALLBACK kind,
63 * this flag indicates that the callback does not take any argument
64 * (like a %G_OPTION_ARG_NONE option). Since 2.8
65 * @G_OPTION_FLAG_FILENAME: For options of the %G_OPTION_ARG_CALLBACK
66 * kind, this flag indicates that the argument should be passed to the
67 * callback in the GLib filename encoding rather than UTF-8. Since 2.8
68 * @G_OPTION_FLAG_OPTIONAL_ARG: For options of the %G_OPTION_ARG_CALLBACK
69 * kind, this flag indicates that the argument supply is optional. If no argument
70 * is given then data of %GOptionParseFunc will be set to NULL. Since 2.8
71 * @G_OPTION_FLAG_NOALIAS: This flag turns off the automatic conflict resolution
72 * which prefixes long option names with <literal>groupname-</literal> if
73 * there is a conflict. This option should only be used in situations where
74 * aliasing is necessary to model some legacy commandline interface. It is
75 * not safe to use this option, unless all option groups are under your
76 * direct control. Since 2.8.
78 * Flags which modify individual options.
80 typedef enum
82 G_OPTION_FLAG_HIDDEN = 1 << 0,
83 G_OPTION_FLAG_IN_MAIN = 1 << 1,
84 G_OPTION_FLAG_REVERSE = 1 << 2,
85 G_OPTION_FLAG_NO_ARG = 1 << 3,
86 G_OPTION_FLAG_FILENAME = 1 << 4,
87 G_OPTION_FLAG_OPTIONAL_ARG = 1 << 5,
88 G_OPTION_FLAG_NOALIAS = 1 << 6
89 } GOptionFlags;
91 /**
92 * GOptionArg:
93 * @G_OPTION_ARG_NONE: No extra argument. This is useful for simple flags.
94 * @G_OPTION_ARG_STRING: The option takes a string argument.
95 * @G_OPTION_ARG_INT: The option takes an integer argument.
96 * @G_OPTION_ARG_CALLBACK: The option provides a callback to parse the
97 * extra argument.
98 * @G_OPTION_ARG_FILENAME: The option takes a filename as argument.
99 * @G_OPTION_ARG_STRING_ARRAY: The option takes a string argument, multiple
100 * uses of the option are collected into an array of strings.
101 * @G_OPTION_ARG_FILENAME_ARRAY: The option takes a filename as argument,
102 * multiple uses of the option are collected into an array of strings.
103 * @G_OPTION_ARG_DOUBLE: The option takes a double argument. The argument
104 * can be formatted either for the user's locale or for the "C" locale. Since 2.12
105 * @G_OPTION_ARG_INT64: The option takes a 64-bit integer. Like %G_OPTION_ARG_INT
106 * but for larger numbers. The number can be in decimal base, or in hexadecimal
107 * (when prefixed with <literal>0x</literal>, for example, <literal>0xffffffff</literal>).
108 * Since 2.12
110 * The #GOptionArg enum values determine which type of extra argument the
111 * options expect to find. If an option expects an extra argument, it
112 * can be specified in several ways; with a short option:
113 * <option>-x arg</option>, with a long option: <option>--name arg</option>
114 * or combined in a single argument: <option>--name=arg</option>.
116 typedef enum
118 G_OPTION_ARG_NONE,
119 G_OPTION_ARG_STRING,
120 G_OPTION_ARG_INT,
121 G_OPTION_ARG_CALLBACK,
122 G_OPTION_ARG_FILENAME,
123 G_OPTION_ARG_STRING_ARRAY,
124 G_OPTION_ARG_FILENAME_ARRAY,
125 G_OPTION_ARG_DOUBLE,
126 G_OPTION_ARG_INT64
127 } GOptionArg;
130 * GOptionArgFunc:
131 * @option_name: The name of the option being parsed. This will be either a
132 * single dash followed by a single letter (for a short name) or two dashes
133 * followed by a long option name.
134 * @value: The value to be parsed.
135 * @data: User data added to the #GOptionGroup containing the option when it
136 * was created with g_option_group_new()
137 * @error: A return location for errors. The error code %G_OPTION_ERROR_FAILED
138 * is intended to be used for errors in #GOptionArgFunc callbacks.
140 * The type of function to be passed as callback for %G_OPTION_ARG_CALLBACK
141 * options.
143 * Returns: %TRUE if the option was successfully parsed, %FALSE if an error
144 * occurred, in which case @error should be set with g_set_error()
146 typedef gboolean (*GOptionArgFunc) (const gchar *option_name,
147 const gchar *value,
148 gpointer data,
149 GError **error);
152 * GOptionParseFunc:
153 * @context: The active #GOptionContext
154 * @group: The group to which the function belongs
155 * @data: User data added to the #GOptionGroup containing the option when it
156 * was created with g_option_group_new()
157 * @error: A return location for error details
159 * The type of function that can be called before and after parsing.
161 * Returns: %TRUE if the function completed successfully, %FALSE if an error
162 * occurred, in which case @error should be set with g_set_error()
164 typedef gboolean (*GOptionParseFunc) (GOptionContext *context,
165 GOptionGroup *group,
166 gpointer data,
167 GError **error);
170 * GOptionErrorFunc:
171 * @context: The active #GOptionContext
172 * @group: The group to which the function belongs
173 * @data: User data added to the #GOptionGroup containing the option when it
174 * was created with g_option_group_new()
175 * @error: The #GError containing details about the parse error
177 * The type of function to be used as callback when a parse error occurs.
179 typedef void (*GOptionErrorFunc) (GOptionContext *context,
180 GOptionGroup *group,
181 gpointer data,
182 GError **error);
185 * G_OPTION_ERROR:
187 * Error domain for option parsing. Errors in this domain will
188 * be from the #GOptionError enumeration. See #GError for information on
189 * error domains.
191 #define G_OPTION_ERROR (g_option_error_quark ())
194 * GOptionError:
195 * @G_OPTION_ERROR_UNKNOWN_OPTION: An option was not known to the parser.
196 * This error will only be reported, if the parser hasn't been instructed
197 * to ignore unknown options, see g_option_context_set_ignore_unknown_options().
198 * @G_OPTION_ERROR_BAD_VALUE: A value couldn't be parsed.
199 * @G_OPTION_ERROR_FAILED: A #GOptionArgFunc callback failed.
201 * Error codes returned by option parsing.
203 typedef enum
205 G_OPTION_ERROR_UNKNOWN_OPTION,
206 G_OPTION_ERROR_BAD_VALUE,
207 G_OPTION_ERROR_FAILED
208 } GOptionError;
210 GLIB_AVAILABLE_IN_ALL
211 GQuark g_option_error_quark (void);
214 * GOptionEntry:
215 * @long_name: The long name of an option can be used to specify it
216 * in a commandline as --<replaceable>long_name</replaceable>. Every
217 * option must have a long name. To resolve conflicts if multiple
218 * option groups contain the same long name, it is also possible to
219 * specify the option as
220 * --<replaceable>groupname</replaceable>-<replaceable>long_name</replaceable>.
221 * @short_name: If an option has a short name, it can be specified
222 * -<replaceable>short_name</replaceable> in a commandline. @short_name must be
223 * a printable ASCII character different from '-', or zero if the option has no
224 * short name.
225 * @flags: Flags from #GOptionFlags.
226 * @arg: The type of the option, as a #GOptionArg.
227 * @arg_data: If the @arg type is %G_OPTION_ARG_CALLBACK, then @arg_data must
228 * point to a #GOptionArgFunc callback function, which will be called to handle
229 * the extra argument. Otherwise, @arg_data is a pointer to a location to store
230 * the value, the required type of the location depends on the @arg type:
231 * <variablelist>
232 * <varlistentry>
233 * <term>%G_OPTION_ARG_NONE</term>
234 * <listitem><para>%gboolean</para></listitem>
235 * </varlistentry>
236 * <varlistentry>
237 * <term>%G_OPTION_ARG_STRING</term>
238 * <listitem><para>%gchar*</para></listitem>
239 * </varlistentry>
240 * <varlistentry>
241 * <term>%G_OPTION_ARG_INT</term>
242 * <listitem><para>%gint</para></listitem>
243 * </varlistentry>
244 * <varlistentry>
245 * <term>%G_OPTION_ARG_FILENAME</term>
246 * <listitem><para>%gchar*</para></listitem>
247 * </varlistentry>
248 * <varlistentry>
249 * <term>%G_OPTION_ARG_STRING_ARRAY</term>
250 * <listitem><para>%gchar**</para></listitem>
251 * </varlistentry>
252 * <varlistentry>
253 * <term>%G_OPTION_ARG_FILENAME_ARRAY</term>
254 * <listitem><para>%gchar**</para></listitem>
255 * </varlistentry>
256 * <varlistentry>
257 * <term>%G_OPTION_ARG_DOUBLE</term>
258 * <listitem><para>%gdouble</para></listitem>
259 * </varlistentry>
260 * </variablelist>
261 * If @arg type is %G_OPTION_ARG_STRING or %G_OPTION_ARG_FILENAME the location
262 * will contain a newly allocated string if the option was given. That string
263 * needs to be freed by the callee using g_free(). Likewise if @arg type is
264 * %G_OPTION_ARG_STRING_ARRAY or %G_OPTION_ARG_FILENAME_ARRAY, the data should
265 * be freed using g_strfreev().
266 * @description: the description for the option in <option>--help</option>
267 * output. The @description is translated using the @translate_func of the
268 * group, see g_option_group_set_translation_domain().
269 * @arg_description: The placeholder to use for the extra argument parsed
270 * by the option in <option>--help</option>
271 * output. The @arg_description is translated using the @translate_func of the
272 * group, see g_option_group_set_translation_domain().
274 * A <structname>GOptionEntry</structname> defines a single option.
275 * To have an effect, they must be added to a #GOptionGroup with
276 * g_option_context_add_main_entries() or g_option_group_add_entries().
278 struct _GOptionEntry
280 const gchar *long_name;
281 gchar short_name;
282 gint flags;
284 GOptionArg arg;
285 gpointer arg_data;
287 const gchar *description;
288 const gchar *arg_description;
292 * G_OPTION_REMAINING:
294 * If a long option in the main group has this name, it is not treated as a
295 * regular option. Instead it collects all non-option arguments which would
296 * otherwise be left in <literal>argv</literal>. The option must be of type
297 * %G_OPTION_ARG_CALLBACK, %G_OPTION_ARG_STRING_ARRAY
298 * or %G_OPTION_ARG_FILENAME_ARRAY.
301 * Using #G_OPTION_REMAINING instead of simply scanning <literal>argv</literal>
302 * for leftover arguments has the advantage that GOption takes care of
303 * necessary encoding conversions for strings or filenames.
305 * Since: 2.6
307 #define G_OPTION_REMAINING ""
309 GLIB_AVAILABLE_IN_ALL
310 GOptionContext *g_option_context_new (const gchar *parameter_string);
311 GLIB_AVAILABLE_IN_ALL
312 void g_option_context_set_summary (GOptionContext *context,
313 const gchar *summary);
314 GLIB_AVAILABLE_IN_ALL
315 const gchar * g_option_context_get_summary (GOptionContext *context);
316 GLIB_AVAILABLE_IN_ALL
317 void g_option_context_set_description (GOptionContext *context,
318 const gchar *description);
319 GLIB_AVAILABLE_IN_ALL
320 const gchar * g_option_context_get_description (GOptionContext *context);
321 GLIB_AVAILABLE_IN_ALL
322 void g_option_context_free (GOptionContext *context);
323 GLIB_AVAILABLE_IN_ALL
324 void g_option_context_set_help_enabled (GOptionContext *context,
325 gboolean help_enabled);
326 GLIB_AVAILABLE_IN_ALL
327 gboolean g_option_context_get_help_enabled (GOptionContext *context);
328 GLIB_AVAILABLE_IN_ALL
329 void g_option_context_set_ignore_unknown_options (GOptionContext *context,
330 gboolean ignore_unknown);
331 GLIB_AVAILABLE_IN_ALL
332 gboolean g_option_context_get_ignore_unknown_options (GOptionContext *context);
334 GLIB_AVAILABLE_IN_ALL
335 void g_option_context_add_main_entries (GOptionContext *context,
336 const GOptionEntry *entries,
337 const gchar *translation_domain);
338 GLIB_AVAILABLE_IN_ALL
339 gboolean g_option_context_parse (GOptionContext *context,
340 gint *argc,
341 gchar ***argv,
342 GError **error);
343 GLIB_AVAILABLE_IN_2_40
344 gboolean g_option_context_parse_strv (GOptionContext *context,
345 gchar ***arguments,
346 GError **error);
347 GLIB_AVAILABLE_IN_ALL
348 void g_option_context_set_translate_func (GOptionContext *context,
349 GTranslateFunc func,
350 gpointer data,
351 GDestroyNotify destroy_notify);
352 GLIB_AVAILABLE_IN_ALL
353 void g_option_context_set_translation_domain (GOptionContext *context,
354 const gchar *domain);
356 GLIB_AVAILABLE_IN_ALL
357 void g_option_context_add_group (GOptionContext *context,
358 GOptionGroup *group);
359 GLIB_AVAILABLE_IN_ALL
360 void g_option_context_set_main_group (GOptionContext *context,
361 GOptionGroup *group);
362 GLIB_AVAILABLE_IN_ALL
363 GOptionGroup *g_option_context_get_main_group (GOptionContext *context);
364 GLIB_AVAILABLE_IN_ALL
365 gchar *g_option_context_get_help (GOptionContext *context,
366 gboolean main_help,
367 GOptionGroup *group);
369 GLIB_AVAILABLE_IN_ALL
370 GOptionGroup *g_option_group_new (const gchar *name,
371 const gchar *description,
372 const gchar *help_description,
373 gpointer user_data,
374 GDestroyNotify destroy);
375 GLIB_AVAILABLE_IN_ALL
376 void g_option_group_set_parse_hooks (GOptionGroup *group,
377 GOptionParseFunc pre_parse_func,
378 GOptionParseFunc post_parse_func);
379 GLIB_AVAILABLE_IN_ALL
380 void g_option_group_set_error_hook (GOptionGroup *group,
381 GOptionErrorFunc error_func);
382 GLIB_AVAILABLE_IN_ALL
383 void g_option_group_free (GOptionGroup *group);
384 GLIB_AVAILABLE_IN_ALL
385 void g_option_group_add_entries (GOptionGroup *group,
386 const GOptionEntry *entries);
387 GLIB_AVAILABLE_IN_ALL
388 void g_option_group_set_translate_func (GOptionGroup *group,
389 GTranslateFunc func,
390 gpointer data,
391 GDestroyNotify destroy_notify);
392 GLIB_AVAILABLE_IN_ALL
393 void g_option_group_set_translation_domain (GOptionGroup *group,
394 const gchar *domain);
396 G_END_DECLS
398 #endif /* __G_OPTION_H__ */