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  * Conversion parameters:
26  * inFile  = glib-Commandline-option-parser.html
27  * outPack = glib
28  * outFile = OptionContext
29  * strct   = GOptionContext
30  * realStrct=
31  * ctorStrct=
32  * clss    = OptionContext
33  * interf  = 
34  * class Code: Yes
35  * interface Code: No
36  * template for:
37  * extend  = 
38  * implements:
39  * prefixes:
40  * 	- g_option_context_
41  * omit structs:
42  * omit prefixes:
43  * 	- g_option_group_
44  * omit code:
45  * omit signals:
46  * imports:
47  * 	- glib.Str
48  * 	- glib.ErrorG
49  * 	- glib.GException
50  * 	- glib.OptionGroup
51  * 	- gtkc.Loader
52  * 	- gtkc.paths
53  * structWrap:
54  * 	- GOptionGroup* -> OptionGroup
55  * module aliases:
56  * local aliases:
57  * overrides:
58  */
59 
60 module glib.OptionContext;
61 
62 public  import gtkc.glibtypes;
63 
64 private import gtkc.glib;
65 private import glib.ConstructionException;
66 
67 
68 private import glib.Str;
69 private import glib.ErrorG;
70 private import glib.GException;
71 private import glib.OptionGroup;
72 private import gtkc.Loader;
73 private import gtkc.paths;
74 
75 
76 
77 
78 /**
79  * The GOption commandline parser is intended to be a simpler replacement
80  * for the popt library. It supports short and long commandline options,
81  * as shown in the following example:
82  *
83  * testtreemodel -r 1 --max-size 20 --rand --display=:1.0 -vb -- file1 file2
84  *
85  * The example demonstrates a number of features of the GOption
86  * commandline parser
87  *
88  *  Options can be single letters, prefixed by a single dash. Multiple
89  *  short options can be grouped behind a single dash.
90  *
91  *  Long options are prefixed by two consecutive dashes.
92  *
93  *  Options can have an extra argument, which can be a number, a string or
94  *  a filename. For long options, the extra argument can be appended with
95  *  an equals sign after the option name, which is useful if the extra
96  *  argument starts with a dash, which would otherwise cause it to be
97  *  interpreted as another option.
98  *
99  *  Non-option arguments are returned to the application as rest arguments.
100  *
101  *  An argument consisting solely of two dashes turns off further parsing,
102  *  any remaining arguments (even those starting with a dash) are returned
103  *  to the application as rest arguments.
104  *
105  * Another important feature of GOption is that it can automatically
106  * generate nicely formatted help output. Unless it is explicitly turned
107  * off with g_option_context_set_help_enabled(), GOption will recognize
108  * the --help, -?,
109  * --help-all and
110  * --help-groupname options
111  * (where groupname is the name of a
112  * GOptionGroup) and write a text similar to the one shown in the
113  * following example to stdout.
114  *
115  * $(DDOC_COMMENT example)
116  *
117  * GOption groups options in GOptionGroups, which makes it easy to
118  * incorporate options from multiple sources. The intended use for this is
119  * to let applications collect option groups from the libraries it uses,
120  * add them to their GOptionContext, and parse all options by a single call
121  * to g_option_context_parse(). See gtk_get_option_group() for an example.
122  *
123  * If an option is declared to be of type string or filename, GOption takes
124  * care of converting it to the right encoding; strings are returned in
125  * UTF-8, filenames are returned in the GLib filename encoding. Note that
126  * this only works if setlocale() has been called before
127  * g_option_context_parse().
128  *
129  * Here is a complete example of setting up GOption to parse the example
130  * commandline above and produce the example help output.
131  *
132  * $(DDOC_COMMENT example)
133  */
134 public class OptionContext
135 {
136 	
137 	/** the main Gtk struct */
138 	protected GOptionContext* gOptionContext;
139 	
140 	
141 	public GOptionContext* getOptionContextStruct()
142 	{
143 		return gOptionContext;
144 	}
145 	
146 	
147 	/** the main Gtk struct as a void* */
148 	protected void* getStruct()
149 	{
150 		return cast(void*)gOptionContext;
151 	}
152 	
153 	/**
154 	 * Sets our main struct and passes it to the parent class
155 	 */
156 	public this (GOptionContext* gOptionContext)
157 	{
158 		this.gOptionContext = gOptionContext;
159 	}
160 	
161 	~this ()
162 	{
163 		if ( Linker.isLoaded(LIBRARY.GLIB) && gOptionContext !is null )
164 		{
165 			g_option_context_free(gOptionContext);
166 		}
167 	}
168 	
169 	/**
170 	 */
171 	
172 	/**
173 	 * Creates a new option context.
174 	 * The parameter_string can serve multiple purposes. It can be used
175 	 * to add descriptions for "rest" arguments, which are not parsed by
176 	 * the GOptionContext, typically something like "FILES" or
177 	 * "FILE1 FILE2...". If you are using G_OPTION_REMAINING for
178 	 * collecting "rest" arguments, GLib handles this automatically by
179 	 * using the arg_description of the corresponding GOptionEntry in
180 	 * the usage summary.
181 	 * Another usage is to give a short summary of the program
182 	 * functionality, like " - frob the strings", which will be displayed
183 	 * in the same line as the usage. For a longer description of the
184 	 * program functionality that should be displayed as a paragraph
185 	 * below the usage line, use g_option_context_set_summary().
186 	 * Note that the parameter_string is translated using the
187 	 * function set with g_option_context_set_translate_func(), so
188 	 * it should normally be passed untranslated.
189 	 * Since 2.6
190 	 * Params:
191 	 * parameterString = a string which is displayed in
192 	 * the first line of --help output, after the
193 	 * usage summary
194 	 * programname [OPTION...]. [allow-none]
195 	 * Throws: ConstructionException GTK+ fails to create the object.
196 	 */
197 	public this (string parameterString)
198 	{
199 		// GOptionContext * g_option_context_new (const gchar *parameter_string);
200 		auto p = g_option_context_new(Str.toStringz(parameterString));
201 		if(p is null)
202 		{
203 			throw new ConstructionException("null returned by g_option_context_new(Str.toStringz(parameterString))");
204 		}
205 		this(cast(GOptionContext*) p);
206 	}
207 	
208 	/**
209 	 * Adds a string to be displayed in --help output
210 	 * before the list of options. This is typically a summary of the
211 	 * program functionality.
212 	 * Note that the summary is translated (see
213 	 * g_option_context_set_translate_func() and
214 	 * g_option_context_set_translation_domain()).
215 	 * Since 2.12
216 	 * Params:
217 	 * summary = a string to be shown in --help output
218 	 * before the list of options, or NULL. [allow-none]
219 	 */
220 	public void setSummary(string summary)
221 	{
222 		// void g_option_context_set_summary (GOptionContext *context,  const gchar *summary);
223 		g_option_context_set_summary(gOptionContext, Str.toStringz(summary));
224 	}
225 	
226 	/**
227 	 * Returns the summary. See g_option_context_set_summary().
228 	 * Since 2.12
229 	 * Returns: the summary
230 	 */
231 	public string getSummary()
232 	{
233 		// const gchar * g_option_context_get_summary (GOptionContext *context);
234 		return Str.toString(g_option_context_get_summary(gOptionContext));
235 	}
236 	
237 	/**
238 	 * Adds a string to be displayed in --help output
239 	 * after the list of options. This text often includes a bug reporting
240 	 * address.
241 	 * Note that the summary is translated (see
242 	 * g_option_context_set_translate_func()).
243 	 * Since 2.12
244 	 * Params:
245 	 * description = a string to be shown in --help output
246 	 * after the list of options, or NULL. [allow-none]
247 	 */
248 	public void setDescription(string description)
249 	{
250 		// void g_option_context_set_description (GOptionContext *context,  const gchar *description);
251 		g_option_context_set_description(gOptionContext, Str.toStringz(description));
252 	}
253 	
254 	/**
255 	 * Returns the description. See g_option_context_set_description().
256 	 * Since 2.12
257 	 * Returns: the description
258 	 */
259 	public string getDescription()
260 	{
261 		// const gchar * g_option_context_get_description (GOptionContext *context);
262 		return Str.toString(g_option_context_get_description(gOptionContext));
263 	}
264 	
265 	/**
266 	 * Sets the function which is used to translate the contexts
267 	 * user-visible strings, for --help output.
268 	 * If func is NULL, strings are not translated.
269 	 * Note that option groups have their own translation functions,
270 	 * this function only affects the parameter_string (see g_option_context_new()),
271 	 * the summary (see g_option_context_set_summary()) and the description
272 	 * (see g_option_context_set_description()).
273 	 * If you are using gettext(), you only need to set the translation
274 	 * domain, see g_option_context_set_translation_domain().
275 	 * Since 2.12
276 	 * Params:
277 	 * func = the GTranslateFunc, or NULL. [allow-none]
278 	 * data = user data to pass to func, or NULL. [allow-none]
279 	 * destroyNotify = a function which gets called to free data, or NULL. [allow-none]
280 	 */
281 	public void setTranslateFunc(GTranslateFunc func, void* data, GDestroyNotify destroyNotify)
282 	{
283 		// void g_option_context_set_translate_func (GOptionContext *context,  GTranslateFunc func,  gpointer data,  GDestroyNotify destroy_notify);
284 		g_option_context_set_translate_func(gOptionContext, func, data, destroyNotify);
285 	}
286 	
287 	/**
288 	 * A convenience function to use gettext() for translating
289 	 * user-visible strings.
290 	 * Since 2.12
291 	 * Params:
292 	 * domain = the domain to use
293 	 */
294 	public void setTranslationDomain(string domain)
295 	{
296 		// void g_option_context_set_translation_domain  (GOptionContext *context,  const gchar *domain);
297 		g_option_context_set_translation_domain(gOptionContext, Str.toStringz(domain));
298 	}
299 	
300 	/**
301 	 * Frees context and all the groups which have been
302 	 * added to it.
303 	 * Please note that parsed arguments need to be freed separately (see
304 	 * GOptionEntry).
305 	 * Since 2.6
306 	 */
307 	public void free()
308 	{
309 		// void g_option_context_free (GOptionContext *context);
310 		g_option_context_free(gOptionContext);
311 	}
312 	
313 	/**
314 	 * Parses the command line arguments, recognizing options
315 	 * which have been added to context. A side-effect of
316 	 * calling this function is that g_set_prgname() will be
317 	 * called.
318 	 * If the parsing is successful, any parsed arguments are
319 	 * removed from the array and argc and argv are updated
320 	 * accordingly. A '--' option is stripped from argv
321 	 * unless there are unparsed options before and after it,
322 	 * or some of the options after it start with '-'. In case
323 	 * of an error, argc and argv are left unmodified.
324 	 * If automatic --help support is enabled
325 	 * (see g_option_context_set_help_enabled()), and the
326 	 * argv array contains one of the recognized help options,
327 	 * this function will produce help output to stdout and
328 	 * call exit (0).
329 	 * Note that function depends on the
330 	 * current locale for
331 	 * automatic character set conversion of string and filename
332 	 * arguments.
333 	 * Since 2.6
334 	 * Params:
335 	 * argv = a pointer to the array of command line arguments. [inout][array length=argc][allow-none]
336 	 * Returns: TRUE if the parsing was successful, FALSE if an error occurred
337 	 * Throws: GException on failure.
338 	 */
339 	public int parse(out string[] argv)
340 	{
341 		// gboolean g_option_context_parse (GOptionContext *context,  gint *argc,  gchar ***argv,  GError **error);
342 		char** outargv = null;
343 		int argc;
344 		GError* err = null;
345 		
346 		auto p = g_option_context_parse(gOptionContext, &argc, &outargv, &err);
347 		
348 		if (err !is null)
349 		{
350 			throw new GException( new ErrorG(err) );
351 		}
352 		
353 		argv = null;
354 		foreach ( cstr; outargv[0 .. argc] )
355 		{
356 			argv ~= Str.toString(cstr);
357 		}
358 		return p;
359 	}
360 	
361 	/**
362 	 * Enables or disables automatic generation of --help
363 	 * output. By default, g_option_context_parse() recognizes
364 	 * --help, -h,
365 	 * -?, --help-all
366 	 * and --help-groupname and creates
367 	 * suitable output to stdout.
368 	 * Since 2.6
369 	 * Params:
370 	 * helpEnabled = TRUE to enable --help, FALSE to disable it
371 	 */
372 	public void setHelpEnabled(int helpEnabled)
373 	{
374 		// void g_option_context_set_help_enabled (GOptionContext *context,  gboolean help_enabled);
375 		g_option_context_set_help_enabled(gOptionContext, helpEnabled);
376 	}
377 	
378 	/**
379 	 * Returns whether automatic --help generation
380 	 * is turned on for context. See g_option_context_set_help_enabled().
381 	 * Since 2.6
382 	 * Returns: TRUE if automatic help generation is turned on.
383 	 */
384 	public int getHelpEnabled()
385 	{
386 		// gboolean g_option_context_get_help_enabled (GOptionContext *context);
387 		return g_option_context_get_help_enabled(gOptionContext);
388 	}
389 	
390 	/**
391 	 * Sets whether to ignore unknown options or not. If an argument is
392 	 * ignored, it is left in the argv array after parsing. By default,
393 	 * g_option_context_parse() treats unknown options as error.
394 	 * This setting does not affect non-option arguments (i.e. arguments
395 	 * which don't start with a dash). But note that GOption cannot reliably
396 	 * determine whether a non-option belongs to a preceding unknown option.
397 	 * Since 2.6
398 	 * Params:
399 	 * ignoreUnknown = TRUE to ignore unknown options, FALSE to produce
400 	 * an error when unknown options are met
401 	 */
402 	public void setIgnoreUnknownOptions(int ignoreUnknown)
403 	{
404 		// void g_option_context_set_ignore_unknown_options  (GOptionContext *context,  gboolean ignore_unknown);
405 		g_option_context_set_ignore_unknown_options(gOptionContext, ignoreUnknown);
406 	}
407 	
408 	/**
409 	 * Returns whether unknown options are ignored or not. See
410 	 * g_option_context_set_ignore_unknown_options().
411 	 * Since 2.6
412 	 * Returns: TRUE if unknown options are ignored.
413 	 */
414 	public int getIgnoreUnknownOptions()
415 	{
416 		// gboolean g_option_context_get_ignore_unknown_options  (GOptionContext *context);
417 		return g_option_context_get_ignore_unknown_options(gOptionContext);
418 	}
419 	
420 	/**
421 	 * Returns a formatted, translated help text for the given context.
422 	 * To obtain the text produced by --help, call
423 	 * g_option_context_get_help (context, TRUE, NULL).
424 	 * To obtain the text produced by --help-all, call
425 	 * g_option_context_get_help (context, FALSE, NULL).
426 	 * To obtain the help text for an option group, call
427 	 * g_option_context_get_help (context, FALSE, group).
428 	 * Since 2.14
429 	 * Params:
430 	 * mainHelp = if TRUE, only include the main group
431 	 * group = the GOptionGroup to create help for, or NULL. [allow-none]
432 	 * Returns: A newly allocated string containing the help text
433 	 */
434 	public string getHelp(int mainHelp, OptionGroup group)
435 	{
436 		// gchar * g_option_context_get_help (GOptionContext *context,  gboolean main_help,  GOptionGroup *group);
437 		return Str.toString(g_option_context_get_help(gOptionContext, mainHelp, (group is null) ? null : group.getOptionGroupStruct()));
438 	}
439 	
440 	/**
441 	 * A convenience function which creates a main group if it doesn't
442 	 * exist, adds the entries to it and sets the translation domain.
443 	 * Since 2.6
444 	 * Params:
445 	 * entries = a NULL-terminated array of GOptionEntrys
446 	 * translationDomain = a translation domain to use for translating
447 	 * the --help output for the options in entries
448 	 * with gettext(), or NULL. [allow-none]
449 	 */
450 	public void addMainEntries(GOptionEntry* entries, string translationDomain)
451 	{
452 		// void g_option_context_add_main_entries (GOptionContext *context,  const GOptionEntry *entries,  const gchar *translation_domain);
453 		g_option_context_add_main_entries(gOptionContext, entries, Str.toStringz(translationDomain));
454 	}
455 	
456 	/**
457 	 * Adds a GOptionGroup to the context, so that parsing with context
458 	 * will recognize the options in the group. Note that the group will
459 	 * be freed together with the context when g_option_context_free() is
460 	 * called, so you must not free the group yourself after adding it
461 	 * to a context.
462 	 * Since 2.6
463 	 * Params:
464 	 * group = the group to add
465 	 */
466 	public void addGroup(OptionGroup group)
467 	{
468 		// void g_option_context_add_group (GOptionContext *context,  GOptionGroup *group);
469 		g_option_context_add_group(gOptionContext, (group is null) ? null : group.getOptionGroupStruct());
470 	}
471 	
472 	/**
473 	 * Sets a GOptionGroup as main group of the context.
474 	 * This has the same effect as calling g_option_context_add_group(),
475 	 * the only difference is that the options in the main group are
476 	 * treated differently when generating --help output.
477 	 * Since 2.6
478 	 * Params:
479 	 * group = the group to set as main group
480 	 */
481 	public void setMainGroup(OptionGroup group)
482 	{
483 		// void g_option_context_set_main_group (GOptionContext *context,  GOptionGroup *group);
484 		g_option_context_set_main_group(gOptionContext, (group is null) ? null : group.getOptionGroupStruct());
485 	}
486 	
487 	/**
488 	 * Returns a pointer to the main group of context.
489 	 * Since 2.6
490 	 * Returns: the main group of context, or NULL if context doesn't have a main group. Note that group belongs to context and should not be modified or freed.
491 	 */
492 	public OptionGroup getMainGroup()
493 	{
494 		// GOptionGroup * g_option_context_get_main_group (GOptionContext *context);
495 		auto p = g_option_context_get_main_group(gOptionContext);
496 		
497 		if(p is null)
498 		{
499 			return null;
500 		}
501 		
502 		return new OptionGroup(cast(GOptionGroup*) p);
503 	}
504 }