alternative to assert
[gtkD.git] / gtkD / src / glib / OptionContext.d
blobb761b191b66c627787b7668b8b2ab51b64023ea3
1 /*
2 * This file is part of gtkD.
4 * gtkD 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 * gtkD 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 gtkD; 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 * module aliases:
51 * local aliases:
54 module glib.OptionContext;
56 version(noAssert)
58 version(Tango)
60 import tango.io.Stdout; // use the tango loging?
64 private import gtkc.glibtypes;
66 private import gtkc.glib;
69 private import glib.ErrorG;
70 private import glib.OptionGroup;
71 private import glib.Str;
76 /**
77 * Description
78 * The GOption commandline parser is intended to be a simpler replacement for the
79 * popt library. It supports short and long commandline options, as shown in the
80 * following example:
81 * testtreemodel -r 1 --max-size 20 --rand --display=:1.0 -vb -- file1 file2
82 * The example demonstrates a number of features of the GOption commandline parser
83 * Options can be single letters, prefixed by a single dash. Multiple
84 * short options can be grouped behind a single dash.
85 * Long options are prefixed by two consecutive dashes.
86 * Options can have an extra argument, which can be a number, a string or a
87 * filename. For long options, the extra argument can be appended with an
88 * equals sign after the option name.
89 * Non-option arguments are returned to the application as rest arguments.
90 * An argument consisting solely of two dashes turns off further parsing,
91 * any remaining arguments (even those starting with a dash) are returned
92 * to the application as rest arguments.
93 * Another important feature of GOption is that it can automatically generate
94 * nicely formatted help output. Unless it is explicitly turned off with
95 * g_option_context_set_help_enabled(), GOption will recognize the
96 * --help, -?, --help-all
97 * and --help-groupname options
98 * (where groupname is the name of a GOptionGroup)
99 * and write a text similar to the one shown in the following example to stdout.
100 * Usage:
101 * testtreemodel [OPTION...] - test tree model performance
102 * Help Options:
103 * -?, --help Show help options
104 * --help-all Show all help options
105 * --help-gtk Show GTK+ Options
106 * Application Options:
107 * -r, --repeats=N Average over N repetitions
108 * -m, --max-size=M Test up to 2^M items
109 * --display=DISPLAY X display to use
110 * -v, --verbose Be verbose
111 * -b, --beep Beep when done
112 * --rand Randomize the data
113 * GOption groups options in GOptionGroups, which makes it easy to
114 * incorporate options from multiple sources. The intended use for this is
115 * to let applications collect option groups from the libraries it uses,
116 * add them to their GOptionContext, and parse all options by a single call
117 * to g_option_context_parse(). See gtk_get_option_group() for an example.
118 * If an option is declared to be of type string or filename, GOption takes
119 * care of converting it to the right encoding; strings are returned in UTF-8,
120 * filenames are returned in the GLib filename encoding. Note that this only
121 * works if setlocale() has been called before g_option_context_parse().
122 * Here is a complete example of setting up GOption to parse the example
123 * commandline above and produce the example help output.
124 * static gint repeats = 2;
125 * static gint max_size = 8;
126 * static gboolean verbose = FALSE;
127 * static gboolean beep = FALSE;
128 * static gboolean rand = FALSE;
129 * static GOptionEntry entries[] =
131 * { "repeats", 'r', 0, G_OPTION_ARG_INT, repeats, "Average over N repetitions", "N" },
132 * { "max-size", 'm', 0, G_OPTION_ARG_INT, max_size, "Test up to 2^M items", "M" },
133 * { "verbose", 'v', 0, G_OPTION_ARG_NONE, verbose, "Be verbose", NULL },
134 * { "beep", 'b', 0, G_OPTION_ARG_NONE, beep, "Beep when done", NULL },
135 * { "rand", 0, 0, G_OPTION_ARG_NONE, rand, "Randomize the data", NULL },
136 * { NULL }
137 * };
138 * int
139 * main (int argc, char *argv[])
141 * GError *error = NULL;
142 * GOptionContext *context;
143 * context = g_option_context_new ("- test tree model performance");
144 * g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
145 * g_option_context_add_group (context, gtk_get_option_group (TRUE));
146 * g_option_context_parse (context, argc, argv, error);
147 * /+* ... +/
150 public class OptionContext
153 /** the main Gtk struct */
154 protected GOptionContext* gOptionContext;
157 public GOptionContext* getOptionContextStruct()
159 return gOptionContext;
163 /** the main Gtk struct as a void* */
164 protected void* getStruct()
166 return cast(void*)gOptionContext;
170 * Sets our main struct and passes it to the parent class
172 public this (GOptionContext* gOptionContext)
174 version(noAssert)
176 if ( gOptionContext is null )
178 int zero = 0;
179 version(Tango)
181 Stdout("struct gOptionContext is null on constructor").newline;
183 else
185 printf("struct gOptionContext is null on constructor");
187 zero = zero / zero;
190 else
192 assert(gOptionContext !is null, "struct gOptionContext is null on constructor");
194 this.gOptionContext = gOptionContext;
205 * Creates a new option context.
206 * The parameter_string can serve multiple purposes. It can be used
207 * to add descriptions for "rest" arguments, which are not parsed by
208 * the GOptionContext, typically something like "FILES" or
209 * "FILE1 FILE2...". If you are using G_OPTION_REMAINING for
210 * collecting "rest" arguments, GLib handles this automatically by
211 * using the arg_description of the corresponding GOptionEntry in
212 * the usage summary.
213 * Another usage is to give a short summary of the program
214 * functionality, like " - frob the strings", which will be displayed
215 * in the same line as the usage. For a longer description of the
216 * program functionality that should be displayed as a paragraph
217 * below the usage line, use g_option_context_set_summary().
218 * Note that the parameter_string is translated (see
219 * g_option_context_set_translate_func()).
220 * parameter_string:
221 * a string which is displayed in
222 * the first line of --help output, after the
223 * usage summary
224 * programname [OPTION...]
225 * Returns:
226 * a newly created GOptionContext, which must be
227 * freed with g_option_context_free() after use.
228 * Since 2.6
230 public this (char[] parameterString)
232 // GOptionContext* g_option_context_new (const gchar *parameter_string);
233 this(cast(GOptionContext*)g_option_context_new(Str.toStringz(parameterString)) );
237 * Adds a string to be displayed in --help output
238 * before the list of options. This is typically a summary of the
239 * program functionality.
240 * Note that the summary is translated (see
241 * g_option_context_set_translate_func()).
242 * context:
243 * a GOptionContext
244 * summary:
245 * a string to be shown in --help output
246 * before the list of options, or NULL
247 * Since 2.12
249 public void setSummary(char[] summary)
251 // void g_option_context_set_summary (GOptionContext *context, const gchar *summary);
252 g_option_context_set_summary(gOptionContext, Str.toStringz(summary));
256 * Returns the summary. See g_option_context_set_summary().
257 * context:
258 * a GOptionContext
259 * Returns:
260 * the summary
261 * Since 2.12
263 public char[] getSummary()
265 // const gchar* g_option_context_get_summary (GOptionContext *context);
266 return Str.toString(g_option_context_get_summary(gOptionContext) );
270 * Adds a string to be displayed in --help output
271 * after the list of options. This text often includes a bug reporting
272 * address.
273 * Note that the summary is translated (see
274 * g_option_context_set_translate_func()).
275 * context:
276 * a GOptionContext
277 * description:
278 * a string to be shown in --help output
279 * after the list of options, or NULL
280 * Since 2.12
282 public void setDescription(char[] description)
284 // void g_option_context_set_description (GOptionContext *context, const gchar *description);
285 g_option_context_set_description(gOptionContext, Str.toStringz(description));
289 * Returns the description. See g_option_context_set_description().
290 * context:
291 * a GOptionContext
292 * Returns:
293 * the description
294 * Since 2.12
296 public char[] getDescription()
298 // const gchar* g_option_context_get_description (GOptionContext *context);
299 return Str.toString(g_option_context_get_description(gOptionContext) );
304 * Sets the function which is used to translate the contexts
305 * user-visible strings, for --help output.
306 * If func is NULL, strings are not translated.
307 * Note that option groups have their own translation functions,
308 * this function only affects the parameter_string (see g_option_context_nex()),
309 * the summary (see g_option_context_set_summary()) and the description
310 * (see g_option_context_set_description()).
311 * If you are using gettext(), you only need to set the translation
312 * domain, see g_context_group_set_translation_domain().
313 * context:
314 * a GOptionContext
315 * func:
316 * the GTranslateFunc, or NULL
317 * data:
318 * user data to pass to func, or NULL
319 * destroy_notify:
320 * a function which gets called to free data, or NULL
321 * Since 2.12
323 public void setTranslateFunc(GTranslateFunc func, void* data, GDestroyNotify destroyNotify)
325 // void g_option_context_set_translate_func (GOptionContext *context, GTranslateFunc func, gpointer data, GDestroyNotify destroy_notify);
326 g_option_context_set_translate_func(gOptionContext, func, data, destroyNotify);
330 * A convenience function to use gettext() for translating
331 * user-visible strings.
332 * context:
333 * a GOptionContext
334 * domain:
335 * the domain to use
336 * Since 2.12
338 public void setTranslationDomain(char[] domain)
340 // void g_option_context_set_translation_domain (GOptionContext *context, const gchar *domain);
341 g_option_context_set_translation_domain(gOptionContext, Str.toStringz(domain));
345 * Frees context and all the groups which have been
346 * added to it.
347 * context:
348 * a GOptionContext
349 * Since 2.6
351 public void free()
353 // void g_option_context_free (GOptionContext *context);
354 g_option_context_free(gOptionContext);
358 * Parses the command line arguments, recognizing options
359 * which have been added to context. A side-effect of
360 * calling this function is that g_set_prgname() will be
361 * called.
362 * If the parsing is successful, any parsed arguments are
363 * removed from the array and argc and argv are updated
364 * accordingly. A '--' option is stripped from argv
365 * unless there are unparsed options before and after it,
366 * or some of the options after it start with '-'. In case
367 * of an error, argc and argv are left unmodified.
368 * If automatic --help support is enabled
369 * (see g_option_context_set_help_enabled()), and the
370 * argv array contains one of the recognized help options,
371 * this function will produce help output to stdout and
372 * call exit (0).
373 * Note that function depends on the
374 * current locale for
375 * automatic character set conversion of string and filename
376 * arguments.
377 * context:
378 * a GOptionContext
379 * argc:
380 * a pointer to the number of command line arguments
381 * argv:
382 * a pointer to the array of command line arguments
383 * error:
384 * a return location for errors
385 * Returns:
386 * TRUE if the parsing was successful,
387 * FALSE if an error occurred
388 * Since 2.6
390 public int parse(int* argc, char*** argv, GError** error)
392 // gboolean g_option_context_parse (GOptionContext *context, gint *argc, gchar ***argv, GError **error);
393 return g_option_context_parse(gOptionContext, argc, argv, error);
397 * Enables or disables automatic generation of --help
398 * output. By default, g_option_context_parse() recognizes
399 * --help, -?, --help-all
400 * and --help-groupname and creates
401 * suitable output to stdout.
402 * context:
403 * a GOptionContext
404 * help_enabled:
405 * TRUE to enable --help, FALSE to disable it
406 * Since 2.6
408 public void setHelpEnabled(int helpEnabled)
410 // void g_option_context_set_help_enabled (GOptionContext *context, gboolean help_enabled);
411 g_option_context_set_help_enabled(gOptionContext, helpEnabled);
415 * Returns whether automatic --help generation
416 * is turned on for context. See g_option_context_set_help_enabled().
417 * context:
418 * a GOptionContext
419 * Returns:
420 * TRUE if automatic help generation is turned on.
421 * Since 2.6
423 public int getHelpEnabled()
425 // gboolean g_option_context_get_help_enabled (GOptionContext *context);
426 return g_option_context_get_help_enabled(gOptionContext);
430 * Sets whether to ignore unknown options or not. If an argument is
431 * ignored, it is left in the argv array after parsing. By default,
432 * g_option_context_parse() treats unknown options as error.
433 * This setting does not affect non-option arguments (i.e. arguments
434 * which don't start with a dash). But note that GOption cannot reliably
435 * determine whether a non-option belongs to a preceding unknown option.
436 * context:
437 * a GOptionContext
438 * ignore_unknown:
439 * TRUE to ignore unknown options, FALSE to produce
440 * an error when unknown options are met
441 * Since 2.6
443 public void setIgnoreUnknownOptions(int ignoreUnknown)
445 // void g_option_context_set_ignore_unknown_options (GOptionContext *context, gboolean ignore_unknown);
446 g_option_context_set_ignore_unknown_options(gOptionContext, ignoreUnknown);
450 * Returns whether unknown options are ignored or not. See
451 * g_option_context_set_ignore_unknown_options().
452 * context:
453 * a GOptionContext
454 * Returns:
455 * TRUE if unknown options are ignored.
456 * Since 2.6
458 public int getIgnoreUnknownOptions()
460 // gboolean g_option_context_get_ignore_unknown_options (GOptionContext *context);
461 return g_option_context_get_ignore_unknown_options(gOptionContext);
469 * A convenience function which creates a main group if it doesn't
470 * exist, adds the entries to it and sets the translation domain.
471 * context:
472 * a GOptionContext
473 * entries:
474 * a NULL-terminated array of GOptionEntrys
475 * translation_domain:
476 * a translation domain to use for translating
477 * the --help output for the options in entries
478 * with gettext(), or NULL
479 * Since 2.6
481 public void addMainEntries(GOptionEntry* entries, char[] translationDomain)
483 // void g_option_context_add_main_entries (GOptionContext *context, const GOptionEntry *entries, const gchar *translation_domain);
484 g_option_context_add_main_entries(gOptionContext, entries, Str.toStringz(translationDomain));
489 * Adds a GOptionGroup to the context, so that parsing with context
490 * will recognize the options in the group. Note that the group will
491 * be freed together with the context when g_option_context_free() is
492 * called, so you must not free the group yourself after adding it
493 * to a context.
494 * context:
495 * a GOptionContext
496 * group:
497 * the group to add
498 * Since 2.6
500 public void addGroup(OptionGroup group)
502 // void g_option_context_add_group (GOptionContext *context, GOptionGroup *group);
503 g_option_context_add_group(gOptionContext, (group is null) ? null : group.getOptionGroupStruct());
507 * Sets a GOptionGroup as main group of the context.
508 * This has the same effect as calling g_option_context_add_group(),
509 * the only difference is that the options in the main group are
510 * treated differently when generating --help output.
511 * context:
512 * a GOptionContext
513 * group:
514 * the group to set as main group
515 * Since 2.6
517 public void setMainGroup(OptionGroup group)
519 // void g_option_context_set_main_group (GOptionContext *context, GOptionGroup *group);
520 g_option_context_set_main_group(gOptionContext, (group is null) ? null : group.getOptionGroupStruct());
524 * Returns a pointer to the main group of context.
525 * context:
526 * a GOptionContext
527 * Returns:
528 * the main group of context, or NULL if context doesn't
529 * have a main group. Note that group belongs to context and should
530 * not be modified or freed.
531 * Since 2.6
533 public OptionGroup getMainGroup()
535 // GOptionGroup* g_option_context_get_main_group (GOptionContext *context);
536 return new OptionGroup( g_option_context_get_main_group(gOptionContext) );