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 OptionGroup to the context, so that parsing with context will recognize the options in the group. Note that this will take ownership of the group and thus the group should not be freed.

Added in version 2.6.

Parameters:

group – the group to add

get_help_enabled() bool

Returns whether automatic --help generation is turned on for context. See set_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.

parse(argv: Sequence[str]) 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 that set_prgname() will be called.

If the parsing is successful, any parsed arguments are removed from the array and argc and argv are updated accordingly. A ‘–’ option is stripped from argv unless there are unparsed options before and after it, or some of the options after it start with ‘-’. In case of an error, argc and argv are left unmodified.

If automatic --help support is enabled (see set_help_enabled()), and the argv array contains one of the recognized help options, this function will produce help output to stdout and call exit (0).

Note that function depends on the [current locale][setlocale] 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

set_help_enabled(help_enabled: bool) None

Enables or disables automatic generation of --help output. By default, parse() recognizes --help, -h, -?, --help-all and --help-groupname and creates suitable output to stdout.

Added in version 2.6.

Parameters:

help_enabledTrue to enable --help, False to 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 argv array 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_unknownTrue to ignore unknown options, False to produce an error when unknown options are met

set_main_group(group: OptionGroup) None

Sets a OptionGroup as main group of the context. This has the same effect as calling add_group(), the only difference is that the options in the main group are treated differently when generating --help output.

Added in version 2.6.

Parameters:

group – the group to set as main group