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
- 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.
- 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
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][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
--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