1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD 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
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module glib.OptionContext;
26 
27 private import glib.ConstructionException;
28 private import glib.ErrorG;
29 private import glib.GException;
30 private import glib.OptionGroup;
31 private import glib.Str;
32 private import gtkc.glib;
33 public  import gtkc.glibtypes;
34 
35 
36 /**
37  * A `GOptionContext` struct defines which options
38  * are accepted by the commandline option parser. The struct has only private
39  * fields and should not be directly accessed.
40  */
41 public class OptionContext
42 {
43 	/** the main Gtk struct */
44 	protected GOptionContext* gOptionContext;
45 
46 	/** Get the main Gtk struct */
47 	public GOptionContext* getOptionContextStruct()
48 	{
49 		return gOptionContext;
50 	}
51 
52 	/** the main Gtk struct as a void* */
53 	protected void* getStruct()
54 	{
55 		return cast(void*)gOptionContext;
56 	}
57 
58 	/**
59 	 * Sets our main struct and passes it to the parent class.
60 	 */
61 	public this (GOptionContext* gOptionContext)
62 	{
63 		this.gOptionContext = gOptionContext;
64 	}
65 
66 	/**
67 	 */
68 
69 	/**
70 	 * Adds a #GOptionGroup to the @context, so that parsing with @context
71 	 * will recognize the options in the group. Note that this will take
72 	 * ownership of the @group and thus the @group should not be freed.
73 	 *
74 	 * Params:
75 	 *     group = the group to add
76 	 *
77 	 * Since: 2.6
78 	 */
79 	public void addGroup(OptionGroup group)
80 	{
81 		g_option_context_add_group(gOptionContext, (group is null) ? null : group.getOptionGroupStruct());
82 	}
83 
84 	/**
85 	 * A convenience function which creates a main group if it doesn't
86 	 * exist, adds the @entries to it and sets the translation domain.
87 	 *
88 	 * Params:
89 	 *     entries = a %NULL-terminated array of #GOptionEntrys
90 	 *     translationDomain = a translation domain to use for translating
91 	 *         the `--help` output for the options in @entries
92 	 *         with gettext(), or %NULL
93 	 *
94 	 * Since: 2.6
95 	 */
96 	public void addMainEntries(GOptionEntry* entries, string translationDomain)
97 	{
98 		g_option_context_add_main_entries(gOptionContext, entries, Str.toStringz(translationDomain));
99 	}
100 
101 	/**
102 	 * Frees context and all the groups which have been
103 	 * added to it.
104 	 *
105 	 * Please note that parsed arguments need to be freed separately (see
106 	 * #GOptionEntry).
107 	 *
108 	 * Since: 2.6
109 	 */
110 	public void free()
111 	{
112 		g_option_context_free(gOptionContext);
113 	}
114 
115 	/**
116 	 * Returns the description. See g_option_context_set_description().
117 	 *
118 	 * Return: the description
119 	 *
120 	 * Since: 2.12
121 	 */
122 	public string getDescription()
123 	{
124 		return Str.toString(g_option_context_get_description(gOptionContext));
125 	}
126 
127 	/**
128 	 * Returns a formatted, translated help text for the given context.
129 	 * To obtain the text produced by `--help`, call
130 	 * `g_option_context_get_help (context, TRUE, NULL)`.
131 	 * To obtain the text produced by `--help-all`, call
132 	 * `g_option_context_get_help (context, FALSE, NULL)`.
133 	 * To obtain the help text for an option group, call
134 	 * `g_option_context_get_help (context, FALSE, group)`.
135 	 *
136 	 * Params:
137 	 *     mainHelp = if %TRUE, only include the main group
138 	 *     group = the #GOptionGroup to create help for, or %NULL
139 	 *
140 	 * Return: A newly allocated string containing the help text
141 	 *
142 	 * Since: 2.14
143 	 */
144 	public string getHelp(bool mainHelp, OptionGroup group)
145 	{
146 		return Str.toString(g_option_context_get_help(gOptionContext, mainHelp, (group is null) ? null : group.getOptionGroupStruct()));
147 	}
148 
149 	/**
150 	 * Returns whether automatic `--help` generation
151 	 * is turned on for @context. See g_option_context_set_help_enabled().
152 	 *
153 	 * Return: %TRUE if automatic help generation is turned on.
154 	 *
155 	 * Since: 2.6
156 	 */
157 	public bool getHelpEnabled()
158 	{
159 		return g_option_context_get_help_enabled(gOptionContext) != 0;
160 	}
161 
162 	/**
163 	 * Returns whether unknown options are ignored or not. See
164 	 * g_option_context_set_ignore_unknown_options().
165 	 *
166 	 * Return: %TRUE if unknown options are ignored.
167 	 *
168 	 * Since: 2.6
169 	 */
170 	public bool getIgnoreUnknownOptions()
171 	{
172 		return g_option_context_get_ignore_unknown_options(gOptionContext) != 0;
173 	}
174 
175 	/**
176 	 * Returns a pointer to the main group of @context.
177 	 *
178 	 * Return: the main group of @context, or %NULL if
179 	 *     @context doesn't have a main group. Note that group belongs to
180 	 *     @context and should not be modified or freed.
181 	 *
182 	 * Since: 2.6
183 	 */
184 	public OptionGroup getMainGroup()
185 	{
186 		auto p = g_option_context_get_main_group(gOptionContext);
187 		
188 		if(p is null)
189 		{
190 			return null;
191 		}
192 		
193 		return new OptionGroup(cast(GOptionGroup*) p);
194 	}
195 
196 	/**
197 	 * Returns whether strict POSIX code is enabled.
198 	 *
199 	 * See g_option_context_set_strict_posix() for more information.
200 	 *
201 	 * Return: %TRUE if strict POSIX is enabled, %FALSE otherwise.
202 	 *
203 	 * Since: 2.44
204 	 */
205 	public bool getStrictPosix()
206 	{
207 		return g_option_context_get_strict_posix(gOptionContext) != 0;
208 	}
209 
210 	/**
211 	 * Returns the summary. See g_option_context_set_summary().
212 	 *
213 	 * Return: the summary
214 	 *
215 	 * Since: 2.12
216 	 */
217 	public string getSummary()
218 	{
219 		return Str.toString(g_option_context_get_summary(gOptionContext));
220 	}
221 
222 	/**
223 	 * Parses the command line arguments, recognizing options
224 	 * which have been added to @context. A side-effect of
225 	 * calling this function is that g_set_prgname() will be
226 	 * called.
227 	 *
228 	 * If the parsing is successful, any parsed arguments are
229 	 * removed from the array and @argc and @argv are updated
230 	 * accordingly. A '--' option is stripped from @argv
231 	 * unless there are unparsed options before and after it,
232 	 * or some of the options after it start with '-'. In case
233 	 * of an error, @argc and @argv are left unmodified.
234 	 *
235 	 * If automatic `--help` support is enabled
236 	 * (see g_option_context_set_help_enabled()), and the
237 	 * @argv array contains one of the recognized help options,
238 	 * this function will produce help output to stdout and
239 	 * call `exit (0)`.
240 	 *
241 	 * Note that function depends on the [current locale][setlocale] for
242 	 * automatic character set conversion of string and filename
243 	 * arguments.
244 	 *
245 	 * Params:
246 	 *     argc = a pointer to the number of command line arguments
247 	 *     argv = a pointer to the array of command line arguments
248 	 *
249 	 * Return: %TRUE if the parsing was successful,
250 	 *     %FALSE if an error occurred
251 	 *
252 	 * Since: 2.6
253 	 *
254 	 * Throws: GException on failure.
255 	 */
256 	public bool parse(ref string[] argv)
257 	{
258 		int argc = cast(int)argv.length;
259 		char** outargv = Str.toStringzArray(argv);
260 		GError* err = null;
261 		
262 		auto p = g_option_context_parse(gOptionContext, &argc, &outargv, &err) != 0;
263 		
264 		if (err !is null)
265 		{
266 			throw new GException( new ErrorG(err) );
267 		}
268 		
269 		argv = Str.toStringArray(outargv, argc);
270 		
271 		return p;
272 	}
273 
274 	/**
275 	 * Parses the command line arguments.
276 	 *
277 	 * This function is similar to g_option_context_parse() except that it
278 	 * respects the normal memory rules when dealing with a strv instead of
279 	 * assuming that the passed-in array is the argv of the main function.
280 	 *
281 	 * In particular, strings that are removed from the arguments list will
282 	 * be freed using g_free().
283 	 *
284 	 * On Windows, the strings are expected to be in UTF-8.  This is in
285 	 * contrast to g_option_context_parse() which expects them to be in the
286 	 * system codepage, which is how they are passed as @argv to main().
287 	 * See g_win32_get_command_line() for a solution.
288 	 *
289 	 * This function is useful if you are trying to use #GOptionContext with
290 	 * #GApplication.
291 	 *
292 	 * Params:
293 	 *     arguments = a pointer to the
294 	 *         command line arguments (which must be in UTF-8 on Windows)
295 	 *
296 	 * Return: %TRUE if the parsing was successful,
297 	 *     %FALSE if an error occurred
298 	 *
299 	 * Since: 2.40
300 	 *
301 	 * Throws: GException on failure.
302 	 */
303 	public bool parseStrv(ref string[] arguments)
304 	{
305 		char** outarguments = Str.toStringzArray(arguments);
306 		GError* err = null;
307 		
308 		auto p = g_option_context_parse_strv(gOptionContext, &outarguments, &err) != 0;
309 		
310 		if (err !is null)
311 		{
312 			throw new GException( new ErrorG(err) );
313 		}
314 		
315 		arguments = Str.toStringArray(outarguments);
316 		
317 		return p;
318 	}
319 
320 	/**
321 	 * Adds a string to be displayed in `--help` output after the list
322 	 * of options. This text often includes a bug reporting address.
323 	 *
324 	 * Note that the summary is translated (see
325 	 * g_option_context_set_translate_func()).
326 	 *
327 	 * Params:
328 	 *     description = a string to be shown in `--help` output
329 	 *         after the list of options, or %NULL
330 	 *
331 	 * Since: 2.12
332 	 */
333 	public void setDescription(string description)
334 	{
335 		g_option_context_set_description(gOptionContext, Str.toStringz(description));
336 	}
337 
338 	/**
339 	 * Enables or disables automatic generation of `--help` output.
340 	 * By default, g_option_context_parse() recognizes `--help`, `-h`,
341 	 * `-?`, `--help-all` and `--help-groupname` and creates suitable
342 	 * output to stdout.
343 	 *
344 	 * Params:
345 	 *     helpEnabled = %TRUE to enable `--help`, %FALSE to disable it
346 	 *
347 	 * Since: 2.6
348 	 */
349 	public void setHelpEnabled(bool helpEnabled)
350 	{
351 		g_option_context_set_help_enabled(gOptionContext, helpEnabled);
352 	}
353 
354 	/**
355 	 * Sets whether to ignore unknown options or not. If an argument is
356 	 * ignored, it is left in the @argv array after parsing. By default,
357 	 * g_option_context_parse() treats unknown options as error.
358 	 *
359 	 * This setting does not affect non-option arguments (i.e. arguments
360 	 * which don't start with a dash). But note that GOption cannot reliably
361 	 * determine whether a non-option belongs to a preceding unknown option.
362 	 *
363 	 * Params:
364 	 *     ignoreUnknown = %TRUE to ignore unknown options, %FALSE to produce
365 	 *         an error when unknown options are met
366 	 *
367 	 * Since: 2.6
368 	 */
369 	public void setIgnoreUnknownOptions(bool ignoreUnknown)
370 	{
371 		g_option_context_set_ignore_unknown_options(gOptionContext, ignoreUnknown);
372 	}
373 
374 	/**
375 	 * Sets a #GOptionGroup as main group of the @context.
376 	 * This has the same effect as calling g_option_context_add_group(),
377 	 * the only difference is that the options in the main group are
378 	 * treated differently when generating `--help` output.
379 	 *
380 	 * Params:
381 	 *     group = the group to set as main group
382 	 *
383 	 * Since: 2.6
384 	 */
385 	public void setMainGroup(OptionGroup group)
386 	{
387 		g_option_context_set_main_group(gOptionContext, (group is null) ? null : group.getOptionGroupStruct());
388 	}
389 
390 	/**
391 	 * Sets strict POSIX mode.
392 	 *
393 	 * By default, this mode is disabled.
394 	 *
395 	 * In strict POSIX mode, the first non-argument parameter encountered
396 	 * (eg: filename) terminates argument processing.  Remaining arguments
397 	 * are treated as non-options and are not attempted to be parsed.
398 	 *
399 	 * If strict POSIX mode is disabled then parsing is done in the GNU way
400 	 * where option arguments can be freely mixed with non-options.
401 	 *
402 	 * As an example, consider "ls foo -l".  With GNU style parsing, this
403 	 * will list "foo" in long mode.  In strict POSIX style, this will list
404 	 * the files named "foo" and "-l".
405 	 *
406 	 * It may be useful to force strict POSIX mode when creating "verb
407 	 * style" command line tools.  For example, the "gsettings" command line
408 	 * tool supports the global option "--schemadir" as well as many
409 	 * subcommands ("get", "set", etc.) which each have their own set of
410 	 * arguments.  Using strict POSIX mode will allow parsing the global
411 	 * options up to the verb name while leaving the remaining options to be
412 	 * parsed by the relevant subcommand (which can be determined by
413 	 * examining the verb name, which should be present in argv[1] after
414 	 * parsing).
415 	 *
416 	 * Params:
417 	 *     strictPosix = the new value
418 	 *
419 	 * Since: 2.44
420 	 */
421 	public void setStrictPosix(bool strictPosix)
422 	{
423 		g_option_context_set_strict_posix(gOptionContext, strictPosix);
424 	}
425 
426 	/**
427 	 * Adds a string to be displayed in `--help` output before the list
428 	 * of options. This is typically a summary of the program functionality.
429 	 *
430 	 * Note that the summary is translated (see
431 	 * g_option_context_set_translate_func() and
432 	 * g_option_context_set_translation_domain()).
433 	 *
434 	 * Params:
435 	 *     summary = a string to be shown in `--help` output
436 	 *         before the list of options, or %NULL
437 	 *
438 	 * Since: 2.12
439 	 */
440 	public void setSummary(string summary)
441 	{
442 		g_option_context_set_summary(gOptionContext, Str.toStringz(summary));
443 	}
444 
445 	/**
446 	 * Sets the function which is used to translate the contexts
447 	 * user-visible strings, for `--help` output. If @func is %NULL,
448 	 * strings are not translated.
449 	 *
450 	 * Note that option groups have their own translation functions,
451 	 * this function only affects the @parameter_string (see g_option_context_new()),
452 	 * the summary (see g_option_context_set_summary()) and the description
453 	 * (see g_option_context_set_description()).
454 	 *
455 	 * If you are using gettext(), you only need to set the translation
456 	 * domain, see g_option_context_set_translation_domain().
457 	 *
458 	 * Params:
459 	 *     func = the #GTranslateFunc, or %NULL
460 	 *     data = user data to pass to @func, or %NULL
461 	 *     destroyNotify = a function which gets called to free @data, or %NULL
462 	 *
463 	 * Since: 2.12
464 	 */
465 	public void setTranslateFunc(GTranslateFunc func, void* data, GDestroyNotify destroyNotify)
466 	{
467 		g_option_context_set_translate_func(gOptionContext, func, data, destroyNotify);
468 	}
469 
470 	/**
471 	 * A convenience function to use gettext() for translating
472 	 * user-visible strings.
473 	 *
474 	 * Params:
475 	 *     domain = the domain to use
476 	 *
477 	 * Since: 2.12
478 	 */
479 	public void setTranslationDomain(string domain)
480 	{
481 		g_option_context_set_translation_domain(gOptionContext, Str.toStringz(domain));
482 	}
483 
484 	/**
485 	 * Creates a new option context.
486 	 *
487 	 * The @parameter_string can serve multiple purposes. It can be used
488 	 * to add descriptions for "rest" arguments, which are not parsed by
489 	 * the #GOptionContext, typically something like "FILES" or
490 	 * "FILE1 FILE2...". If you are using #G_OPTION_REMAINING for
491 	 * collecting "rest" arguments, GLib handles this automatically by
492 	 * using the @arg_description of the corresponding #GOptionEntry in
493 	 * the usage summary.
494 	 *
495 	 * Another usage is to give a short summary of the program
496 	 * functionality, like " - frob the strings", which will be displayed
497 	 * in the same line as the usage. For a longer description of the
498 	 * program functionality that should be displayed as a paragraph
499 	 * below the usage line, use g_option_context_set_summary().
500 	 *
501 	 * Note that the @parameter_string is translated using the
502 	 * function set with g_option_context_set_translate_func(), so
503 	 * it should normally be passed untranslated.
504 	 *
505 	 * Params:
506 	 *     parameterString = a string which is displayed in
507 	 *         the first line of `--help` output, after the usage summary
508 	 *         `programname [OPTION...]`
509 	 *
510 	 * Return: a newly created #GOptionContext, which must be
511 	 *     freed with g_option_context_free() after use.
512 	 *
513 	 * Since: 2.6
514 	 *
515 	 * Throws: ConstructionException GTK+ fails to create the object.
516 	 */
517 	public this(string parameterString)
518 	{
519 		auto p = g_option_context_new(Str.toStringz(parameterString));
520 		
521 		if(p is null)
522 		{
523 			throw new ConstructionException("null returned by new");
524 		}
525 		
526 		this(cast(GOptionContext*) p);
527 	}
528 
529 	public static GQuark optionErrorQuark()
530 	{
531 		return g_option_error_quark();
532 	}
533 }