diff options
author | Ryan Lortie <desrt@desrt.ca> | 2014-01-10 12:16:24 -0500 |
---|---|---|
committer | Ryan Lortie <desrt@desrt.ca> | 2014-01-10 12:32:35 -0500 |
commit | f062fae4d6d705736c2b1b899c4413b99d4cfc96 (patch) | |
tree | 6ea0accbf0bde4cbf50b6b16aa1e172b759e4ed9 /glib/goption.c | |
parent | d3017967d8123e800fd593e22fda1c0d7f40071f (diff) | |
download | glib-f062fae4d6d705736c2b1b899c4413b99d4cfc96.tar.gz |
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
Diffstat (limited to 'glib/goption.c')
-rw-r--r-- | glib/goption.c | 43 |
1 files changed, 43 insertions, 0 deletions
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; +} |