Class OptionContext
- All Implemented Interfaces:
Proxy
GOptionContext struct defines which options
are accepted by the commandline option parser. The struct has only private
fields and should not be directly accessed.-
Constructor Summary
ConstructorsConstructorDescriptionOptionContext(MemorySegment address) Create a OptionContext proxy instance for the provided memory address. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddGroup(OptionGroup group) Adds aGOptionGroupto thecontext,so that parsing with this OptionContext will recognize the options in the group.voidaddMainEntries(@Nullable OptionEntry @Nullable [] entries, @Nullable String translationDomain) A convenience function which creates a main group if it doesn't exist, adds theentriesto it and sets the translation domain.voidfree()Frees context and all the groups which have been added to it.Returns the description.getHelp(boolean mainHelp, @Nullable OptionGroup group) Returns a formatted, translated help text for the given context.booleanReturns whether automatic--helpgeneration is turned on forcontext.See g_option_context_set_help_enabled().booleanReturns whether unknown options are ignored or not.Returns a pointer to the main group ofcontext.booleanReturns whether strict POSIX code is enabled.Returns the summary.static OptionContextCreates a new option context.booleanParses the command line arguments, recognizing options which have been added tocontext.A side-effect of calling this function is that g_set_prgname() will be called.booleanParses the command line arguments.voidsetDescription(@Nullable String description) Adds a string to be displayed in--helpoutput after the list of options.voidsetHelpEnabled(boolean helpEnabled) Enables or disables automatic generation of--helpoutput.voidsetIgnoreUnknownOptions(boolean ignoreUnknown) Sets whether to ignore unknown options or not.voidsetMainGroup(OptionGroup group) Sets aGOptionGroupas main group of thecontext.This has the same effect as calling g_option_context_add_group(), the only difference is that the options in the main group are treated differently when generating--helpoutput.voidsetStrictPosix(boolean strictPosix) Sets strict POSIX mode.voidsetSummary(@Nullable String summary) Adds a string to be displayed in--helpoutput before the list of options.voidsetTranslateFunc(@Nullable TranslateFunc func) Sets the function which is used to translate the contexts user-visible strings, for--helpoutput.voidsetTranslationDomain(String domain) A convenience function to use gettext() for translating user-visible strings.Methods inherited from class org.javagi.base.ProxyInstance
equals, handle, hashCode
-
Constructor Details
-
OptionContext
Create a OptionContext proxy instance for the provided memory address.- Parameters:
address- the memory address of the native object
-
-
Method Details
-
new_
Creates a new option context.The
parameterStringcan serve multiple purposes. It can be used to add descriptions for "rest" arguments, which are not parsed by theGOptionContext, typically something like "FILES" or "FILE1 FILE2...". If you are usingG_OPTION_REMAININGfor collecting "rest" arguments, GLib handles this automatically by using theargDescriptionof the correspondingGOptionEntryin the usage summary.Another usage is to give a short summary of the program functionality, like " - frob the strings", which will be displayed in the same line as the usage. For a longer description of the program functionality that should be displayed as a paragraph below the usage line, use g_option_context_set_summary().
Note that the
parameterStringis translated using the function set with g_option_context_set_translate_func(), so it should normally be passed untranslated.- Parameters:
parameterString- a string which is displayed in the first line of--helpoutput, after the usage summaryprogramname [OPTION...]- Returns:
- a newly created
GOptionContext, which must be freed with g_option_context_free() after use. - Since:
- 2.6
-
addGroup
Adds aGOptionGroupto thecontext,so that parsing with this OptionContext will recognize the options in the group. Note that this will take ownership of thegroupand thus thegroupshould not be freed.- Parameters:
group- the group to add- Since:
- 2.6
-
addMainEntries
public void addMainEntries(@Nullable OptionEntry @Nullable [] entries, @Nullable String translationDomain) A convenience function which creates a main group if it doesn't exist, adds theentriesto it and sets the translation domain.- Parameters:
entries- anull-terminated array ofGOptionEntrystranslationDomain- a translation domain to use for translating the--helpoutput for the options inentrieswith gettext(), ornull- Since:
- 2.6
-
free
public void free()Frees context and all the groups which have been added to it.Please note that parsed arguments need to be freed separately (see
GOptionEntry).- Since:
- 2.6
-
getDescription
Returns the description. See g_option_context_set_description().- Returns:
- the description
- Since:
- 2.12
-
getHelp
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).- Parameters:
mainHelp- iftrue, only include the main groupgroup- theGOptionGroupto create help for, ornull- Returns:
- A newly allocated string containing the help text
- Since:
- 2.14
-
getHelpEnabled
public boolean getHelpEnabled()Returns whether automatic--helpgeneration is turned on forcontext.See g_option_context_set_help_enabled().- Returns:
trueif automatic help generation is turned on.- Since:
- 2.6
-
getIgnoreUnknownOptions
public boolean getIgnoreUnknownOptions()Returns whether unknown options are ignored or not. See g_option_context_set_ignore_unknown_options().- Returns:
trueif unknown options are ignored.- Since:
- 2.6
-
getMainGroup
Returns a pointer to the main group ofcontext.- Returns:
- the main group of
context,ornullif this OptionContext doesn't have a main group. Note that group belongs to this OptionContext and should not be modified or freed. - Since:
- 2.6
-
getStrictPosix
public boolean getStrictPosix()Returns whether strict POSIX code is enabled.See g_option_context_set_strict_posix() for more information.
- Returns:
trueif strict POSIX is enabled,falseotherwise.- Since:
- 2.44
-
getSummary
Returns the summary. See g_option_context_set_summary().- Returns:
- the summary
- Since:
- 2.12
-
parse
Parses the command line arguments, recognizing options which have been added tocontext.A side-effect of calling this function is that g_set_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 (see g_option_context_set_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.
- Parameters:
argv- a pointer to the array of command line arguments- Returns:
trueif the parsing was successful,falseif an error occurred- Throws:
GErrorException- seeGError- Since:
- 2.6
-
parseStrv
Parses the command line arguments.This function is similar to g_option_context_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 g_free().
On Windows, the strings are expected to be in UTF-8. This is in contrast to g_option_context_parse() which expects them to be in the system codepage, which is how they are passed as
argvto main(). See g_win32_get_command_line() for a solution.This function is useful if you are trying to use
GOptionContextwithGApplication.- Parameters:
arguments- a pointer to the command line arguments (which must be in UTF-8 on Windows). Starting with GLib 2.62,argumentscan benull, which matches g_option_context_parse().- Returns:
trueif the parsing was successful,falseif an error occurred- Throws:
GErrorException- seeGError- Since:
- 2.40
-
setDescription
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 g_option_context_set_translate_func()).
- Parameters:
description- a string to be shown in--helpoutput after the list of options, ornull- Since:
- 2.12
-
setHelpEnabled
public void setHelpEnabled(boolean helpEnabled) Enables or disables automatic generation of--helpoutput. By default, g_option_context_parse() recognizes--help,-h,-?,--help-alland--help-groupnameand creates suitable output to stdout.- Parameters:
helpEnabled-trueto enable--help,falseto disable it- Since:
- 2.6
-
setIgnoreUnknownOptions
public void setIgnoreUnknownOptions(boolean ignoreUnknown) Sets whether to ignore unknown options or not. If an argument is ignored, it is left in theargvarray after parsing. By default, g_option_context_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.
- Parameters:
ignoreUnknown-trueto ignore unknown options,falseto produce an error when unknown options are met- Since:
- 2.6
-
setMainGroup
Sets aGOptionGroupas main group of thecontext.This has the same effect as calling g_option_context_add_group(), the only difference is that the options in the main group are treated differently when generating--helpoutput.- Parameters:
group- the group to set as main group- Since:
- 2.6
-
setStrictPosix
public void setStrictPosix(boolean strictPosix) 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).
- Parameters:
strictPosix- the new value- Since:
- 2.44
-
setSummary
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 g_option_context_set_translate_func() and g_option_context_set_translation_domain()).
- Parameters:
summary- a string to be shown in--helpoutput before the list of options, ornull- Since:
- 2.12
-
setTranslateFunc
Sets the function which is used to translate the contexts user-visible strings, for--helpoutput. Iffuncisnull, strings are not translated.Note that option groups have their own translation functions, this function only affects the
parameterString(see g_option_context_new()), the summary (see g_option_context_set_summary()) and the description (see g_option_context_set_description()).If you are using gettext(), you only need to set the translation domain, see g_option_context_set_translation_domain().
- Parameters:
func- theGTranslateFunc, ornull- Since:
- 2.12
-
setTranslationDomain
A convenience function to use gettext() for translating user-visible strings.- Parameters:
domain- the domain to use- Since:
- 2.12
-