[gupnp] examples/light-server: add --quiet and --help options



commit 7d1c1d65f92de96468fa7d4b818d28fc373d4c3b
Author: Andrzej Bieniek <andyhelp gmail com>
Date:   Fri Jun 28 22:00:27 2013 +0100

    examples/light-server: add --quiet and --help options
    
    Quiet option is useful to disable printing overhead
    when measuring library performance.
    
    Example run:
    gupnp/examples$ ./light-server -q
    ^C
    gupnp/examples$ ./light-server --help
    Usage:
      lt-light-server [OPTION...]
    
    Help Options:
      -h, --help       Show help options
    
    Application Options:
      -q, --quiet      Turn off output
    
    https://bugzilla.gnome.org/show_bug.cgi?id=708575

 examples/light-server.c |   26 ++++++++++++++++++++++++--
 1 files changed, 24 insertions(+), 2 deletions(-)
---
diff --git a/examples/light-server.c b/examples/light-server.c
index 2dcddf3..f1b7162 100644
--- a/examples/light-server.c
+++ b/examples/light-server.c
@@ -14,6 +14,13 @@
 #include <gmodule.h>
 
 static gboolean status;
+static gboolean quiet;
+
+static GOptionEntry entries[] =
+{
+  { "quiet", 'q', 0, G_OPTION_ARG_NONE, &quiet, "Turn off output", NULL },
+  { NULL }
+};
 
 G_BEGIN_DECLS
 G_MODULE_EXPORT void set_target_cb (GUPnPService *service,
@@ -55,7 +62,10 @@ set_target_cb (GUPnPService          *service,
                           "Status", G_TYPE_BOOLEAN, status,
                           NULL);
 
-    g_print ("The light is now %s.\n", status ? "on" : "off");
+    if (!quiet)
+    {
+      g_print ("The light is now %s.\n", status ? "on" : "off");
+    }
   }
 
   /* Return success to the client */
@@ -116,6 +126,7 @@ query_status_cb (G_GNUC_UNUSED GUPnPService *service,
 int
 main (G_GNUC_UNUSED int argc, G_GNUC_UNUSED char **argv)
 {
+  GOptionContext *optionContext;
   GMainLoop *main_loop;
   GError *error = NULL;
   GUPnPContext *context;
@@ -126,9 +137,20 @@ main (G_GNUC_UNUSED int argc, G_GNUC_UNUSED char **argv)
   g_type_init ();
 #endif
 
+  optionContext = g_option_context_new (NULL);
+  g_option_context_add_main_entries (optionContext, entries, NULL);
+  if (!g_option_context_parse (optionContext, &argc, &argv, &error))
+  {
+    g_print ("option parsing failed: %s\n", error->message);
+    exit (1);
+  }
+
   /* By default the light is off */
   status = FALSE;
-  g_print ("The light is now %s.\n", status ? "on" : "off");
+  if (!quiet)
+  {
+    g_print ("The light is now %s.\n", status ? "on" : "off");
+  }
 
   /* Create the UPnP context */
   context = gupnp_context_new (NULL, NULL, 0, &error);


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