From f062fae4d6d705736c2b1b899c4413b99d4cfc96 Mon Sep 17 00:00:00 2001 From: Ryan Lortie Date: Fri, 10 Jan 2014 12:16:24 -0500 Subject: [PATCH] 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 --- docs/reference/glib/glib-sections.txt | 1 + glib/goption.c | 43 +++++++++++++++++++++++++++++++++++ glib/goption.h | 4 ++++ 3 files changed, 48 insertions(+) diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt index 84b2f4b..86894f2 100644 --- a/docs/reference/glib/glib-sections.txt +++ b/docs/reference/glib/glib-sections.txt @@ -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 diff --git a/glib/goption.c b/glib/goption.c index 0a22f6f..9a20040 100644 --- a/glib/goption.c +++ b/glib/goption.c @@ -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; +} diff --git a/glib/goption.h b/glib/goption.h index ee01377..78df2ec 100644 --- a/glib/goption.h +++ b/glib/goption.h @@ -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, -- 2.7.4