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 protected bool ownedRef; 46 47 /** Get the main Gtk struct */ 48 public GOptionContext* getOptionContextStruct() 49 { 50 return gOptionContext; 51 } 52 53 /** the main Gtk struct as a void* */ 54 protected void* getStruct() 55 { 56 return cast(void*)gOptionContext; 57 } 58 59 /** 60 * Sets our main struct and passes it to the parent class. 61 */ 62 public this (GOptionContext* gOptionContext, bool ownedRef = false) 63 { 64 this.gOptionContext = gOptionContext; 65 this.ownedRef = ownedRef; 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 auto retStr = g_option_context_get_help(gOptionContext, mainHelp, (group is null) ? null : group.getOptionGroupStruct()); 147 148 scope(exit) Str.freeString(retStr); 149 return Str.toString(retStr); 150 } 151 152 /** 153 * Returns whether automatic `--help` generation 154 * is turned on for @context. See g_option_context_set_help_enabled(). 155 * 156 * Return: %TRUE if automatic help generation is turned on. 157 * 158 * Since: 2.6 159 */ 160 public bool getHelpEnabled() 161 { 162 return g_option_context_get_help_enabled(gOptionContext) != 0; 163 } 164 165 /** 166 * Returns whether unknown options are ignored or not. See 167 * g_option_context_set_ignore_unknown_options(). 168 * 169 * Return: %TRUE if unknown options are ignored. 170 * 171 * Since: 2.6 172 */ 173 public bool getIgnoreUnknownOptions() 174 { 175 return g_option_context_get_ignore_unknown_options(gOptionContext) != 0; 176 } 177 178 /** 179 * Returns a pointer to the main group of @context. 180 * 181 * Return: the main group of @context, or %NULL if 182 * @context doesn't have a main group. Note that group belongs to 183 * @context and should not be modified or freed. 184 * 185 * Since: 2.6 186 */ 187 public OptionGroup getMainGroup() 188 { 189 auto p = g_option_context_get_main_group(gOptionContext); 190 191 if(p is null) 192 { 193 return null; 194 } 195 196 return new OptionGroup(cast(GOptionGroup*) p); 197 } 198 199 /** 200 * Returns whether strict POSIX code is enabled. 201 * 202 * See g_option_context_set_strict_posix() for more information. 203 * 204 * Return: %TRUE if strict POSIX is enabled, %FALSE otherwise. 205 * 206 * Since: 2.44 207 */ 208 public bool getStrictPosix() 209 { 210 return g_option_context_get_strict_posix(gOptionContext) != 0; 211 } 212 213 /** 214 * Returns the summary. See g_option_context_set_summary(). 215 * 216 * Return: the summary 217 * 218 * Since: 2.12 219 */ 220 public string getSummary() 221 { 222 return Str.toString(g_option_context_get_summary(gOptionContext)); 223 } 224 225 /** 226 * Parses the command line arguments, recognizing options 227 * which have been added to @context. A side-effect of 228 * calling this function is that g_set_prgname() will be 229 * called. 230 * 231 * If the parsing is successful, any parsed arguments are 232 * removed from the array and @argc and @argv are updated 233 * accordingly. A '--' option is stripped from @argv 234 * unless there are unparsed options before and after it, 235 * or some of the options after it start with '-'. In case 236 * of an error, @argc and @argv are left unmodified. 237 * 238 * If automatic `--help` support is enabled 239 * (see g_option_context_set_help_enabled()), and the 240 * @argv array contains one of the recognized help options, 241 * this function will produce help output to stdout and 242 * call `exit (0)`. 243 * 244 * Note that function depends on the [current locale][setlocale] for 245 * automatic character set conversion of string and filename 246 * arguments. 247 * 248 * Params: 249 * argc = a pointer to the number of command line arguments 250 * argv = a pointer to the array of command line arguments 251 * 252 * Return: %TRUE if the parsing was successful, 253 * %FALSE if an error occurred 254 * 255 * Since: 2.6 256 * 257 * Throws: GException on failure. 258 */ 259 public bool parse(ref string[] argv) 260 { 261 int argc = cast(int)argv.length; 262 char** outargv = Str.toStringzArray(argv); 263 GError* err = null; 264 265 auto p = g_option_context_parse(gOptionContext, &argc, &outargv, &err) != 0; 266 267 if (err !is null) 268 { 269 throw new GException( new ErrorG(err) ); 270 } 271 272 argv = Str.toStringArray(outargv, argc); 273 274 return p; 275 } 276 277 /** 278 * Parses the command line arguments. 279 * 280 * This function is similar to g_option_context_parse() except that it 281 * respects the normal memory rules when dealing with a strv instead of 282 * assuming that the passed-in array is the argv of the main function. 283 * 284 * In particular, strings that are removed from the arguments list will 285 * be freed using g_free(). 286 * 287 * On Windows, the strings are expected to be in UTF-8. This is in 288 * contrast to g_option_context_parse() which expects them to be in the 289 * system codepage, which is how they are passed as @argv to main(). 290 * See g_win32_get_command_line() for a solution. 291 * 292 * This function is useful if you are trying to use #GOptionContext with 293 * #GApplication. 294 * 295 * Params: 296 * arguments = a pointer to the 297 * command line arguments (which must be in UTF-8 on Windows) 298 * 299 * Return: %TRUE if the parsing was successful, 300 * %FALSE if an error occurred 301 * 302 * Since: 2.40 303 * 304 * Throws: GException on failure. 305 */ 306 public bool parseStrv(ref string[] arguments) 307 { 308 char** outarguments = Str.toStringzArray(arguments); 309 GError* err = null; 310 311 auto p = g_option_context_parse_strv(gOptionContext, &outarguments, &err) != 0; 312 313 if (err !is null) 314 { 315 throw new GException( new ErrorG(err) ); 316 } 317 318 arguments = Str.toStringArray(outarguments); 319 320 return p; 321 } 322 323 /** 324 * Adds a string to be displayed in `--help` output after the list 325 * of options. This text often includes a bug reporting address. 326 * 327 * Note that the summary is translated (see 328 * g_option_context_set_translate_func()). 329 * 330 * Params: 331 * description = a string to be shown in `--help` output 332 * after the list of options, or %NULL 333 * 334 * Since: 2.12 335 */ 336 public void setDescription(string description) 337 { 338 g_option_context_set_description(gOptionContext, Str.toStringz(description)); 339 } 340 341 /** 342 * Enables or disables automatic generation of `--help` output. 343 * By default, g_option_context_parse() recognizes `--help`, `-h`, 344 * `-?`, `--help-all` and `--help-groupname` and creates suitable 345 * output to stdout. 346 * 347 * Params: 348 * helpEnabled = %TRUE to enable `--help`, %FALSE to disable it 349 * 350 * Since: 2.6 351 */ 352 public void setHelpEnabled(bool helpEnabled) 353 { 354 g_option_context_set_help_enabled(gOptionContext, helpEnabled); 355 } 356 357 /** 358 * Sets whether to ignore unknown options or not. If an argument is 359 * ignored, it is left in the @argv array after parsing. By default, 360 * g_option_context_parse() treats unknown options as error. 361 * 362 * This setting does not affect non-option arguments (i.e. arguments 363 * which don't start with a dash). But note that GOption cannot reliably 364 * determine whether a non-option belongs to a preceding unknown option. 365 * 366 * Params: 367 * ignoreUnknown = %TRUE to ignore unknown options, %FALSE to produce 368 * an error when unknown options are met 369 * 370 * Since: 2.6 371 */ 372 public void setIgnoreUnknownOptions(bool ignoreUnknown) 373 { 374 g_option_context_set_ignore_unknown_options(gOptionContext, ignoreUnknown); 375 } 376 377 /** 378 * Sets a #GOptionGroup as main group of the @context. 379 * This has the same effect as calling g_option_context_add_group(), 380 * the only difference is that the options in the main group are 381 * treated differently when generating `--help` output. 382 * 383 * Params: 384 * group = the group to set as main group 385 * 386 * Since: 2.6 387 */ 388 public void setMainGroup(OptionGroup group) 389 { 390 g_option_context_set_main_group(gOptionContext, (group is null) ? null : group.getOptionGroupStruct()); 391 } 392 393 /** 394 * Sets strict POSIX mode. 395 * 396 * By default, this mode is disabled. 397 * 398 * In strict POSIX mode, the first non-argument parameter encountered 399 * (eg: filename) terminates argument processing. Remaining arguments 400 * are treated as non-options and are not attempted to be parsed. 401 * 402 * If strict POSIX mode is disabled then parsing is done in the GNU way 403 * where option arguments can be freely mixed with non-options. 404 * 405 * As an example, consider "ls foo -l". With GNU style parsing, this 406 * will list "foo" in long mode. In strict POSIX style, this will list 407 * the files named "foo" and "-l". 408 * 409 * It may be useful to force strict POSIX mode when creating "verb 410 * style" command line tools. For example, the "gsettings" command line 411 * tool supports the global option "--schemadir" as well as many 412 * subcommands ("get", "set", etc.) which each have their own set of 413 * arguments. Using strict POSIX mode will allow parsing the global 414 * options up to the verb name while leaving the remaining options to be 415 * parsed by the relevant subcommand (which can be determined by 416 * examining the verb name, which should be present in argv[1] after 417 * parsing). 418 * 419 * Params: 420 * strictPosix = the new value 421 * 422 * Since: 2.44 423 */ 424 public void setStrictPosix(bool strictPosix) 425 { 426 g_option_context_set_strict_posix(gOptionContext, strictPosix); 427 } 428 429 /** 430 * Adds a string to be displayed in `--help` output before the list 431 * of options. This is typically a summary of the program functionality. 432 * 433 * Note that the summary is translated (see 434 * g_option_context_set_translate_func() and 435 * g_option_context_set_translation_domain()). 436 * 437 * Params: 438 * summary = a string to be shown in `--help` output 439 * before the list of options, or %NULL 440 * 441 * Since: 2.12 442 */ 443 public void setSummary(string summary) 444 { 445 g_option_context_set_summary(gOptionContext, Str.toStringz(summary)); 446 } 447 448 /** 449 * Sets the function which is used to translate the contexts 450 * user-visible strings, for `--help` output. If @func is %NULL, 451 * strings are not translated. 452 * 453 * Note that option groups have their own translation functions, 454 * this function only affects the @parameter_string (see g_option_context_new()), 455 * the summary (see g_option_context_set_summary()) and the description 456 * (see g_option_context_set_description()). 457 * 458 * If you are using gettext(), you only need to set the translation 459 * domain, see g_option_context_set_translation_domain(). 460 * 461 * Params: 462 * func = the #GTranslateFunc, or %NULL 463 * data = user data to pass to @func, or %NULL 464 * destroyNotify = a function which gets called to free @data, or %NULL 465 * 466 * Since: 2.12 467 */ 468 public void setTranslateFunc(GTranslateFunc func, void* data, GDestroyNotify destroyNotify) 469 { 470 g_option_context_set_translate_func(gOptionContext, func, data, destroyNotify); 471 } 472 473 /** 474 * A convenience function to use gettext() for translating 475 * user-visible strings. 476 * 477 * Params: 478 * domain = the domain to use 479 * 480 * Since: 2.12 481 */ 482 public void setTranslationDomain(string domain) 483 { 484 g_option_context_set_translation_domain(gOptionContext, Str.toStringz(domain)); 485 } 486 487 /** 488 * Creates a new option context. 489 * 490 * The @parameter_string can serve multiple purposes. It can be used 491 * to add descriptions for "rest" arguments, which are not parsed by 492 * the #GOptionContext, typically something like "FILES" or 493 * "FILE1 FILE2...". If you are using #G_OPTION_REMAINING for 494 * collecting "rest" arguments, GLib handles this automatically by 495 * using the @arg_description of the corresponding #GOptionEntry in 496 * the usage summary. 497 * 498 * Another usage is to give a short summary of the program 499 * functionality, like " - frob the strings", which will be displayed 500 * in the same line as the usage. For a longer description of the 501 * program functionality that should be displayed as a paragraph 502 * below the usage line, use g_option_context_set_summary(). 503 * 504 * Note that the @parameter_string is translated using the 505 * function set with g_option_context_set_translate_func(), so 506 * it should normally be passed untranslated. 507 * 508 * Params: 509 * parameterString = a string which is displayed in 510 * the first line of `--help` output, after the usage summary 511 * `programname [OPTION...]` 512 * 513 * Return: a newly created #GOptionContext, which must be 514 * freed with g_option_context_free() after use. 515 * 516 * Since: 2.6 517 * 518 * Throws: ConstructionException GTK+ fails to create the object. 519 */ 520 public this(string parameterString) 521 { 522 auto p = g_option_context_new(Str.toStringz(parameterString)); 523 524 if(p is null) 525 { 526 throw new ConstructionException("null returned by new"); 527 } 528 529 this(cast(GOptionContext*) p); 530 } 531 532 /** */ 533 public static GQuark optionErrorQuark() 534 { 535 return g_option_error_quark(); 536 } 537 }