Warn if there already is a main group. (#170445, Jeff Franks)
[platform/upstream/glib.git] / glib / goption.c
index 1332ddf..a8528de 100644 (file)
  * Boston, MA 02111-1307, USA.
  */
 
  * Boston, MA 02111-1307, USA.
  */
 
-#include "goption.h"
+#include "config.h"
 
 
+#include "goption.h"
 #include "glib.h"
 #include "gi18n.h"
 
 #include "glib.h"
 #include "gi18n.h"
 
+#include "galias.h"
+
 #include <string.h>
 #include <stdlib.h>
 #include <errno.h>
 #include <string.h>
 #include <stdlib.h>
 #include <errno.h>
@@ -101,7 +104,7 @@ static void free_pending_nulls (GOptionContext *context,
                                gboolean        perform_nulls);
 
 GQuark
                                gboolean        perform_nulls);
 
 GQuark
-g_option_context_error_quark (void)
+g_option_error_quark (void)
 {
   static GQuark q = 0;
   
 {
   static GQuark q = 0;
   
@@ -111,6 +114,19 @@ g_option_context_error_quark (void)
   return q;
 }
 
   return q;
 }
 
+/**
+ * g_option_context_new:
+ * @parameter_string: a string which is displayed in
+ *    the first line of <option>--help</option> output, after 
+ *    <literal><replaceable>programname</replaceable> [OPTION...]</literal>
+ *
+ * Creates a new option context. 
+ *
+ * Returns: a newly created #GOptionContext, which must be
+ *    freed with g_option_context_free() after use.
+ *
+ * Since: 2.6
+ */
 GOptionContext *
 g_option_context_new (const gchar *parameter_string)
 
 GOptionContext *
 g_option_context_new (const gchar *parameter_string)
 
@@ -121,19 +137,29 @@ g_option_context_new (const gchar *parameter_string)
 
   context->parameter_string = g_strdup (parameter_string);
   context->help_enabled = TRUE;
 
   context->parameter_string = g_strdup (parameter_string);
   context->help_enabled = TRUE;
+  context->ignore_unknown = FALSE;
 
   return context;
 }
 
 
   return context;
 }
 
-void
-g_option_context_free (GOptionContext *context)
+/**
+ * g_option_context_free:
+ * @context: a #GOptionContext 
+ *
+ * Frees context and all the groups which have been 
+ * added to it.
+ *
+ * Since: 2.6
+ */
+void g_option_context_free (GOptionContext *context) 
 {
   g_return_if_fail (context != NULL);
 
   g_list_foreach (context->groups, (GFunc)g_option_group_free, NULL);
   g_list_free (context->groups);
 
 {
   g_return_if_fail (context != NULL);
 
   g_list_foreach (context->groups, (GFunc)g_option_group_free, NULL);
   g_list_free (context->groups);
 
-  g_option_group_free (context->main_group);
+  if (context->main_group) 
+    g_option_group_free (context->main_group);
 
   free_changes_list (context, FALSE);
   free_pending_nulls (context, FALSE);
 
   free_changes_list (context, FALSE);
   free_pending_nulls (context, FALSE);
@@ -143,9 +169,22 @@ g_option_context_free (GOptionContext *context)
   g_free (context);
 }
 
   g_free (context);
 }
 
-void
-g_option_context_set_help_enabled (GOptionContext *context,
-                                  gboolean        help_enabled)
+
+/**
+ * g_option_context_set_help_enabled:
+ * @context: a #GOptionContext
+ * @help_enabled: %TRUE to enable <option>--help</option>, %FALSE to disable it
+ *
+ * Enables or disables automatic generation of <option>--help</option> 
+ * output. By default, g_option_context_parse() recognizes
+ * <option>--help</option>, <option>-?</option>, <option>--help-all</option>
+ * and <option>--help-</option><replaceable>groupname</replaceable> and creates
+ * suitable output to stdout. 
+ *
+ * Since: 2.6
+ */
+void g_option_context_set_help_enabled (GOptionContext *context,
+                                        gboolean        help_enabled)
 
 {
   g_return_if_fail (context != NULL);
 
 {
   g_return_if_fail (context != NULL);
@@ -153,14 +192,41 @@ g_option_context_set_help_enabled (GOptionContext *context,
   context->help_enabled = help_enabled;
 }
 
   context->help_enabled = help_enabled;
 }
 
-gboolean
-g_option_context_get_help_enabled (GOptionContext *context)
+/**
+ * g_option_context_get_help_enabled:
+ * @context: a #GOptionContext
+ * 
+ * Returns whether automatic <option>--help</option> generation
+ * is turned on for @context. See g_option_context_set_help_enabled().
+ * 
+ * Returns: %TRUE if automatic help generation is turned on.
+ *
+ * Since: 2.6
+ */
+gboolean 
+g_option_context_get_help_enabled (GOptionContext *context) 
 {
   g_return_val_if_fail (context != NULL, FALSE);
 {
   g_return_val_if_fail (context != NULL, FALSE);
-
+  
   return context->help_enabled;
 }
 
   return context->help_enabled;
 }
 
+/**
+ * g_option_context_set_ignore_unknown_options:
+ * @context: a #GOptionContext
+ * @ignore_unknown: %TRUE to ignore unknown options, %FALSE to produce
+ *    an error when unknown options are met
+ * 
+ * Sets whether to ignore unknown options or not. If an argument is 
+ * ignored, it is left in the @argv array after parsing. By default, 
+ * g_option_context_parse() treats unknown options as error.
+ * 
+ * This setting does not affect non-option arguments (i.e. arguments 
+ * which don't start with a dash). But note that GOption cannot reliably
+ * determine whether a non-option belongs to a preceding unknown option.
+ *
+ * Since: 2.6
+ **/
 void
 g_option_context_set_ignore_unknown_options (GOptionContext *context,
                                             gboolean        ignore_unknown)
 void
 g_option_context_set_ignore_unknown_options (GOptionContext *context,
                                             gboolean        ignore_unknown)
@@ -170,6 +236,17 @@ g_option_context_set_ignore_unknown_options (GOptionContext *context,
   context->ignore_unknown = ignore_unknown;
 }
 
   context->ignore_unknown = ignore_unknown;
 }
 
+/**
+ * g_option_context_get_ignore_unknown_options:
+ * @context: a #GOptionContext
+ * 
+ * Returns whether unknown options are ignored or not. See
+ * g_option_context_set_ignore_unknown_options().
+ * 
+ * Returns: %TRUE if unknown options are ignored.
+ * 
+ * Since: 2.6
+ **/
 gboolean
 g_option_context_get_ignore_unknown_options (GOptionContext *context)
 {
 gboolean
 g_option_context_get_ignore_unknown_options (GOptionContext *context)
 {
@@ -178,19 +255,56 @@ g_option_context_get_ignore_unknown_options (GOptionContext *context)
   return context->ignore_unknown;
 }
 
   return context->ignore_unknown;
 }
 
+/**
+ * g_option_context_add_group:
+ * @context: a #GOptionContext
+ * @group: the group to add
+ * 
+ * Adds a #GOptionGroup to the @context, so that parsing with @context
+ * will recognize the options in the group. Note that the group will
+ * be freed together with the context when g_option_context_free() is
+ * called, so you must not free the group yourself after adding it
+ * to a context.
+ *
+ * Since: 2.6
+ **/
 void
 g_option_context_add_group (GOptionContext *context,
                            GOptionGroup   *group)
 {
 void
 g_option_context_add_group (GOptionContext *context,
                            GOptionGroup   *group)
 {
+  GList *list;
+
   g_return_if_fail (context != NULL);
   g_return_if_fail (group != NULL);
   g_return_if_fail (group->name != NULL);
   g_return_if_fail (group->description != NULL);
   g_return_if_fail (group->help_description != NULL);
 
   g_return_if_fail (context != NULL);
   g_return_if_fail (group != NULL);
   g_return_if_fail (group->name != NULL);
   g_return_if_fail (group->description != NULL);
   g_return_if_fail (group->help_description != NULL);
 
-  context->groups = g_list_prepend (context->groups, group);
+  for (list = context->groups; list; list = list->next)
+    {
+      GOptionGroup *g = (GOptionGroup *)list->data;
+
+      if ((group->name == NULL && g->name == NULL) ||
+         (group->name && g->name && strcmp (group->name, g->name) == 0))
+       g_warning ("A group named \"%s\" is already part of this GOptionContext", 
+                  group->name);
+    }
+
+  context->groups = g_list_append (context->groups, group);
 }
 
 }
 
+/**
+ * g_option_context_set_main_group:
+ * @context: a #GOptionContext
+ * @group: the group to set as main group
+ * 
+ * Sets a #GOptionGroup as main group of the @context. 
+ * This has the same effect as calling g_option_context_add_group(), 
+ * the only difference is that the options in the main group are 
+ * treated differently when generating <option>--help</option> output.
+ *
+ * Since: 2.6
+ **/
 void
 g_option_context_set_main_group (GOptionContext *context,
                                 GOptionGroup   *group)
 void
 g_option_context_set_main_group (GOptionContext *context,
                                 GOptionGroup   *group)
@@ -198,9 +312,28 @@ g_option_context_set_main_group (GOptionContext *context,
   g_return_if_fail (context != NULL);
   g_return_if_fail (group != NULL);
 
   g_return_if_fail (context != NULL);
   g_return_if_fail (group != NULL);
 
+  if (context->main_group)
+    {
+      g_warning ("This GOptionContext already has a main group");
+
+      return;
+    }
+  
   context->main_group = group;
 }
 
   context->main_group = group;
 }
 
+/**
+ * g_option_context_get_main_group:
+ * @context: a #GOptionContext
+ * 
+ * Returns a pointer to the main group of @context.
+ * 
+ * Return value: the main group of @context, or %NULL if @context doesn't
+ *  have a main group. Note that group belongs to @context and should
+ *  not be modified or freed.
+ *
+ * Since: 2.6
+ **/
 GOptionGroup *
 g_option_context_get_main_group (GOptionContext *context)
 {
 GOptionGroup *
 g_option_context_get_main_group (GOptionContext *context)
 {
@@ -209,6 +342,19 @@ g_option_context_get_main_group (GOptionContext *context)
   return context->main_group;
 }
 
   return context->main_group;
 }
 
+/**
+ * g_option_context_add_main_entries:
+ * @context: a #GOptionContext
+ * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
+ * @translation_domain: a translation domain to use for translating
+ *    the <option>--help</option> output for the options in @entries
+ *    with gettext(), or %NULL
+ * 
+ * A convenience function which creates a main group if it doesn't 
+ * exist, adds the @entries to it and sets the translation domain.
+ * 
+ * Since: 2.6
+ **/
 void
 g_option_context_add_main_entries (GOptionContext      *context,
                                   const GOptionEntry  *entries,
 void
 g_option_context_add_main_entries (GOptionContext      *context,
                                   const GOptionEntry  *entries,
@@ -223,6 +369,35 @@ g_option_context_add_main_entries (GOptionContext      *context,
   g_option_group_set_translation_domain (context->main_group, translation_domain);
 }
 
   g_option_group_set_translation_domain (context->main_group, translation_domain);
 }
 
+static gint
+calculate_max_length (GOptionGroup *group)
+{
+  GOptionEntry *entry;
+  gint i, len, max_length;
+
+  max_length = 0;
+
+  for (i = 0; i < group->n_entries; i++)
+    {
+      entry = &group->entries[i];
+
+      if (entry->flags & G_OPTION_FLAG_HIDDEN)
+       continue;
+
+      len = g_utf8_strlen (entry->long_name, -1);
+      
+      if (entry->short_name)
+       len += 4;
+      
+      if (entry->arg != G_OPTION_ARG_NONE && entry->arg_description)
+       len += 1 + g_utf8_strlen (TRANSLATE (group, entry->arg_description), -1);
+      
+      max_length = MAX (max_length, len);
+    }
+
+  return max_length;
+}
+
 static void
 print_entry (GOptionGroup       *group,
             gint                max_length,
 static void
 print_entry (GOptionGroup       *group,
             gint                max_length,
@@ -232,7 +407,10 @@ print_entry (GOptionGroup       *group,
 
   if (entry->flags & G_OPTION_FLAG_HIDDEN)
     return;
 
   if (entry->flags & G_OPTION_FLAG_HIDDEN)
     return;
-         
+
+  if (entry->long_name[0] == 0)
+    return;
+
   str = g_string_new (NULL);
   
   if (entry->short_name)
   str = g_string_new (NULL);
   
   if (entry->short_name)
@@ -251,19 +429,63 @@ print_entry (GOptionGroup       *group,
 static void
 print_help (GOptionContext *context,
            gboolean        main_help,
 static void
 print_help (GOptionContext *context,
            gboolean        main_help,
-           GOptionGroup   *group)
+           GOptionGroup   *group) 
 {
   GList *list;
   gint max_length, len;
   gint i;
 {
   GList *list;
   gint max_length, len;
   gint i;
-
+  GOptionEntry *entry;
+  GHashTable *shadow_map;
+  gboolean seen[256];
+  
   g_print ("%s\n  %s %s %s\n\n", 
   g_print ("%s\n  %s %s %s\n\n", 
-          _("Usage:"), g_get_prgname (), _("[OPTION...]"),
+          _("Usage:"), g_get_prgname(), _("[OPTION...]"),
           context->parameter_string ? context->parameter_string : "");
 
           context->parameter_string ? context->parameter_string : "");
 
+  memset (seen, 0, sizeof (gboolean) * 256);
+  shadow_map = g_hash_table_new (g_str_hash, g_str_equal);
+
+  if (context->main_group)
+    {
+      for (i = 0; i < context->main_group->n_entries; i++)
+       {
+         entry = &context->main_group->entries[i];
+         g_hash_table_insert (shadow_map, 
+                              (gpointer)entry->long_name, 
+                              entry);
+         
+         if (seen[(guchar)entry->short_name])
+           entry->short_name = 0;
+         else
+           seen[(guchar)entry->short_name] = TRUE;
+       }
+    }
+
+  list = context->groups;
+  while (list != NULL)
+    {
+      GOptionGroup *group = list->data;
+      for (i = 0; i < group->n_entries; i++)
+       {
+         entry = &group->entries[i];
+         if (g_hash_table_lookup (shadow_map, entry->long_name))
+           entry->long_name = g_strdup_printf ("%s-%s", group->name, entry->long_name);
+         else  
+           g_hash_table_insert (shadow_map, (gpointer)entry->long_name, entry);
+
+         if (seen[(guchar)entry->short_name])
+           entry->short_name = 0;
+         else
+           seen[(guchar)entry->short_name] = TRUE;
+       }
+      list = list->next;
+    }
+
+  g_hash_table_destroy (shadow_map);
+
   list = context->groups;
 
   list = context->groups;
 
-  max_length = g_utf8_strlen ("--help, -?", -1);
+  max_length = g_utf8_strlen ("-?, --help", -1);
 
   if (list)
     {
 
   if (list)
     {
@@ -271,6 +493,12 @@ print_help (GOptionContext *context,
       max_length = MAX (max_length, len);
     }
 
       max_length = MAX (max_length, len);
     }
 
+  if (context->main_group)
+    {
+      len = calculate_max_length (context->main_group);
+      max_length = MAX (max_length, len);
+    }
+
   while (list != NULL)
     {
       GOptionGroup *group = list->data;
   while (list != NULL)
     {
       GOptionGroup *group = list->data;
@@ -280,45 +508,40 @@ print_help (GOptionContext *context,
       max_length = MAX (max_length, len);
 
       /* Then we go through the entries */
       max_length = MAX (max_length, len);
 
       /* Then we go through the entries */
-      for (i = 0; i < group->n_entries; i++)
-       {
-         len = g_utf8_strlen (group->entries[i].long_name, -1);
-
-         if (group->entries[i].short_name)
-           len += 4;
-
-         if (group->entries[i].arg != G_OPTION_ARG_NONE &&
-             group->entries[i].arg_description)
-           len += 1 + g_utf8_strlen (TRANSLATE (group, group->entries[i].arg_description), -1);
-
-         max_length = MAX (max_length, len);
-       }
+      len = calculate_max_length (group);
+      max_length = MAX (max_length, len);
       
       list = list->next;
     }
 
   /* Add a bit of padding */
   max_length += 4;
       
       list = list->next;
     }
 
   /* Add a bit of padding */
   max_length += 4;
-  
-  list = context->groups;
-
-  g_print ("%s\n  --%-*s %s\n", 
-          _("Help Options:"), max_length, "help", _("Show help options"));
-
-  /* We only want --help-all when there are groups */
-  if (list)
-    g_print ("  --%-*s %s\n", max_length, "help-all", _("Show all help options"));
 
 
-  while (list)
+  if (!group)
     {
     {
-      GOptionGroup *group = list->data;
-
-      g_print ("  --help-%-*s %s\n", max_length - 5, group->name, TRANSLATE (group, group->help_description));
+      list = context->groups;
       
       
-      list = list->next;
-    }
+      g_print ("%s\n  -%c, --%-*s %s\n", 
+              _("Help Options:"), '?', max_length - 4, "help", 
+              _("Show help options"));
+      
+      /* We only want --help-all when there are groups */
+      if (list)
+       g_print ("  --%-*s %s\n", max_length, "help-all", 
+                _("Show all help options"));
+      
+      while (list)
+       {
+         GOptionGroup *group = list->data;
+         
+         g_print ("  --help-%-*s %s\n", max_length - 5, group->name, 
+                  TRANSLATE (group, group->help_description));
+         
+         list = list->next;
+       }
 
 
-  g_print ("\n");
+      g_print ("\n");
+    }
 
   if (group)
     {
 
   if (group)
     {
@@ -357,8 +580,10 @@ print_help (GOptionContext *context,
 
       g_print ("%s\n", _("Application Options:"));
 
 
       g_print ("%s\n", _("Application Options:"));
 
-      for (i = 0; i < context->main_group->n_entries; i++) 
-       print_entry (context->main_group, max_length, &context->main_group->entries[i]);
+      if (context->main_group)
+       for (i = 0; i < context->main_group->n_entries; i++) 
+         print_entry (context->main_group, max_length, 
+                      &context->main_group->entries[i]);
 
       while (list != NULL)
        {
 
       while (list != NULL)
        {
@@ -374,7 +599,6 @@ print_help (GOptionContext *context,
 
       g_print ("\n");
     }
 
       g_print ("\n");
     }
-
   
   exit (0);
 }
   
   exit (0);
 }
@@ -394,7 +618,7 @@ parse_int (const gchar *arg_name,
     {
       g_set_error (error,
                   G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
     {
       g_set_error (error,
                   G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
-                  _("Cannot parse integer value '%s' for --%s"),
+                  _("Cannot parse integer value '%s' for %s"),
                   arg, arg_name);
       return FALSE;
     }
                   arg, arg_name);
       return FALSE;
     }
@@ -425,18 +649,17 @@ get_change (GOptionContext *context,
       change = list->data;
 
       if (change->arg_data == arg_data)
       change = list->data;
 
       if (change->arg_data == arg_data)
-       break;
+       goto found;
     }
 
     }
 
-  if (!change)
-    {
-      change = g_new0 (Change, 1);
-      change->arg_type = arg_type;
-      change->arg_data = arg_data;
-
-      context->changes = g_list_prepend (context->changes, change);
-    }
+  change = g_new0 (Change, 1);
+  change->arg_type = arg_type;
+  change->arg_data = arg_data;
+  
+  context->changes = g_list_prepend (context->changes, change);
   
   
+ found:
+
   return change;
 }
 
   return change;
 }
 
@@ -472,7 +695,7 @@ parse_arg (GOptionContext *context,
        change = get_change (context, G_OPTION_ARG_NONE,
                             entry->arg_data);
 
        change = get_change (context, G_OPTION_ARG_NONE,
                             entry->arg_data);
 
-       *(gboolean *)entry->arg_data = TRUE;
+       *(gboolean *)entry->arg_data = !(entry->flags & G_OPTION_FLAG_REVERSE);
        break;
       }             
     case G_OPTION_ARG_STRING:
        break;
       }             
     case G_OPTION_ARG_STRING:
@@ -530,8 +753,14 @@ parse_arg (GOptionContext *context,
       {
        gchar *data;
 
       {
        gchar *data;
 
+#ifdef G_OS_WIN32
+       data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
+       
+       if (!data)
+         return FALSE;
+#else
        data = g_strdup (value);
        data = g_strdup (value);
-
+#endif
        change = get_change (context, G_OPTION_ARG_FILENAME,
                             entry->arg_data);
        g_free (change->allocated.str);
        change = get_change (context, G_OPTION_ARG_FILENAME,
                             entry->arg_data);
        g_free (change->allocated.str);
@@ -539,6 +768,7 @@ parse_arg (GOptionContext *context,
        change->prev.str = *(gchar **)entry->arg_data;
        change->allocated.str = data;
 
        change->prev.str = *(gchar **)entry->arg_data;
        change->allocated.str = data;
 
+       *(gchar **)entry->arg_data = data;
        break;
       }
 
        break;
       }
 
@@ -546,8 +776,14 @@ parse_arg (GOptionContext *context,
       {
        gchar *data;
        
       {
        gchar *data;
        
+#ifdef G_OS_WIN32
+       data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
+       
+       if (!data)
+         return FALSE;
+#else
        data = g_strdup (value);
        data = g_strdup (value);
-
+#endif
        change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
                             entry->arg_data);
 
        change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
                             entry->arg_data);
 
@@ -584,8 +820,8 @@ parse_arg (GOptionContext *context,
                             entry->arg_data);
        change->prev.integer = *(gint *)entry->arg_data;
        *(gint *)entry->arg_data = data;
                             entry->arg_data);
        change->prev.integer = *(gint *)entry->arg_data;
        *(gint *)entry->arg_data = data;
+       break;
       }
       }
-      break;
     case G_OPTION_ARG_CALLBACK:
       {
        gchar *tmp;
     case G_OPTION_ARG_CALLBACK:
       {
        gchar *tmp;
@@ -697,6 +933,7 @@ parse_long_option (GOptionContext *context,
                     NULL, NULL, error);
          
          add_pending_null (context, &((*argv)[*index]), NULL);
                     NULL, NULL, error);
          
          add_pending_null (context, &((*argv)[*index]), NULL);
+         *parsed = TRUE;
        }
       else
        {
        }
       else
        {
@@ -708,7 +945,7 @@ parse_long_option (GOptionContext *context,
              gchar *value = NULL;
              gchar *option_name;
 
              gchar *value = NULL;
              gchar *option_name;
 
-             add_pending_null (context, &((*argv)[*index + 1]), NULL);
+             add_pending_null (context, &((*argv)[*index]), NULL);
              
              if (arg[len] == '=')
                value = arg + len + 1;
              
              if (arg[len] == '=')
                value = arg + len + 1;
@@ -738,6 +975,40 @@ parse_long_option (GOptionContext *context,
   return TRUE;
 }
 
   return TRUE;
 }
 
+static gboolean
+parse_remaining_arg (GOptionContext *context,
+                    GOptionGroup   *group,
+                    gint           *index,
+                    gint           *argc,
+                    gchar        ***argv,
+                    GError        **error,
+                    gboolean       *parsed)
+{
+  gint j;
+
+  for (j = 0; j < group->n_entries; j++)
+    {
+      if (*index >= *argc)
+       return TRUE;
+
+      if (group->entries[j].long_name[0])
+       continue;
+
+      g_return_val_if_fail (group->entries[j].arg == G_OPTION_ARG_STRING_ARRAY ||
+                           group->entries[j].arg == G_OPTION_ARG_FILENAME_ARRAY, FALSE);
+      
+      add_pending_null (context, &((*argv)[*index]), NULL);
+      
+      if (!parse_arg (context, group, &group->entries[j], (*argv)[*index], "", error))
+       return FALSE;
+      
+      *parsed = TRUE;
+      return TRUE;
+    }
+
+  return TRUE;
+}
+
 static void
 free_changes_list (GOptionContext *context,
                   gboolean        revert)
 static void
 free_changes_list (GOptionContext *context,
                   gboolean        revert)
@@ -750,10 +1021,6 @@ free_changes_list (GOptionContext *context,
 
       if (revert)
        {
 
       if (revert)
        {
-         /* Free any allocated data */
-         g_free (change->allocated.str);
-         g_strfreev (change->allocated.array.data);
-         
          switch (change->arg_type)
            {
            case G_OPTION_ARG_NONE:
          switch (change->arg_type)
            {
            case G_OPTION_ARG_NONE:
@@ -764,11 +1031,14 @@ free_changes_list (GOptionContext *context,
              break;
            case G_OPTION_ARG_STRING:
            case G_OPTION_ARG_FILENAME:
              break;
            case G_OPTION_ARG_STRING:
            case G_OPTION_ARG_FILENAME:
+              g_free (change->allocated.str);
              *(gchar **)change->arg_data = change->prev.str;
              break;
            case G_OPTION_ARG_STRING_ARRAY:
            case G_OPTION_ARG_FILENAME_ARRAY:
              *(gchar **)change->arg_data = change->prev.str;
              break;
            case G_OPTION_ARG_STRING_ARRAY:
            case G_OPTION_ARG_FILENAME_ARRAY:
+             g_strfreev (change->allocated.array.data);
              *(gchar ***)change->arg_data = change->prev.array;
              *(gchar ***)change->arg_data = change->prev.array;
+             break;
            default:
              g_assert_not_reached ();
            }
            default:
              g_assert_not_reached ();
            }
@@ -811,6 +1081,36 @@ free_pending_nulls (GOptionContext *context,
   context->pending_nulls = NULL;
 }
 
   context->pending_nulls = NULL;
 }
 
+/**
+ * g_option_context_parse:
+ * @context: a #GOptionContext
+ * @argc: a pointer to the number of command line arguments.
+ * @argv: a pointer to the array of command line arguments.
+ * @error: a return location for errors 
+ * 
+ * Parses the command line arguments, recognizing options
+ * which have been added to @context. A side-effect of 
+ * calling this function is that g_set_prgname() will be
+ * called.
+ *
+ * If the parsing is successful, any parsed arguments are
+ * removed from the array and @argc and @argv are updated 
+ * accordingly. A '--' option is stripped from @argv
+ * unless there are unparsed options before and after it, 
+ * or some of the options after it start with '-'. In case 
+ * of an error, @argc and @argv are left unmodified. 
+ *
+ * If automatic <option>--help</option> support is enabled
+ * (see g_option_context_set_help_enabled()), and the 
+ * @argv array contains one of the recognized help options,
+ * this function will produce help output to stdout and
+ * call <literal>exit (0)</literal>.
+ * 
+ * Return value: %TRUE if the parsing was successful, 
+ *               %FALSE if an error occurred
+ *
+ * Since: 2.6
+ **/
 gboolean
 g_option_context_parse (GOptionContext   *context,
                        gint             *argc,
 gboolean
 g_option_context_parse (GOptionContext   *context,
                        gint             *argc,
@@ -820,6 +1120,20 @@ g_option_context_parse (GOptionContext   *context,
   gint i, j, k;
   GList *list;
 
   gint i, j, k;
   GList *list;
 
+  /* Set program name */
+  if (argc && argv && *argc)
+    {
+      gchar *prgname;
+      
+      prgname = g_path_get_basename ((*argv)[0]);
+      g_set_prgname (prgname);
+      g_free (prgname);
+    }
+  else
+    {
+      g_set_prgname ("<unknown>");
+    }
+  
   /* Call pre-parse hooks */
   list = context->groups;
   while (list)
   /* Call pre-parse hooks */
   list = context->groups;
   while (list)
@@ -836,7 +1150,7 @@ g_option_context_parse (GOptionContext   *context,
       list = list->next;
     }
 
       list = list->next;
     }
 
-  if (context->main_group->pre_parse_func)
+  if (context->main_group && context->main_group->pre_parse_func)
     {
       if (!(* context->main_group->pre_parse_func) (context, context->main_group,
                                                    context->main_group->user_data, error))
     {
       if (!(* context->main_group->pre_parse_func) (context, context->main_group,
                                                    context->main_group->user_data, error))
@@ -845,12 +1159,16 @@ g_option_context_parse (GOptionContext   *context,
 
   if (argc && argv)
     {
 
   if (argc && argv)
     {
+      gboolean stop_parsing = FALSE;
+      gboolean has_unknown = FALSE;
+      gint separator_pos = 0;
+
       for (i = 1; i < *argc; i++)
        {
       for (i = 1; i < *argc; i++)
        {
-         gchar *arg;
+         gchar *arg, *dash;
          gboolean parsed = FALSE;
 
          gboolean parsed = FALSE;
 
-         if ((*argv)[i][0] == '-')
+         if ((*argv)[i][0] == '-' && !stop_parsing)
            {
              if ((*argv)[i][1] == '-')
                {
            {
              if ((*argv)[i][1] == '-')
                {
@@ -861,8 +1179,9 @@ g_option_context_parse (GOptionContext   *context,
                  /* '--' terminates list of arguments */
                  if (*arg == 0)
                    {
                  /* '--' terminates list of arguments */
                  if (*arg == 0)
                    {
-                     add_pending_null (context, &((*argv)[i]), NULL);
-                     break;
+                     separator_pos = i;
+                     stop_parsing = TRUE;
+                     continue;
                    }
 
                  /* Handle help options */
                    }
 
                  /* Handle help options */
@@ -890,7 +1209,8 @@ g_option_context_parse (GOptionContext   *context,
                        }
                    }
 
                        }
                    }
 
-                 if (!parse_long_option (context, context->main_group, &i, arg,
+                 if (context->main_group &&
+                     !parse_long_option (context, context->main_group, &i, arg,
                                          argc, argv, error, &parsed))
                    goto fail;
 
                                          argc, argv, error, &parsed))
                    goto fail;
 
@@ -912,12 +1232,41 @@ g_option_context_parse (GOptionContext   *context,
                      
                      list = list->next;
                    }
                      
                      list = list->next;
                    }
+                 
+                 if (parsed)
+                   continue;
 
 
+                 /* Now look for --<group>-<option> */
+                 dash = strchr (arg, '-');
+                 if (dash)
+                   {
+                     /* Try the groups */
+                     list = context->groups;
+                     while (list)
+                       {
+                         GOptionGroup *group = list->data;
+                         
+                         if (strncmp (group->name, arg, dash - arg) == 0)
+                           {
+                             if (!parse_long_option (context, group, &i, dash + 1,
+                                                     argc, argv, error, &parsed))
+                               goto fail;
+                             
+                             if (parsed)
+                               break;
+                           }
+                         
+                         list = list->next;
+                       }
+                   }
+                 
                  if (context->ignore_unknown)
                    continue;
                }
              else
                {
                  if (context->ignore_unknown)
                    continue;
                }
              else
                {
+                 /* short option */
+
                  gint new_i, j;
                  gboolean *nulled_out = NULL;
                  
                  gint new_i, j;
                  gboolean *nulled_out = NULL;
                  
@@ -930,9 +1279,13 @@ g_option_context_parse (GOptionContext   *context,
                  
                  for (j = 0; j < strlen (arg); j++)
                    {
                  
                  for (j = 0; j < strlen (arg); j++)
                    {
+                     if (context->help_enabled && arg[j] == '?')
+                       print_help (context, TRUE, NULL);
+                     
                      parsed = FALSE;
                      
                      parsed = FALSE;
                      
-                     if (!parse_short_option (context, context->main_group,
+                     if (context->main_group &&
+                         !parse_short_option (context, context->main_group,
                                               i, &new_i, arg[j],
                                               argc, argv, error, &parsed))
                        {
                                               i, &new_i, arg[j],
                                               argc, argv, error, &parsed))
                        {
@@ -982,7 +1335,7 @@ g_option_context_parse (GOptionContext   *context,
                          if (!nulled_out[j])
                            {
                              if (!new_arg)
                          if (!nulled_out[j])
                            {
                              if (!new_arg)
-                               new_arg = g_malloc (strlen (arg));
+                               new_arg = g_malloc (strlen (arg) + 1);
                              new_arg[arg_index++] = arg[j];
                            }
                        }
                              new_arg[arg_index++] = arg[j];
                            }
                        }
@@ -998,39 +1351,60 @@ g_option_context_parse (GOptionContext   *context,
                    }
                }
              
                    }
                }
              
+             if (!parsed)
+               has_unknown = TRUE;
+
              if (!parsed && !context->ignore_unknown)
                {
                  g_set_error (error,
                               G_OPTION_ERROR, G_OPTION_ERROR_UNKNOWN_OPTION,
              if (!parsed && !context->ignore_unknown)
                {
                  g_set_error (error,
                               G_OPTION_ERROR, G_OPTION_ERROR_UNKNOWN_OPTION,
-                              _("Unknown option %s"), (*argv)[i]);
+                                  _("Unknown option %s"), (*argv)[i]);
                  goto fail;
                }
            }
                  goto fail;
                }
            }
-       }
-
-      /* Call post-parse hooks */
-      list = context->groups;
-      while (list)
-       {
-         GOptionGroup *group = list->data;
-
-         if (group->post_parse_func)
+         else
            {
            {
-             if (!(* group->post_parse_func) (context, group,
-                                              group->user_data, error))
+             /* Collect remaining args */
+             if (context->main_group &&
+                 !parse_remaining_arg (context, context->main_group, &i,
+                                       argc, argv, error, &parsed))
                goto fail;
                goto fail;
+             
+             if (!parsed && (has_unknown || (*argv)[i][0] == '-'))
+               separator_pos = 0;
            }
            }
-         
-         list = list->next;
        }
 
        }
 
-      if (context->main_group->post_parse_func)
+      if (separator_pos > 0)
+       add_pending_null (context, &((*argv)[separator_pos]), NULL);
+       
+    }
+
+  /* Call post-parse hooks */
+  list = context->groups;
+  while (list)
+    {
+      GOptionGroup *group = list->data;
+      
+      if (group->post_parse_func)
        {
        {
-         if (!(* context->main_group->post_parse_func) (context, context->main_group,
-                                                        context->main_group->user_data, error))
+         if (!(* group->post_parse_func) (context, group,
+                                          group->user_data, error))
            goto fail;
        }
            goto fail;
        }
-
+      
+      list = list->next;
+    }
+  
+  if (context->main_group && context->main_group->post_parse_func)
+    {
+      if (!(* context->main_group->post_parse_func) (context, context->main_group,
+                                                    context->main_group->user_data, error))
+       goto fail;
+    }
+  
+  if (argc && argv)
+    {
       free_pending_nulls (context, TRUE);
       
       for (i = 1; i < *argc; i++)
       free_pending_nulls (context, TRUE);
       
       for (i = 1; i < *argc; i++)
@@ -1043,11 +1417,13 @@ g_option_context_parse (GOptionContext   *context,
            {
              k -= i;
              for (j = i + k; j < *argc; j++)
            {
              k -= i;
              for (j = i + k; j < *argc; j++)
-               (*argv)[j-k] = (*argv)[j];
+               {
+                 (*argv)[j-k] = (*argv)[j];
+                 (*argv)[j] = NULL;
+               }
              *argc -= k;
            }
              *argc -= k;
            }
-       }
-      
+       }      
     }
 
   return TRUE;
     }
 
   return TRUE;
@@ -1061,13 +1437,13 @@ g_option_context_parse (GOptionContext   *context,
       GOptionGroup *group = list->data;
       
       if (group->error_func)
       GOptionGroup *group = list->data;
       
       if (group->error_func)
-       (* group->post_parse_func) (context, group,
-                                   group->user_data, error);
+       (* group->error_func) (context, group,
+                              group->user_data, error);
       
       list = list->next;
     }
 
       
       list = list->next;
     }
 
-  if (context->main_group->error_func)
+  if (context->main_group && context->main_group->error_func)
     (* context->main_group->error_func) (context, context->main_group,
                                         context->main_group->user_data, error);
   
     (* context->main_group->error_func) (context, context->main_group,
                                         context->main_group->user_data, error);
   
@@ -1077,7 +1453,27 @@ g_option_context_parse (GOptionContext   *context,
   return FALSE;
 }
                                   
   return FALSE;
 }
                                   
-     
+/**
+ * g_option_group_new:
+ * @name: the name for the option group, this is used to provide
+ *   help for the options in this group with <option>--help-</option>@name
+ * @description: a description for this group to be shown in 
+ *   <option>--help</option>. This string is translated using the translation
+ *   domain or translation function of the group
+ * @help_description: a description for the <option>--help-</option>@name option.
+ *   This string is translated using the translation domain or translation function
+ *   of the group
+ * @user_data: user data that will be passed to the pre- and post-parse hooks,
+ *   the error hook and to callbacks of %G_OPTION_ARG_CALLBACK options, or %NULL
+ * @destroy: a function that will be called to free @user_data, or %NULL
+ * 
+ * Creates a new #GOptionGroup.
+ * 
+ * Return value: a newly created option group. It should be added 
+ *   to a #GOptionContext or freed with g_option_group_free().
+ *
+ * Since: 2.6
+ **/
 GOptionGroup *
 g_option_group_new (const gchar    *name,
                    const gchar    *description,
 GOptionGroup *
 g_option_group_new (const gchar    *name,
                    const gchar    *description,
@@ -1098,6 +1494,16 @@ g_option_group_new (const gchar    *name,
   return group;
 }
 
   return group;
 }
 
+
+/**
+ * g_option_group_free:
+ * @group: a #GOptionGroup
+ * 
+ * Frees a #GOptionGroup. Note that you must <emphasis>not</emphasis>
+ * free groups which have been added to a #GOptionContext.
+ *
+ * Since: 2.6
+ **/
 void
 g_option_group_free (GOptionGroup *group)
 {
 void
 g_option_group_free (GOptionGroup *group)
 {
@@ -1119,6 +1525,15 @@ g_option_group_free (GOptionGroup *group)
 }
 
 
 }
 
 
+/**
+ * g_option_group_add_entries:
+ * @group: a #GOptionGroup
+ * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
+ * 
+ * Adds the options specified in @entries to @group.
+ *
+ * Since: 2.6
+ **/
 void
 g_option_group_add_entries (GOptionGroup       *group,
                            const GOptionEntry *entries)
 void
 g_option_group_add_entries (GOptionGroup       *group,
                            const GOptionEntry *entries)
@@ -1136,6 +1551,22 @@ g_option_group_add_entries (GOptionGroup       *group,
   group->n_entries += n_entries;
 }
 
   group->n_entries += n_entries;
 }
 
+/**
+ * g_option_group_set_parse_hooks:
+ * @group: a #GOptionGroup
+ * @pre_parse_func: a function to call before parsing, or %NULL
+ * @post_parse_func: a function to call after parsing, or %NULL
+ * 
+ * Associates two functions with @group which will be called 
+ * from g_option_context_parse() before the first option is parsed
+ * and after the last option has been parsed, respectively.
+ *
+ * Note that the user data to be passed to @pre_parse_func and
+ * @post_parse_func can be specified when constructing the group
+ * with g_option_group_new().
+ *
+ * Since: 2.6
+ **/
 void
 g_option_group_set_parse_hooks (GOptionGroup     *group,
                                GOptionParseFunc  pre_parse_func,
 void
 g_option_group_set_parse_hooks (GOptionGroup     *group,
                                GOptionParseFunc  pre_parse_func,
@@ -1147,6 +1578,20 @@ g_option_group_set_parse_hooks (GOptionGroup     *group,
   group->post_parse_func = post_parse_func;  
 }
 
   group->post_parse_func = post_parse_func;  
 }
 
+/**
+ * g_option_group_set_error_hook:
+ * @group: a #GOptionGroup
+ * @error_func: a function to call when an error occurs
+ * 
+ * Associates a function with @group which will be called 
+ * from g_option_context_parse() when an error occurs.
+ *
+ * Note that the user data to be passed to @pre_parse_func and
+ * @post_parse_func can be specified when constructing the group
+ * with g_option_group_new().
+ *
+ * Since: 2.6
+ **/
 void
 g_option_group_set_error_hook (GOptionGroup     *group,
                               GOptionErrorFunc  error_func)
 void
 g_option_group_set_error_hook (GOptionGroup     *group,
                               GOptionErrorFunc  error_func)
@@ -1157,11 +1602,28 @@ g_option_group_set_error_hook (GOptionGroup     *group,
 }
 
 
 }
 
 
+/**
+ * g_option_group_set_translate_func:
+ * @group: a #GOptionGroup
+ * @func: the #GTranslateFunc, or %NULL 
+ * @data: user data to pass to @func, or %NULL
+ * @destroy_notify: a function which gets called to free @data, or %NULL
+ * 
+ * Sets the function which is used to translate user-visible
+ * strings, for <option>--help</option> output. Different
+ * groups can use different #GTranslateFunc<!-- -->s. If @func
+ * is %NULL, strings are not translated.
+ *
+ * If you are using gettext(), you only need to set the translation
+ * domain, see g_option_group_set_translation_domain().
+ *
+ * Since: 2.6
+ **/
 void
 g_option_group_set_translate_func (GOptionGroup   *group,
                                   GTranslateFunc  func,
                                   gpointer        data,
 void
 g_option_group_set_translate_func (GOptionGroup   *group,
                                   GTranslateFunc  func,
                                   gpointer        data,
-                                  GDestroyNotify  notify)
+                                  GDestroyNotify  destroy_notify)
 {
   g_return_if_fail (group != NULL);
   
 {
   g_return_if_fail (group != NULL);
   
@@ -1170,7 +1632,7 @@ g_option_group_set_translate_func (GOptionGroup   *group,
       
   group->translate_func = func;
   group->translate_data = data;
       
   group->translate_func = func;
   group->translate_data = data;
-  group->translate_notify = notify;
+  group->translate_notify = destroy_notify;
 }
 
 static gchar *
 }
 
 static gchar *
@@ -1180,6 +1642,16 @@ dgettext_swapped (const gchar *msgid,
   return dgettext (domainname, msgid);
 }
 
   return dgettext (domainname, msgid);
 }
 
+/**
+ * g_option_group_set_translation_domain:
+ * @group: a #GOptionGroup
+ * @domain: the domain to use
+ * 
+ * A convenience function to use gettext() for translating
+ * user-visible strings. 
+ * 
+ * Since: 2.6
+ **/
 void
 g_option_group_set_translation_domain (GOptionGroup *group,
                                       const gchar  *domain)
 void
 g_option_group_set_translation_domain (GOptionGroup *group,
                                       const gchar  *domain)
@@ -1192,3 +1664,5 @@ g_option_group_set_translation_domain (GOptionGroup *group,
                                     g_free);
 } 
 
                                     g_free);
 } 
 
+#define __G_OPTION_C__
+#include "galiasdef.c"