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