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