[glib/glib-2-28] Fix a typo



commit fe4d9ab7633b1c29602825e5350db3cc10759d6e
Author: Matthias Clasen <mclasen redhat com>
Date:   Fri Mar 4 01:36:51 2011 -0500

    Fix a typo

 gio/gapplicationcommandline.c             |    9 +++++
 gio/gioenums.h                            |    2 +-
 gio/tests/gapplication-example-cmdline3.c |   54 ++++++++++++++++++++++------
 3 files changed, 52 insertions(+), 13 deletions(-)
---
diff --git a/gio/gapplicationcommandline.c b/gio/gapplicationcommandline.c
index af9e4ce..7b0175a 100644
--- a/gio/gapplicationcommandline.c
+++ b/gio/gapplicationcommandline.c
@@ -46,6 +46,11 @@ G_DEFINE_TYPE (GApplicationCommandLine, g_application_command_line, G_TYPE_OBJEC
  * invocation) or remote (ie: some other process forwarded the
  * commandline to this process).
  *
+ * The GApplicationCommandLine object can provide the @argc and @argv
+ * parameters for use with the #GOptionContext command-line parsing API,
+ * with the g_application_command_line_get_arguments() function. See
+ * <xref linkend="gapplication-example-cmdline3"> for an example.
+ *
  * The exit status of the originally-invoked process may be set and
  * messages can be printed to stdout or stderr of that process.  The
  * lifecycle of the originally-invoked process is tied to the lifecycle
@@ -97,6 +102,10 @@ G_DEFINE_TYPE (GApplicationCommandLine, g_application_command_line, G_TYPE_OBJEC
  * (in this example, in an idle). Note that it is necessary to hold the
  * application until you are done with the commandline.
  * </para>
+ * <para>
+ * This example also shows how to use #GOptionContext for parsing the
+ * commandline arguments.
+ * </para>
  * <programlisting>
  * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude"; parse="text" href="../../../../gio/tests/gapplication-example-cmdline3.c">
  *   <xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback>
diff --git a/gio/gioenums.h b/gio/gioenums.h
index 6203aca..c9f95c2 100644
--- a/gio/gioenums.h
+++ b/gio/gioenums.h
@@ -1256,7 +1256,7 @@ typedef enum
  *     launching process to the primary instance. Set this flag if your
  *     application is expected to behave differently depending on certain
  *     environment variables. For instance, an editor might be expected
- *     to use the <envvar>GIT_COMMITTER_NAME</envvar> environment variable
+ *     to use the <envar>GIT_COMMITTER_NAME</envar> environment variable
  *     when editing a git commit message.
  *
  * Flags used to define the behaviour of a #GApplication.
diff --git a/gio/tests/gapplication-example-cmdline3.c b/gio/tests/gapplication-example-cmdline3.c
index 59704ed..3d0c8b1 100644
--- a/gio/tests/gapplication-example-cmdline3.c
+++ b/gio/tests/gapplication-example-cmdline3.c
@@ -6,22 +6,52 @@ static gboolean
 my_cmdline_handler (gpointer data)
 {
   GApplicationCommandLine *cmdline = data;
+  gchar **args;
   gchar **argv;
   gint argc;
+  gint arg1;
+  gboolean arg2;
+  GOptionContext *context;
+  GOptionEntry entries[] = {
+    { "arg1", 0, 0, G_OPTION_ARG_INT, &arg1, NULL, NULL },
+    { "arg2", 0, 0, G_OPTION_ARG_NONE, &arg2, NULL, NULL },
+    { NULL }
+  };
+  GError *error;
   gint i;
 
-  argv = g_application_command_line_get_arguments (cmdline, &argc);
-
-  g_application_command_line_print (cmdline,
-                                    "This text is written back\n"
-                                    "to stdout of the caller\n");
-
-  for (i = 0; i < argc; i++)
-    g_print ("argument %d: %s\n", i, argv[i]);
-
-  g_strfreev (argv);
-
-  g_application_command_line_set_exit_status (cmdline, 1);
+  args = g_application_command_line_get_arguments (cmdline, &argc);
+
+  /* We have to make an extra copy of the array, since g_option_context_parse()
+   * assumes that it can remove strings from the array without freeing them.
+   */
+  argv = g_new (gchar*, argc + 1);
+  for (i = 0; i <= argc; i++)
+    argv[i] = args[i];
+
+  context = g_option_context_new (NULL);
+  g_option_context_add_main_entries (context, entries, NULL);
+
+  arg1 = 0;
+  arg2 = FALSE;
+  error = NULL;
+  if (!g_option_context_parse (context, &argc, &argv, &error))
+    {
+      g_application_command_line_printerr (cmdline, "%s\n", error->message);
+      g_error_free (error);
+      g_application_command_line_set_exit_status (cmdline, 1);
+    }
+  else
+    {
+      g_application_command_line_print (cmdline, "arg1 is %d and arg2 is %s\n",
+                                        arg1, arg2 ? "TRUE" : "FALSE");
+      g_application_command_line_set_exit_status (cmdline, 0);
+    }
+
+  g_free (argv);
+  g_strfreev (args);
+
+  g_option_context_free (context);
 
   /* we are done handling this commandline */
   g_object_unref (cmdline);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]