mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-01-24 21:16:15 +01:00
GOptionContext: add memory-friendly parse mode
Add g_option_context_parse_strv() that obeys the normal memory conventions for dealing with a strv instead of assuming that we're dealing with the 'argv' parameter to main(). This will help for using GOptionContext with GApplication. https://bugzilla.gnome.org/show_bug.cgi?id=721947
This commit is contained in:
parent
d3017967d8
commit
f062fae4d6
@ -1175,6 +1175,7 @@ g_option_context_set_translate_func
|
||||
g_option_context_set_translation_domain
|
||||
g_option_context_free
|
||||
g_option_context_parse
|
||||
g_option_context_parse_strv
|
||||
g_option_context_set_help_enabled
|
||||
g_option_context_get_help_enabled
|
||||
g_option_context_set_ignore_unknown_options
|
||||
|
@ -204,6 +204,7 @@ struct _GOptionContext
|
||||
|
||||
guint help_enabled : 1;
|
||||
guint ignore_unknown : 1;
|
||||
guint strv_mode : 1;
|
||||
|
||||
GOptionGroup *main_group;
|
||||
|
||||
@ -1645,6 +1646,9 @@ free_pending_nulls (GOptionContext *context,
|
||||
|
||||
if (perform_nulls)
|
||||
{
|
||||
if (context->strv_mode)
|
||||
g_free (*n->ptr);
|
||||
|
||||
if (n->value)
|
||||
{
|
||||
/* Copy back the short options */
|
||||
@ -2464,3 +2468,42 @@ g_option_context_get_description (GOptionContext *context)
|
||||
|
||||
return context->description;
|
||||
}
|
||||
|
||||
/**
|
||||
* g_option_context_parse_strv:
|
||||
* @context: a #GOptionContext
|
||||
* @arguments: (inout) (array null-terminated=1): a pointer to the command line arguments
|
||||
* @error: a return location for errors
|
||||
*
|
||||
* 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().
|
||||
*
|
||||
* This function is useful if you are trying to use #GOptionContext with
|
||||
* #GApplication.
|
||||
*
|
||||
* Returns: %TRUE if the parsing was successful,
|
||||
* %FALSE if an error occurred
|
||||
*
|
||||
* Since: 2.40
|
||||
**/
|
||||
gboolean
|
||||
g_option_context_parse_strv (GOptionContext *context,
|
||||
gchar ***arguments,
|
||||
GError **error)
|
||||
{
|
||||
gboolean success;
|
||||
gint argc;
|
||||
|
||||
context->strv_mode = TRUE;
|
||||
argc = g_strv_length (*arguments);
|
||||
success = g_option_context_parse (context, &argc, arguments, error);
|
||||
context->strv_mode = FALSE;
|
||||
|
||||
return success;
|
||||
}
|
||||
|
@ -342,6 +342,10 @@ gboolean g_option_context_parse (GOptionContext *context,
|
||||
gint *argc,
|
||||
gchar ***argv,
|
||||
GError **error);
|
||||
GLIB_AVAILABLE_IN_2_40
|
||||
gboolean g_option_context_parse_strv (GOptionContext *context,
|
||||
gchar ***arguments,
|
||||
GError **error);
|
||||
GLIB_AVAILABLE_IN_ALL
|
||||
void g_option_context_set_translate_func (GOptionContext *context,
|
||||
GTranslateFunc func,
|
||||
|
Loading…
Reference in New Issue
Block a user