Package: glib
CStruct g-option-context
Details
The GOption command line parser is intended to be a simpler replacement for
the popt library.
It supports short and long command line options, as shown in the following
example: testtreemodel -r 1 --max-size 20 --rand --display=:1.0 -vb -- file1 file2The example demonstrates a number of features of the GOption command line parser:
Usage: testtreemodel [OPTION...] - test tree model performanceGOption groups options in a g-option-group instance, which makes it easy to incorporate options from multiple sources. The intended use for this is to let applications collect option groups from the libraries it uses, add them to their g-option-context instance, and parse all options by a single call to the g-option-context-parse function. See the gtk-option-group function for an example. If an option is declared to be of type string or filename, GOption takes care of converting it to the right encoding. Strings are returned in UTF-8, filenames are returned in the GLib filename encoding. Note that this only works if the setlocale() function has been called before the g-option-context-parse function. Here is a complete example of setting up GOption to parse the example command line above and produce the example help output. (defvar repeats (cffi:foreign-alloc :int :initial-element 2)) (defvar max-size (cffi:foreign-alloc :int :initial-element 0)) (defvar verbose (cffi:foreign-alloc :boolean :initial-element nil)) (defvar beep (cffi:foreign-alloc :boolean :initial-element nil)) (defvar randomize (cffi:foreign-alloc :boolean :initial-element nil))On UNIX systems, the argv argument that is passed to the main function has no particular encoding, even to the extent that different parts of it may have different encodings. In general, normal arguments and flags will be in the current locale and filenames should be considered to be opaque byte strings. Proper use of the :filename versus :string option arguments is therefore important. Note that on Windows, filenames do have an encoding, but using a g-option-context instance with the argv argument as passed to the main function will result in a program that can only accept command line arguments with characters from the system codepage. This can cause problems when attempting to deal with filenames containing Unicode characters that fall outside of the codepage. A solution to this is to use the g_win32_get_command_line() and g-option-context-parse-strv functions which will properly handle full Unicode filenames. If you are using a g-application instance, this is done automatically for you. | See also |
2021-9-18