diff options
author | Matthias Clasen <mclasen@redhat.com> | 2005-06-18 04:55:26 +0000 |
---|---|---|
committer | Matthias Clasen <matthiasc@src.gnome.org> | 2005-06-18 04:55:26 +0000 |
commit | 3a7a09668253973235e7e48fcaf9b06051c8c3b2 (patch) | |
tree | 43399650a47e07f7d108cf986efc1c8025acc7bf /glib/goption.c | |
parent | 2e5ed68b653c67b25e859cc77a4282d8b9d724c0 (diff) | |
download | glib-3a7a09668253973235e7e48fcaf9b06051c8c3b2.tar.gz |
Add G_OPTION_FLAG_NO_ARG and G_OPTION_FLAG_FILENAME to allow greater
2005-06-18 Matthias Clasen <mclasen@redhat.com>
* glib/goption.h:
* glib/goption.c: Add G_OPTION_FLAG_NO_ARG and
G_OPTION_FLAG_FILENAME to allow greater control of
G_OPTION_ARG_CALLBACK options. (#302632, Dan Winship)
* tests/option-test.c: test callback args
Diffstat (limited to 'glib/goption.c')
-rw-r--r-- | glib/goption.c | 31 |
1 files changed, 23 insertions, 8 deletions
diff --git a/glib/goption.c b/glib/goption.c index 4f2d2b6ca..78a66c768 100644 --- a/glib/goption.c +++ b/glib/goption.c @@ -33,6 +33,10 @@ #define TRANSLATE(group, str) (((group)->translate_func ? (* (group)->translate_func) ((str), (group)->translate_data) : (str))) +#define NO_ARG(entry) ((entry)->arg == G_OPTION_ARG_NONE || \ + ((entry)->arg == G_OPTION_ARG_CALLBACK && \ + ((entry)->flags & G_OPTION_FLAG_NO_ARG))) + typedef struct { GOptionArg arg_type; @@ -393,7 +397,7 @@ calculate_max_length (GOptionGroup *group) if (entry->short_name) len += 4; - if (entry->arg != G_OPTION_ARG_NONE && entry->arg_description) + if (!NO_ARG (entry) && entry->arg_description) len += 1 + g_utf8_strlen (TRANSLATE (group, entry->arg_description), -1); max_length = MAX (max_length, len); @@ -847,17 +851,28 @@ parse_arg (GOptionContext *context, } case G_OPTION_ARG_CALLBACK: { - gchar *tmp; + gchar *data; gboolean retval; - tmp = g_locale_to_utf8 (value, -1, NULL, NULL, error); + if (entry->flags & G_OPTION_FLAG_NO_ARG) + data = NULL; + else if (entry->flags & G_OPTION_FLAG_FILENAME) + { +#ifdef G_OS_WIN32 + data = g_locale_to_utf8 (value, -1, NULL, NULL, error); +#else + data = g_strdup (value); +#endif + } + else + data = g_locale_to_utf8 (value, -1, NULL, NULL, error); - if (!value) + if (!(entry->flags & G_OPTION_FLAG_NO_ARG) && !data) return FALSE; - retval = (* (GOptionArgFunc) entry->arg_data) (option_name, tmp, group->user_data, error); + retval = (* (GOptionArgFunc) entry->arg_data) (option_name, data, group->user_data, error); - g_free (tmp); + g_free (data); return retval; @@ -887,7 +902,7 @@ parse_short_option (GOptionContext *context, { if (arg == group->entries[j].short_name) { - if (group->entries[j].arg == G_OPTION_ARG_NONE) + if (NO_ARG (&group->entries[j])) { parse_arg (context, group, &group->entries[j], NULL, NULL, error); @@ -954,7 +969,7 @@ parse_long_option (GOptionContext *context, if (*index >= *argc) return TRUE; - if (group->entries[j].arg == G_OPTION_ARG_NONE && + if (NO_ARG (&group->entries[j]) && strcmp (arg, group->entries[j].long_name) == 0) { parse_arg (context, group, &group->entries[j], |