OptionContext#
- class OptionContext(*args, **kwargs)#
A GOptionContext struct defines which options
are accepted by the commandline option parser. The struct has only private
fields and should not be directly accessed.
Methods#
- class OptionContext
- add_group(group: OptionGroup) None#
Adds a
OptionGroupto thecontext, so that parsing withcontextwill recognize the options in the group. Note that this will take ownership of thegroupand thus thegroupshould not be freed.Added in version 2.6.
- Parameters:
group – the group to add
- add_main_entries(entries: list[OptionEntry], translation_domain: str | None = None) None#
A convenience function which creates a main group if it doesn’t exist, adds the
entriesto it and sets the translation domain.Added in version 2.6.
- Parameters:
entries – a
None-terminated array ofOptionEntrytranslation_domain – a translation domain to use for translating the
--helpoutput for the options inentrieswith gettext(), orNone
- free() None#
Frees context and all the groups which have been added to it.
Please note that parsed arguments need to be freed separately (see
OptionEntry).Added in version 2.6.
- get_description() str#
Returns the description. See
set_description().Added in version 2.12.
- get_help(main_help: bool, group: OptionGroup | None = None) str#
Returns a formatted, translated help text for the given context. To obtain the text produced by
--help, callg_option_context_get_help (context, TRUE, NULL). To obtain the text produced by--help-all, callg_option_context_get_help (context, FALSE, NULL). To obtain the help text for an option group, callg_option_context_get_help (context, FALSE, group).Added in version 2.14.
- Parameters:
main_help – if
True, only include the main groupgroup – the
OptionGroupto create help for, orNone
- get_help_enabled() bool#
Returns whether automatic
--helpgeneration is turned on forcontext. Seeset_help_enabled().Added in version 2.6.
- get_ignore_unknown_options() bool#
Returns whether unknown options are ignored or not. See
set_ignore_unknown_options().Added in version 2.6.
- get_main_group() OptionGroup#
Returns a pointer to the main group of
context.Added in version 2.6.
- get_strict_posix() bool#
Returns whether strict POSIX code is enabled.
See
set_strict_posix()for more information.Added in version 2.44.
- get_summary() str#
Returns the summary. See
set_summary().Added in version 2.12.
- parse(argv: list[str] = Ellipsis) tuple[bool, list[str]]#
Parses the command line arguments, recognizing options which have been added to
context. A side-effect of calling this function is thatset_prgname()will be called.If the parsing is successful, any parsed arguments are removed from the array and
argcandargvare updated accordingly. A ‘–’ option is stripped fromargvunless there are unparsed options before and after it, or some of the options after it start with ‘-’. In case of an error,argcandargvare left unmodified.If automatic
--helpsupport is enabled (seeset_help_enabled()), and theargvarray contains one of the recognized help options, this function will produce help output to stdout and callexit (0).Note that function depends on the current locale for automatic character set conversion of string and filename arguments.
Added in version 2.6.
- Parameters:
argv – a pointer to the array of command line arguments
- parse_strv(arguments: list[str] = Ellipsis) tuple[bool, list[str]]#
Parses the command line arguments.
This function is similar to
parse()except that it respects the normal memory rules when dealing with a strv instead of assuming that the passed-in array is the argv of the main function.In particular, strings that are removed from the arguments list will be freed using
free().On Windows, the strings are expected to be in UTF-8. This is in contrast to
parse()which expects them to be in the system codepage, which is how they are passed asargvto main(). See g_win32_get_command_line() for a solution.This function is useful if you are trying to use
OptionContextwithGApplication.Added in version 2.40.
- Parameters:
arguments – a pointer to the command line arguments (which must be in UTF-8 on Windows). Starting with GLib 2.62,
argumentscan beNone, which matchesparse().
- set_description(description: str | None = None) None#
Adds a string to be displayed in
--helpoutput after the list of options. This text often includes a bug reporting address.Note that the summary is translated (see
set_translate_func()).Added in version 2.12.
- Parameters:
description – a string to be shown in
--helpoutput after the list of options, orNone
- set_help_enabled(help_enabled: bool) None#
Enables or disables automatic generation of
--helpoutput. By default,parse()recognizes--help,-h,-?,--help-alland--help-groupnameand creates suitable output to stdout.Added in version 2.6.
- Parameters:
help_enabled –
Trueto enable--help,Falseto disable it
- set_ignore_unknown_options(ignore_unknown: bool) None#
Sets whether to ignore unknown options or not. If an argument is ignored, it is left in the
argvarray after parsing. By default,parse()treats unknown options as error.This setting does not affect non-option arguments (i.e. arguments which don’t start with a dash). But note that GOption cannot reliably determine whether a non-option belongs to a preceding unknown option.
Added in version 2.6.
- Parameters:
ignore_unknown –
Trueto ignore unknown options,Falseto produce an error when unknown options are met
- set_main_group(group: OptionGroup) None#
Sets a
OptionGroupas main group of thecontext. This has the same effect as callingadd_group(), the only difference is that the options in the main group are treated differently when generating--helpoutput.Added in version 2.6.
- Parameters:
group – the group to set as main group
- set_strict_posix(strict_posix: bool) None#
Sets strict POSIX mode.
By default, this mode is disabled.
In strict POSIX mode, the first non-argument parameter encountered (eg: filename) terminates argument processing. Remaining arguments are treated as non-options and are not attempted to be parsed.
If strict POSIX mode is disabled then parsing is done in the GNU way where option arguments can be freely mixed with non-options.
As an example, consider “ls foo -l”. With GNU style parsing, this will list “foo” in long mode. In strict POSIX style, this will list the files named “foo” and “-l”.
It may be useful to force strict POSIX mode when creating “verb style” command line tools. For example, the “gsettings” command line tool supports the global option “–schemadir” as well as many subcommands (“get”, “set”, etc.) which each have their own set of arguments. Using strict POSIX mode will allow parsing the global options up to the verb name while leaving the remaining options to be parsed by the relevant subcommand (which can be determined by examining the verb name, which should be present in argv[1] after parsing).
Added in version 2.44.
- Parameters:
strict_posix – the new value
- set_summary(summary: str | None = None) None#
Adds a string to be displayed in
--helpoutput before the list of options. This is typically a summary of the program functionality.Note that the summary is translated (see
set_translate_func()andset_translation_domain()).Added in version 2.12.
- Parameters:
summary – a string to be shown in
--helpoutput before the list of options, orNone
- set_translate_func(func: Callable[[str, Any], str] | None = None, data: Any = None) None#
Sets the function which is used to translate the contexts user-visible strings, for
--helpoutput. IffuncisNone, strings are not translated.Note that option groups have their own translation functions, this function only affects the
parameter_string(seenew()), the summary (seeset_summary()) and the description (seeset_description()).If you are using gettext(), you only need to set the translation domain, see
set_translation_domain().Added in version 2.12.
- Parameters:
func – the
TranslateFunc, orNonedata – user data to pass to
func, orNone