Index: glib/goption.h
===================================================================
--- glib/goption.h	(revision 7309)
+++ glib/goption.h	(working copy)
@@ -122,6 +122,11 @@
 						   gint                *argc,
 						   gchar             ***argv,
 						   GError             **error);
+gboolean	g_option_context_parse_utf8       (GOptionContext      *context,
+						   gint                *argc,
+						   gchar             ***argv,
+						   GError             **error);
+
 void            g_option_context_set_translate_func (GOptionContext     *context,
 						     GTranslateFunc      func,
 						     gpointer            data,
Index: glib/goption.c
===================================================================
--- glib/goption.c	(revision 7309)
+++ glib/goption.c	(working copy)
@@ -982,6 +982,7 @@
 	   GOptionEntry   *entry,
 	   const gchar    *value,
 	   const gchar    *option_name,
+	   gboolean        utf8_flag,
 	   GError        **error)
      
 {
@@ -1003,7 +1004,10 @@
       {
 	gchar *data;
 	
-	data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
+	if (utf8_flag)
+	  data = g_strdup (value);
+	else
+	  data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
 
 	if (!data)
 	  return FALSE;
@@ -1022,7 +1026,10 @@
       {
 	gchar *data;
 
-	data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
+	if (utf8_flag)
+	  data = g_strdup (value);
+	else
+	  data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
 
 	if (!data)
 	  return FALSE;
@@ -1055,12 +1062,18 @@
 	gchar *data;
 
 #ifdef G_OS_WIN32
-	data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
+	if (utf8_flag)
+	  data = g_strdup (value);
+	else
+	  data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
 	
 	if (!data)
 	  return FALSE;
 #else
-	data = g_strdup (value);
+	if (utf8_flag)
+	  data = g_filename_from_utf8 (value, -1, NULL, NULL, error);
+	else
+	  data = g_strdup (value);
 #endif
 	change = get_change (context, G_OPTION_ARG_FILENAME,
 			     entry->arg_data);
@@ -1078,12 +1091,18 @@
 	gchar *data;
 	
 #ifdef G_OS_WIN32
-	data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
+	if (utf8_flag)
+	  data = g_strdup (value);
+	else
+	  data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
 	
 	if (!data)
 	  return FALSE;
 #else
-	data = g_strdup (value);
+	if (utf8_flag)
+	  data = g_filename_from_utf8 (value, -1, NULL, NULL, error);
+	else
+	  data = g_strdup (value);
 #endif
 	change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
 			     entry->arg_data);
@@ -1135,13 +1154,24 @@
 	else if (entry->flags & G_OPTION_FLAG_FILENAME)
 	  {
 #ifdef G_OS_WIN32
-  	    data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
+	    if (utf8_flag)
+	      data = g_strdup (value);
+	    else
+	      data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
 #else
-	    data = g_strdup (value);
+	    if (utf8_flag)
+	      data = g_filename_from_utf8 (value, -1, NULL, NULL, error);
+	    else
+	      data = g_strdup (value);
 #endif
 	  }
 	else
-	  data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
+	  {
+	    if (utf8_flag)
+	      data = g_strdup (value);
+	    else
+	      data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
+	  }
 
 	if (!(entry->flags & (G_OPTION_FLAG_NO_ARG|G_OPTION_FLAG_OPTIONAL_ARG)) && 
 	    !data)
@@ -1205,7 +1235,8 @@
 		    gint           *argc,
 		    gchar        ***argv,
 		    GError        **error,
-		    gboolean       *parsed)
+		    gboolean       *parsed,
+		    gboolean        utf8_flag)
 {
   gint j;
     
@@ -1264,7 +1295,7 @@
 	    }
 
 	  if (!parse_arg (context, group, &group->entries[j], 
-			  value, option_name, error))
+			  value, option_name, utf8_flag, error))
 	    {
 	      g_free (option_name);
 	      return FALSE;
@@ -1287,7 +1318,8 @@
 		   gint           *argc,
 		   gchar        ***argv,
 		   GError        **error,
-		   gboolean       *parsed)
+		   gboolean       *parsed,
+		   gboolean        utf8_flag)
 {
   gint j;
 
@@ -1306,7 +1338,7 @@
 
 	  option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
 	  parse_arg (context, group, &group->entries[j],
-		     NULL, option_name, error);
+		     NULL, option_name, utf8_flag, error);
 	  g_free(option_name);
 	  
 	  add_pending_null (context, &((*argv)[*index]), NULL);
@@ -1341,7 +1373,7 @@
 		        {
 		          gboolean retval;
 		          retval = parse_arg (context, group, &group->entries[j],
-					      NULL, option_name, error);
+					      NULL, option_name, utf8_flag, error);
 	  	          *parsed = TRUE;
 		          g_free (option_name);
 	   	          return retval;
@@ -1359,7 +1391,7 @@
 		{
 		    gboolean retval;
 		    retval = parse_arg (context, group, &group->entries[j],
-					NULL, option_name, error);
+					NULL, option_name, utf8_flag, error);
 	  	    *parsed = TRUE;
 		    g_free (option_name);
 	   	    return retval;
@@ -1374,7 +1406,7 @@
 		}
 
 	      if (!parse_arg (context, group, &group->entries[j], 
-			      value, option_name, error))
+			      value, option_name, utf8_flag, error))
 		{
 		  g_free (option_name);
 		  return FALSE;
@@ -1396,7 +1428,8 @@
 		     gint           *argc,
 		     gchar        ***argv,
 		     GError        **error,
-		     gboolean       *parsed)
+		     gboolean       *parsed,
+		     gboolean        utf8_flag)
 {
   gint j;
 
@@ -1414,7 +1447,7 @@
       
       add_pending_null (context, &((*argv)[*index]), NULL);
       
-      if (!parse_arg (context, group, &group->entries[j], (*argv)[*index], "", error))
+      if (!parse_arg (context, group, &group->entries[j], (*argv)[*index], "", utf8_flag, error))
 	return FALSE;
       
       *parsed = TRUE;
@@ -1502,46 +1535,12 @@
   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>.
- *
- * Note that function depends on the 
- * <link linkend="setlocale">current locale</link> for 
- * automatic character set conversion of string and filename
- * arguments.
- * 
- * 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,
-			gchar          ***argv,
-			GError          **error)
+static gboolean
+option_context_parse_internal (GOptionContext   *context,
+			       gint             *argc,
+			       gchar          ***argv,
+			       GError          **error,
+			       gboolean          utf8_flag)
 {
   gint i, j, k;
   GList *list;
@@ -1638,7 +1637,8 @@
 
 		  if (context->main_group &&
 		      !parse_long_option (context, context->main_group, &i, arg,
-					  FALSE, argc, argv, error, &parsed))
+					  FALSE, argc, argv, error, &parsed,
+					  utf8_flag))
 		    goto fail;
 
 		  if (parsed)
@@ -1651,7 +1651,8 @@
 		      GOptionGroup *group = list->data;
 		      
 		      if (!parse_long_option (context, group, &i, arg, 
-					      FALSE, argc, argv, error, &parsed))
+					      FALSE, argc, argv, error, &parsed,
+					      utf8_flag))
 			goto fail;
 		      
 		      if (parsed)
@@ -1676,7 +1677,8 @@
 			  if (strncmp (group->name, arg, dash - arg) == 0)
 			    {
 			      if (!parse_long_option (context, group, &i, dash + 1,
-						      TRUE, argc, argv, error, &parsed))
+						      TRUE, argc, argv, error, &parsed,
+						      utf8_flag))
 				goto fail;
 			      
 			      if (parsed)
@@ -1706,7 +1708,8 @@
 		      if (context->main_group &&
 			  !parse_short_option (context, context->main_group,
 					       i, &new_i, arg[j],
-					       argc, argv, error, &parsed))
+					       argc, argv, error, &parsed,
+					       utf8_flag))
                         goto fail;
 		      if (!parsed)
 			{
@@ -1716,7 +1719,8 @@
 			    {
 			      GOptionGroup *group = list->data;
 			      if (!parse_short_option (context, group, i, &new_i, arg[j],
-						       argc, argv, error, &parsed))
+						       argc, argv, error, &parsed,
+						       utf8_flag))
 				goto fail;
 			      if (parsed)
 				break;
@@ -1772,7 +1776,8 @@
 	      /* Collect remaining args */
 	      if (context->main_group &&
 		  !parse_remaining_arg (context, context->main_group, &i,
-					argc, argv, error, &parsed))
+					argc, argv, error, &parsed,
+					utf8_flag))
 		goto fail;
 	      
 	      if (!parsed && (has_unknown || (*argv)[i][0] == '-'))
@@ -1857,7 +1862,93 @@
 
   return FALSE;
 }
+
+/**
+ * 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.
+ *
+ * The command line arguments in general should be as passed from the
+ * C library startup code to main(). On Windows, this means that they
+ * should be in the C library's locale encoding.
+ *
+ * File name arguments that are parsed are stored in the GLib file
+ * name encoding. On POSIX systems means that the are stored as they
+ * were in the command line vector. On Windows file names are stored
+ * in UTF-8 (but still also they should be in the locale encoding in
+ * the input command line).
+ *
+ * If you have a command line vector that you have constructed or
+ * retrieved in some way so that you know it is in UTF-8, use
+ * g_option_context_parse_utf8().
+ *
+ * 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>.
+ *
+ * Note that function depends on the 
+ * <link linkend="setlocale">current locale</link> for 
+ * automatic character set conversion of string and filename
+ * arguments.
+ * 
+ * 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,
+			gchar          ***argv,
+			GError          **error)
+{
+  return option_context_parse_internal (context, argc, argv, error, FALSE);
+}
+
+/**
+ * g_option_context_parse_utf8:
+ * @context: a #GOptionContext
+ * @argc: a pointer to the number of command line arguments
+ * @argv: a pointer to the array of command line arguments in UTF-8
+ * @error: a return location for errors 
+ * 
+ * Like g_option_context_parse(), except that the input command line
+ * arguments are all in UTF-8. Note that on POSIX systems file name
+ * arguments that are parsed will still be stored in the GLib file
+ * name encoding, which need not be UTF-8. (On Windows the GLib file
+ * name encoding is UTF-8.)
+ *
+ * Return value: %TRUE if the parsing was successful, 
+ *               %FALSE if an error occurred
+ *
+ * Since: 2.18
+ **/
+gboolean
+g_option_context_parse_utf8 (GOptionContext   *context,
+				   gint             *argc,
+				   gchar          ***argv,
+				   GError          **error)
 				   
+{
+  return option_context_parse_internal (context, argc, argv, error, TRUE);
+}
+
 /**
  * g_option_group_new:
  * @name: the name for the option group, this is used to provide
