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 thecontext
, so that parsing withcontext
will recognize the options in the group. Note that this will take ownership of thegroup
and thus thegroup
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 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.
- 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 thatset_prgname()
will be called.If the parsing is successful, any parsed arguments are removed from the array and
argc
andargv
are updated accordingly. A ‘–’ option is stripped fromargv
unless there are unparsed options before and after it, or some of the options after it start with ‘-’. In case of an error,argc
andargv
are left unmodified.If automatic
--help
support is enabled (seeset_help_enabled()
), and theargv
array 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][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_enabled –
True
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_unknown –
True
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 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--help
output.Added in version 2.6.
- Parameters:
group – the group to set as main group