diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt index 84b2f4b55..86894f261 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 0a22f6feb..9a20040d1 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 ee0137787..78df2eca6 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,