alternative to assert
[gtkD.git] / src / glib / OptionContext.d
blobb4180d13c3649f9ba66c3465b70c219fed4d9289
1 /*
2 * This file is part of duit.
4 * duit is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; either version 2.1 of the License, or
7 * (at your option) any later version.
9 * duit is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with duit; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 // generated automatically - do not change
20 // find conversion definition on APILookup.txt
21 // implement new conversion functionalities on the wrap.utils pakage
24 * Conversion parameters:
25 * inFile = glib-Commandline-option-parser.html
26 * outPack = glib
27 * outFile = OptionContext
28 * strct = GOptionContext
29 * realStrct=
30 * ctorStrct=
31 * clss = OptionContext
32 * interf =
33 * class Code: No
34 * interface Code: No
35 * template for:
36 * extend =
37 * implements:
38 * prefixes:
39 * - g_option_context_
40 * omit structs:
41 * omit prefixes:
42 * - g_option_group_
43 * omit code:
44 * imports:
45 * - glib.ErrorG
46 * - glib.OptionGroup
47 * - glib.Str
48 * structWrap:
49 * - GOptionGroup* -> OptionGroup
50 * local aliases:
53 module glib.OptionContext;
55 private import glib.glibtypes;
57 private import lib.glib;
59 private import glib.ErrorG;
60 private import glib.OptionGroup;
61 private import glib.Str;
63 /**
64 * Description
65 * The GOption commandline parser is intended to be a simpler replacement for the
66 * popt library. It supports short and long commandline options, as shown in the
67 * following example:
68 * testtreemodel -r 1 --max-size 20 --rand --display=:1.0 -vb -- file1 file2
69 * The example demonstrates a number of features of the GOption commandline parser
70 * Options can be single letters, prefixed by a single dash. Multiple
71 * short options can be grouped behind a single dash.
72 * Long options are prefixed by two consecutive dashes.
73 * Options can have an extra argument, which can be a number, a string or a filename.
74 * For long options, the extra argument can be appended with an equals sign after the
75 * option name.
76 * Non-option arguments are returned to the application as rest arguments.
77 * An argument consisting solely of two dashes turns off further parsing, any remaining
78 * arguments (even those starting with a dash) are returned to the application as rest
79 * arguments.
80 * Another important feature of GOption is that it can automatically generate nicely
81 * formatted help output. Unless it is explicitly turned off with
82 * g_option_context_set_help_enabled(), GOption will recognize the
83 * --help, -?, --help-all
84 * and --help-groupname options
85 * (where groupname is the name of a GOptionGroup)
86 * and write a text similar to the one shown in the following example to stdout.
87 * Usage:
88 * testtreemodel [OPTION...] - test tree model performance
89 * Help Options:
90 * -?, --help Show help options
91 * --help-all Show all help options
92 * --help-gtk Show GTK+ Options
93 * Application Options:
94 * -r, --repeats=N Average over N repetitions
95 * -m, --max-size=M Test up to 2^M items
96 * --display=DISPLAY X display to use
97 * -v, --verbose Be verbose
98 * -b, --beep Beep when done
99 * --rand Randomize the data
100 * GOption groups options in GOptionGroups, which makes it easy to
101 * incorporate options from multiple sources. The intended use for this is
102 * to let applications collect option groups from the libraries it uses,
103 * add them to their GOptionContext, and parse all options by a single call
104 * to g_option_context_parse(). See gtk_get_option_group() for an example.
105 * If an option is declared to be of type string or filename, GOption takes
106 * care of converting it to the right encoding; strings are returned in UTF-8,
107 * filenames are returned in the GLib filename encoding.
108 * Here is a complete example of setting up GOption to parse the example
109 * commandline above and produce the example help output.
110 * static gint repeats = 2;
111 * static gint max_size = 8;
112 * static gboolean verbose = FALSE;
113 * static gboolean beep = FALSE;
114 * static gboolean rand = FALSE;
115 * static GOptionEntry entries[] =
117 * { "repeats", 'r', 0, G_OPTION_ARG_INT, repeats, "Average over N repetitions", "N" },
118 * { "max-size", 'm', 0, G_OPTION_ARG_INT, max_size, "Test up to 2^M items", "M" },
119 * { "verbose", 'v', 0, G_OPTION_ARG_NONE, verbose, "Be verbose", NULL },
120 * { "beep", 'b', 0, G_OPTION_ARG_NONE, beep, "Beep when done", NULL },
121 * { "rand", 0, 0, G_OPTION_ARG_NONE, rand, "Randomize the data", NULL },
122 * { NULL }
123 * };
124 * int
125 * main (int argc, char *argv[])
127 * GError *error = NULL;
128 * GOptionContext *context;
129 * context = g_option_context_new ("- test tree model performance");
130 * g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
131 * g_option_context_add_group (context, gtk_get_option_group (TRUE));
132 * g_option_context_parse (context, argc, argv, error);
133 * /+* ... +/
136 public class OptionContext
139 /** the main Gtk struct */
140 protected GOptionContext* gOptionContext;
143 public GOptionContext* getOptionContextStruct()
145 return gOptionContext;
149 /** the main Gtk struct as a void* */
150 protected void* getStruct()
152 return cast(void*)gOptionContext;
156 * Sets our main struct and passes it to the parent class
158 public this (GOptionContext* gOptionContext)
160 this.gOptionContext = gOptionContext;
171 * Creates a new option context.
172 * The parameter_string can serve multiple purposes. It can be used
173 * to add descriptions for "rest" arguments, which are not parsed by
174 * the GOptionContext, typically something like "FILES" or
175 * "FILE1 FILE2...". If you are using G_OPTION_REMAINING for
176 * collecting "rest" arguments, GLib handles this automatically by
177 * using the arg_description of the corresponding GOptionEntry in
178 * the usage summary.
179 * Another usage is to give a short summary of the program
180 * functionality, like " - frob the strings", which will be displayed
181 * in the same line as the usage. For a longer description of the
182 * program functionality that should be displayed as a paragraph
183 * below the usage line, use g_option_context_set_summary().
184 * Note that the parameter_string is translated (see
185 * g_option_context_set_translate_func()).
186 * parameter_string:
187 * a string which is displayed in
188 * the first line of --help output, after the
189 * usage summary
190 * programname [OPTION...]
191 * Returns:
192 * a newly created GOptionContext, which must be
193 * freed with g_option_context_free() after use.
194 * Since 2.6
196 public this (char[] parameterString)
198 // GOptionContext* g_option_context_new (const gchar *parameter_string);
199 this(cast(GOptionContext*)g_option_context_new(Str.toStringz(parameterString)) );
203 * Adds a string to be displayed in --help output
204 * before the list of options. This is typically a summary of the
205 * program functionality.
206 * Note that the summary is translated (see
207 * g_option_context_set_translate_func()).
208 * context:
209 * a GOptionContext
210 * summary:
211 * a string to be shown in --help output
212 * before the list of options, or NULL
213 * Since 2.12
215 public void setSummary(char[] summary)
217 // void g_option_context_set_summary (GOptionContext *context, const gchar *summary);
218 g_option_context_set_summary(gOptionContext, Str.toStringz(summary));
222 * Returns the summary. See g_option_context_set_summary().
223 * context:
224 * a GOptionContext
225 * Returns:
226 * the summary
227 * Since 2.12
229 public char[] getSummary()
231 // const gchar* g_option_context_get_summary (GOptionContext *context);
232 return Str.toString(g_option_context_get_summary(gOptionContext) );
236 * Adds a string to be displayed in --help output
237 * after the list of options. This text often includes a bug reporting
238 * address.
239 * Note that the summary is translated (see
240 * g_option_context_set_translate_func()).
241 * context:
242 * a GOptionContext
243 * description:
244 * a string to be shown in --help output
245 * after the list of options, or NULL
246 * Since 2.12
248 public void setDescription(char[] description)
250 // void g_option_context_set_description (GOptionContext *context, const gchar *description);
251 g_option_context_set_description(gOptionContext, Str.toStringz(description));
255 * Returns the description. See g_option_context_set_description().
256 * context:
257 * a GOptionContext
258 * Returns:
259 * the description
260 * Since 2.12
262 public char[] getDescription()
264 // const gchar* g_option_context_get_description (GOptionContext *context);
265 return Str.toString(g_option_context_get_description(gOptionContext) );
270 * Sets the function which is used to translate the contexts
271 * user-visible strings, for --help output.
272 * If func is NULL, strings are not translated.
273 * Note that option groups have their own translation functions,
274 * this function only affects the parameter_string (see g_option_context_nex()),
275 * the summary (see g_option_context_set_summary()) and the description
276 * (see g_option_context_set_description()).
277 * If you are using gettext(), you only need to set the translation
278 * domain, see g_context_group_set_translation_domain().
279 * context:
280 * a GOptionContext
281 * func:
282 * the GTranslateFunc, or NULL
283 * data:
284 * user data to pass to func, or NULL
285 * destroy_notify:
286 * a function which gets called to free data, or NULL
287 * Since 2.12
289 public void setTranslateFunc(GTranslateFunc func, void* data, GDestroyNotify destroyNotify)
291 // void g_option_context_set_translate_func (GOptionContext *context, GTranslateFunc func, gpointer data, GDestroyNotify destroy_notify);
292 g_option_context_set_translate_func(gOptionContext, func, data, destroyNotify);
296 * A convenience function to use gettext() for translating
297 * user-visible strings.
298 * context:
299 * a GOptionContext
300 * domain:
301 * the domain to use
302 * Since 2.12
304 public void setTranslationDomain(char[] domain)
306 // void g_option_context_set_translation_domain (GOptionContext *context, const gchar *domain);
307 g_option_context_set_translation_domain(gOptionContext, Str.toStringz(domain));
311 * Frees context and all the groups which have been
312 * added to it.
313 * context:
314 * a GOptionContext
315 * Since 2.6
317 public void free()
319 // void g_option_context_free (GOptionContext *context);
320 g_option_context_free(gOptionContext);
324 * Parses the command line arguments, recognizing options
325 * which have been added to context. A side-effect of
326 * calling this function is that g_set_prgname() will be
327 * called.
328 * If the parsing is successful, any parsed arguments are
329 * removed from the array and argc and argv are updated
330 * accordingly. A '--' option is stripped from argv
331 * unless there are unparsed options before and after it,
332 * or some of the options after it start with '-'. In case
333 * of an error, argc and argv are left unmodified.
334 * If automatic --help support is enabled
335 * (see g_option_context_set_help_enabled()), and the
336 * argv array contains one of the recognized help options,
337 * this function will produce help output to stdout and
338 * call exit (0).
339 * context:
340 * a GOptionContext
341 * argc:
342 * a pointer to the number of command line arguments.
343 * argv:
344 * a pointer to the array of command line arguments.
345 * error:
346 * a return location for errors
347 * Returns:
348 * TRUE if the parsing was successful,
349 * FALSE if an error occurred
350 * Since 2.6
352 public int parse(int* argc, char*** argv, GError** error)
354 // gboolean g_option_context_parse (GOptionContext *context, gint *argc, gchar ***argv, GError **error);
355 return g_option_context_parse(gOptionContext, argc, argv, error);
359 * Enables or disables automatic generation of --help
360 * output. By default, g_option_context_parse() recognizes
361 * --help, -?, --help-all
362 * and --help-groupname and creates
363 * suitable output to stdout.
364 * context:
365 * a GOptionContext
366 * help_enabled:
367 * TRUE to enable --help, FALSE to disable it
368 * Since 2.6
370 public void setHelpEnabled(int helpEnabled)
372 // void g_option_context_set_help_enabled (GOptionContext *context, gboolean help_enabled);
373 g_option_context_set_help_enabled(gOptionContext, helpEnabled);
377 * Returns whether automatic --help generation
378 * is turned on for context. See g_option_context_set_help_enabled().
379 * context:
380 * a GOptionContext
381 * Returns:
382 * TRUE if automatic help generation is turned on.
383 * Since 2.6
385 public int getHelpEnabled()
387 // gboolean g_option_context_get_help_enabled (GOptionContext *context);
388 return g_option_context_get_help_enabled(gOptionContext);
392 * Sets whether to ignore unknown options or not. If an argument is
393 * ignored, it is left in the argv array after parsing. By default,
394 * g_option_context_parse() treats unknown options as error.
395 * This setting does not affect non-option arguments (i.e. arguments
396 * which don't start with a dash). But note that GOption cannot reliably
397 * determine whether a non-option belongs to a preceding unknown option.
398 * context:
399 * a GOptionContext
400 * ignore_unknown:
401 * TRUE to ignore unknown options, FALSE to produce
402 * an error when unknown options are met
403 * Since 2.6
405 public void setIgnoreUnknownOptions(int ignoreUnknown)
407 // void g_option_context_set_ignore_unknown_options (GOptionContext *context, gboolean ignore_unknown);
408 g_option_context_set_ignore_unknown_options(gOptionContext, ignoreUnknown);
412 * Returns whether unknown options are ignored or not. See
413 * g_option_context_set_ignore_unknown_options().
414 * context:
415 * a GOptionContext
416 * Returns:
417 * TRUE if unknown options are ignored.
418 * Since 2.6
420 public int getIgnoreUnknownOptions()
422 // gboolean g_option_context_get_ignore_unknown_options (GOptionContext *context);
423 return g_option_context_get_ignore_unknown_options(gOptionContext);
431 * A convenience function which creates a main group if it doesn't
432 * exist, adds the entries to it and sets the translation domain.
433 * context:
434 * a GOptionContext
435 * entries:
436 * a NULL-terminated array of GOptionEntrys
437 * translation_domain:
438 * a translation domain to use for translating
439 * the --help output for the options in entries
440 * with gettext(), or NULL
441 * Since 2.6
443 public void addMainEntries(GOptionEntry* entries, char[] translationDomain)
445 // void g_option_context_add_main_entries (GOptionContext *context, const GOptionEntry *entries, const gchar *translation_domain);
446 g_option_context_add_main_entries(gOptionContext, entries, Str.toStringz(translationDomain));
451 * Adds a GOptionGroup to the context, so that parsing with context
452 * will recognize the options in the group. Note that the group will
453 * be freed together with the context when g_option_context_free() is
454 * called, so you must not free the group yourself after adding it
455 * to a context.
456 * context:
457 * a GOptionContext
458 * group:
459 * the group to add
460 * Since 2.6
462 public void addGroup(OptionGroup group)
464 // void g_option_context_add_group (GOptionContext *context, GOptionGroup *group);
465 g_option_context_add_group(gOptionContext, (group is null) ? null : group.getOptionGroupStruct());
469 * Sets a GOptionGroup as main group of the context.
470 * This has the same effect as calling g_option_context_add_group(),
471 * the only difference is that the options in the main group are
472 * treated differently when generating --help output.
473 * context:
474 * a GOptionContext
475 * group:
476 * the group to set as main group
477 * Since 2.6
479 public void setMainGroup(OptionGroup group)
481 // void g_option_context_set_main_group (GOptionContext *context, GOptionGroup *group);
482 g_option_context_set_main_group(gOptionContext, (group is null) ? null : group.getOptionGroupStruct());
486 * Returns a pointer to the main group of context.
487 * context:
488 * a GOptionContext
489 * Returns:
490 * the main group of context, or NULL if context doesn't
491 * have a main group. Note that group belongs to context and should
492 * not be modified or freed.
493 * Since 2.6
495 public OptionGroup getMainGroup()
497 // GOptionGroup* g_option_context_get_main_group (GOptionContext *context);
498 return new OptionGroup( g_option_context_get_main_group(gOptionContext) );