1 <!-- ##### SECTION Title ##### --> 2 Commandline option parser 3 4 <!-- ##### SECTION Short_Description ##### --> 5 parses commandline options 6 7 <!-- ##### SECTION Long_Description ##### --> 8 <para> 9 The GOption commandline parser is intended to be a simpler replacement for the 10 popt library. It supports short and long commandline options, as shown in the 11 following example: 12 </para> 13 14 <para> 15 <literal>testtreemodel -r 1 --max-size 20 --rand --display=:1.0 -vb -- file1 file2</literal> 16 </para> 17 18 <para> 19 The example demonstrates a number of features of the GOption commandline parser 20 <itemizedlist> 21 <listitem><para> 22 Options can be single letters, prefixed by a single dash. Multiple 23 short options can be grouped behind a single dash. 24 </para></listitem> 25 <listitem><para> 26 Long options are prefixed by two consecutive dashes. 27 </para></listitem> 28 <listitem><para> 29 Options can have an extra argument, which can be a number, a string or a 30 filename. For long options, the extra argument can be appended with an 31 equals sign after the option name. 32 </para></listitem> 33 <listitem><para> 34 Non-option arguments are returned to the application as rest arguments. 35 </para></listitem> 36 <listitem><para> 37 An argument consisting solely of two dashes turns off further parsing, 38 any remaining arguments (even those starting with a dash) are returned 39 to the application as rest arguments. 40 </para></listitem> 41 </itemizedlist> 42 </para> 43 44 <para> 45 Another important feature of GOption is that it can automatically generate 46 nicely formatted help output. Unless it is explicitly turned off with 47 g_option_context_set_help_enabled(), GOption will recognize the 48 <option>--help</option>, <option>-?</option>, <option>--help-all</option> 49 and <option>--help-</option><replaceable>groupname</replaceable> options 50 (where <replaceable>groupname</replaceable> is the name of a #GOptionGroup) 51 and write a text similar to the one shown in the following example to stdout. 52 </para> 53 54 <informalexample><screen> 55 Usage: 56 testtreemodel [OPTION...] - test tree model performance 57 58 Help Options: 59 -?, --help Show help options 60 --help-all Show all help options 61 --help-gtk Show GTK+ Options 62 63 Application Options: 64 -r, --repeats=N Average over N repetitions 65 -m, --max-size=M Test up to 2^M items 66 --display=DISPLAY X display to use 67 -v, --verbose Be verbose 68 -b, --beep Beep when done 69 --rand Randomize the data 70 </screen></informalexample> 71 72 <para> 73 GOption groups options in #GOptionGroup<!-- -->s, which makes it easy to 74 incorporate options from multiple sources. The intended use for this is 75 to let applications collect option groups from the libraries it uses, 76 add them to their #GOptionContext, and parse all options by a single call 77 to g_option_context_parse(). See gtk_get_option_group() for an example. 78 </para> 79 80 <para> 81 If an option is declared to be of type string or filename, GOption takes 82 care of converting it to the right encoding; strings are returned in UTF-8, 83 filenames are returned in the GLib filename encoding. Note that this only 84 works if setlocale() has been called before g_option_context_parse(). 85 </para> 86 87 <para> 88 Here is a complete example of setting up GOption to parse the example 89 commandline above and produce the example help output. 90 </para> 91 <informalexample><programlisting> 92 static gint repeats = 2; 93 static gint max_size = 8; 94 static gboolean verbose = FALSE; 95 static gboolean beep = FALSE; 96 static gboolean rand = FALSE; 97 98 static GOptionEntry entries[] = 99 { 100 { "repeats", 'r', 0, G_OPTION_ARG_INT, &repeats, "Average over N repetitions", "N" }, 101 { "max-size", 'm', 0, G_OPTION_ARG_INT, &max_size, "Test up to 2^M items", "M" }, 102 { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "Be verbose", NULL }, 103 { "beep", 'b', 0, G_OPTION_ARG_NONE, &beep, "Beep when done", NULL }, 104 { "rand", 0, 0, G_OPTION_ARG_NONE, &rand, "Randomize the data", NULL }, 105 { NULL } 106 }; 107 108 int 109 main (int argc, char *argv[]) 110 { 111 GError *error = NULL; 112 GOptionContext *context; 113 114 context = g_option_context_new ("- test tree model performance"); 115 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE); 116 g_option_context_add_group (context, gtk_get_option_group (TRUE)); 117 if (!g_option_context_parse (context, &argc, &argv, &error)) 118 { 119 g_print ("option parsing failed: %s\n", error->message); 120 exit (1); 121 } 122 123 /* ... */ 124 125 } 126 </programlisting></informalexample> 127 128 <!-- ##### SECTION See_Also ##### --> 129 <para> 130 131 </para> 132 133 <!-- ##### SECTION Stability_Level ##### --> 134 135 136 <!-- ##### ENUM GOptionError ##### --> 137 <para> 138 Error codes returned by option parsing. 139 </para> 140 141 @G_OPTION_ERROR_UNKNOWN_OPTION: An option was not known to the parser. 142 This error will only be reported, if the parser hasn't been instructed 143 to ignore unknown options, see g_option_context_set_ignore_unknown_options(). 144 @G_OPTION_ERROR_BAD_VALUE: A value couldn't be parsed. 145 @G_OPTION_ERROR_FAILED: A #GOptionArgFunc callback failed. 146 147 <!-- ##### MACRO G_OPTION_ERROR ##### --> 148 <para> 149 Error domain for option parsing. Errors in this domain will 150 be from the #GOptionError enumeration. See #GError for information on 151 error domains. 152 </para> 153 154 155 156 <!-- ##### USER_FUNCTION GOptionArgFunc ##### --> 157 <para> 158 The type of function to be passed as callback for %G_OPTION_ARG_CALLBACK 159 options. 160 </para> 161 162 @option_name: The name of the option being parsed. This will be either a 163 single dash followed by a single letter (for a short name) or two dashes 164 followed by a long option name. 165 @value: The value to be parsed. 166 @data: User data added to the #GOptionGroup containing the option when it 167 was created with g_option_group_new() 168 @error: A return location for errors. The error code %G_OPTION_ERROR_FAILED 169 is intended to be used for errors in #GOptionArgFunc callbacks. 170 @Returns: %TRUE if the option was successfully parsed, %FALSE if an error 171 occurred, in which case @error should be set with g_set_error() 172 173 174 <!-- ##### STRUCT GOptionContext ##### --> 175 <para> 176 A <structname>GOptionContext</structname> struct defines which options 177 are accepted by the commandline option parser. The struct has only private 178 fields and should not be directly accessed. 179 </para> 180 181 182 <!-- ##### FUNCTION g_option_context_new ##### --> 183 <para> 184 185 </para> 186 187 @parameter_string: 188 @Returns: 189 190 191 <!-- ##### FUNCTION g_option_context_set_summary ##### --> 192 <para> 193 194 </para> 195 196 @context: 197 @summary: 198 199 200 <!-- ##### FUNCTION g_option_context_get_summary ##### --> 201 <para> 202 203 </para> 204 205 @context: 206 @Returns: 207 208 209 <!-- ##### FUNCTION g_option_context_set_description ##### --> 210 <para> 211 212 </para> 213 214 @context: 215 @description: 216 217 218 <!-- ##### FUNCTION g_option_context_get_description ##### --> 219 <para> 220 221 </para> 222 223 @context: 224 @Returns: 225 226 227 <!-- ##### USER_FUNCTION GTranslateFunc ##### --> 228 <para> 229 The type of functions which are used to translate user-visible 230 strings, for <option>--help</option> output. 231 </para> 232 233 @str: the untranslated string 234 @data: user data specified when installing the function, e.g. 235 in g_option_group_set_translate_func() 236 @Returns: a translation of the string for the current locale. 237 The returned string is owned by GLib and must not be freed. 238 239 240 <!-- ##### FUNCTION g_option_context_set_translate_func ##### --> 241 <para> 242 243 </para> 244 245 @context: 246 @func: 247 @data: 248 @destroy_notify: 249 250 251 <!-- ##### FUNCTION g_option_context_set_translation_domain ##### --> 252 <para> 253 254 </para> 255 256 @context: 257 @domain: 258 259 260 <!-- ##### FUNCTION g_option_context_free ##### --> 261 <para> 262 263 </para> 264 265 @context: 266 267 268 <!-- ##### FUNCTION g_option_context_parse ##### --> 269 <para> 270 271 </para> 272 273 @context: 274 @argc: 275 @argv: 276 @error: 277 @Returns: 278 279 280 <!-- ##### FUNCTION g_option_context_set_help_enabled ##### --> 281 <para> 282 283 </para> 284 285 @context: 286 @help_enabled: 287 288 289 <!-- ##### FUNCTION g_option_context_get_help_enabled ##### --> 290 <para> 291 292 </para> 293 294 @context: 295 @Returns: 296 297 298 <!-- ##### FUNCTION g_option_context_set_ignore_unknown_options ##### --> 299 <para> 300 301 </para> 302 303 @context: 304 @ignore_unknown: 305 306 307 <!-- ##### FUNCTION g_option_context_get_ignore_unknown_options ##### --> 308 <para> 309 310 </para> 311 312 @context: 313 @Returns: 314 315 316 <!-- ##### FUNCTION g_option_context_get_help ##### --> 317 <para> 318 319 </para> 320 321 @context: 322 @main_help: 323 @group: 324 @Returns: 325 326 327 <!-- ##### ENUM GOptionArg ##### --> 328 <para> 329 The #GOptionArg enum values determine which type of extra argument the 330 options expect to find. If an option expects an extra argument, it 331 can be specified in several ways; with a short option: 332 <option>-x arg</option>, with a long option: <option>--name arg</option> 333 or combined in a single argument: <option>--name=arg</option>. 334 </para> 335 336 @G_OPTION_ARG_NONE: No extra argument. This is useful for simple flags. 337 @G_OPTION_ARG_STRING: The option takes a string argument. 338 @G_OPTION_ARG_INT: The option takes an integer argument. 339 @G_OPTION_ARG_CALLBACK: The option provides a callback to parse the 340 extra argument. 341 @G_OPTION_ARG_FILENAME: The option takes a filename as argument. 342 @G_OPTION_ARG_STRING_ARRAY: The option takes a string argument, multiple 343 uses of the option are collected into an array of strings. 344 @G_OPTION_ARG_FILENAME_ARRAY: The option takes a filename as argument, 345 multiple uses of the option are collected into an array of strings. 346 @G_OPTION_ARG_DOUBLE: The option takes a double argument. The argument 347 can be formatted either for the user's locale or for the "C" locale. Since 2.12 348 @G_OPTION_ARG_INT64: The option takes a 64-bit integer. Like %G_OPTION_ARG_INT 349 but for larger numbers. The number can be in decimal base, or in hexadecimal 350 (when prefixed with <literal>0x</literal>, for example, <literal>0xffffffff</literal>). 351 Since 2.12 352 353 <!-- ##### ENUM GOptionFlags ##### --> 354 <para> 355 Flags which modify individual options. 356 </para> 357 358 @G_OPTION_FLAG_HIDDEN: The option doesn't appear in <option>--help</option> 359 output. 360 @G_OPTION_FLAG_IN_MAIN: The option appears in the main section of the 361 <option>--help</option> output, even if it is defined in a group. 362 @G_OPTION_FLAG_REVERSE: For options of the %G_OPTION_ARG_NONE kind, this flag 363 indicates that the sense of the option is reversed. 364 @G_OPTION_FLAG_NO_ARG: For options of the %G_OPTION_ARG_CALLBACK kind, 365 this flag indicates that the callback does not take any argument 366 (like a %G_OPTION_ARG_NONE option). Since 2.8 367 @G_OPTION_FLAG_FILENAME: For options of the %G_OPTION_ARG_CALLBACK 368 kind, this flag indicates that the argument should be passed to the 369 callback in the GLib filename encoding rather than UTF-8. Since 2.8 370 @G_OPTION_FLAG_OPTIONAL_ARG: For options of the %G_OPTION_ARG_CALLBACK 371 kind, this flag indicates that the argument supply is optional. If no argument 372 is given then data of %GOptionParseFunc will be set to NULL. Since 2.8 373 @G_OPTION_FLAG_NOALIAS: This flag turns off the automatic conflict resolution 374 which prefixes long option names with <literal>groupname-</literal> if 375 there is a conflict. This option should only be used in situations where 376 aliasing is necessary to model some legacy commandline interface. It is 377 not safe to use this option, unless all option groups are under your 378 direct control. Since 2.8. 379 380 <!-- ##### MACRO G_OPTION_REMAINING ##### --> 381 <para> 382 If a long option in the main group has this name, it is not treated as a 383 regular option. Instead it collects all non-option arguments which would 384 otherwise be left in <literal>argv</literal>. The option must be of type 385 %G_OPTION_ARG_CALLBACK, %G_OPTION_ARG_STRING_ARRAY 386 or %G_OPTION_ARG_FILENAME_ARRAY. 387 </para> 388 389 <para> 390 Using #G_OPTION_REMAINING instead of simply scanning <literal>argv</literal> 391 for leftover arguments has the advantage that GOption takes care of 392 necessary encoding conversions for strings or filenames. 393 </para> 394 395 @Since: 2.6 396 397 398 <!-- ##### STRUCT GOptionEntry ##### --> 399 <para> 400 A <structname>GOptionEntry</structname> defines a single option. 401 To have an effect, they must be added to a #GOptionGroup with 402 g_option_context_add_main_entries() or g_option_group_add_entries(). 403 </para> 404 405 @long_name: The long name of an option can be used to specify it 406 in a commandline as --<replaceable>long_name</replaceable>. Every 407 option must have a long name. To resolve conflicts if multiple 408 option groups contain the same long name, it is also possible to 409 specify the option as 410 --<replaceable>groupname</replaceable>-<replaceable>long_name</replaceable>. 411 @short_name: If an option has a short name, it can be specified 412 -<replaceable>short_name</replaceable> in a commandline. @short_name must be 413 a printable ASCII character different from '-', or zero if the option has no 414 short name. 415 @flags: Flags from #GOptionFlags. 416 @arg: The type of the option, as a #GOptionArg. 417 @arg_data: If the @arg type is %G_OPTION_ARG_CALLBACK, then @arg_data must 418 point to a #GOptionArgFunc callback function, which will be called to handle 419 the extra argument. Otherwise, @arg_data is a pointer to a location to store 420 the value, the required type of the location depends on the @arg type: 421 <variablelist> 422 <varlistentry> 423 <term>%G_OPTION_ARG_NONE</term> 424 <listitem><para>%gboolean</para></listitem> 425 </varlistentry> 426 <varlistentry> 427 <term>%G_OPTION_ARG_STRING</term> 428 <listitem><para>%gchar*</para></listitem> 429 </varlistentry> 430 <varlistentry> 431 <term>%G_OPTION_ARG_INT</term> 432 <listitem><para>%gint</para></listitem> 433 </varlistentry> 434 <varlistentry> 435 <term>%G_OPTION_ARG_FILENAME</term> 436 <listitem><para>%gchar*</para></listitem> 437 </varlistentry> 438 <varlistentry> 439 <term>%G_OPTION_ARG_STRING_ARRAY</term> 440 <listitem><para>%gchar**</para></listitem> 441 </varlistentry> 442 <varlistentry> 443 <term>%G_OPTION_ARG_FILENAME_ARRAY</term> 444 <listitem><para>%gchar**</para></listitem> 445 </varlistentry> 446 <varlistentry> 447 <term>%G_OPTION_ARG_DOUBLE</term> 448 <listitem><para>%gdouble</para></listitem> 449 </varlistentry> 450 </variablelist> 451 @description: the description for the option in <option>--help</option> 452 output. The @description is translated using the @translate_func of the 453 group, see g_option_group_set_translation_domain(). 454 @arg_description: The placeholder to use for the extra argument parsed 455 by the option in <option>--help</option> 456 output. The @arg_description is translated using the @translate_func of the 457 group, see g_option_group_set_translation_domain(). 458 459 <!-- ##### FUNCTION g_option_context_add_main_entries ##### --> 460 <para> 461 462 </para> 463 464 @context: 465 @entries: 466 @translation_domain: 467 468 469 <!-- ##### STRUCT GOptionGroup ##### --> 470 <para> 471 A <structname>GOptionGroup</structname> struct defines the options in a single 472 group. The struct has only private fields and should not be directly accessed. 473 </para> 474 <para> 475 All options in a group share the same translation function. Libaries which 476 need to parse commandline options are expected to provide a function for 477 getting a <structname>GOptionGroup</structname> holding their options, which 478 the application can then add to its #GOptionContext. 479 </para> 480 481 482 <!-- ##### FUNCTION g_option_context_add_group ##### --> 483 <para> 484 485 </para> 486 487 @context: 488 @group: 489 490 491 <!-- ##### FUNCTION g_option_context_set_main_group ##### --> 492 <para> 493 494 </para> 495 496 @context: 497 @group: 498 499 500 <!-- ##### FUNCTION g_option_context_get_main_group ##### --> 501 <para> 502 503 </para> 504 505 @context: 506 @Returns: 507 508 509 <!-- ##### FUNCTION g_option_group_new ##### --> 510 <para> 511 512 </para> 513 514 @name: 515 @description: 516 @help_description: 517 @user_data: 518 @destroy: 519 @Returns: 520 521 522 <!-- ##### FUNCTION g_option_group_free ##### --> 523 <para> 524 525 </para> 526 527 @group: 528 529 530 <!-- ##### FUNCTION g_option_group_add_entries ##### --> 531 <para> 532 533 </para> 534 535 @group: 536 @entries: 537 538 539 <!-- ##### USER_FUNCTION GOptionParseFunc ##### --> 540 <para> 541 The type of function that can be called before and after parsing. 542 </para> 543 544 @context: The active #GOptionContext 545 @group: The group to which the function belongs 546 @data: User data added to the #GOptionGroup containing the option when it 547 was created with g_option_group_new() 548 @error: A return location for error details 549 @Returns: %TRUE if the function completed successfully, %FALSE if an error 550 occurred, in which case @error should be set with g_set_error() 551 552 553 <!-- ##### FUNCTION g_option_group_set_parse_hooks ##### --> 554 <para> 555 556 </para> 557 558 @group: 559 @pre_parse_func: 560 @post_parse_func: 561 562 563 <!-- ##### USER_FUNCTION GOptionErrorFunc ##### --> 564 <para> 565 The type of function to be used as callback when a parse error occurs. 566 </para> 567 568 @context: The active #GOptionContext 569 @group: The group to which the function belongs 570 @data: User data added to the #GOptionGroup containing the option when it 571 was created with g_option_group_new() 572 @error: The #GError containing details about the parse error 573 574 575 <!-- ##### FUNCTION g_option_group_set_error_hook ##### --> 576 <para> 577 578 </para> 579 580 @group: 581 @error_func: 582 583 584 <!-- ##### FUNCTION g_option_group_set_translate_func ##### --> 585 <para> 586 587 </para> 588 589 @group: 590 @func: 591 @data: 592 @destroy_notify: 593 594 595 <!-- ##### FUNCTION g_option_group_set_translation_domain ##### --> 596 <para> 597 598 </para> 599 600 @group: 601 @domain: 602 603 604