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