Re: is there a g_option_print_usage() command?



On Mon, 2007-03-12 at 12:08 -0700, Rick Jones wrote:

I see that goption can uatomagically handle --help and the like.  I'd 
like to emit a usage string when someone gives a bogus option - is there 
  a call one can make to just print the same stuff that goption does on 
its own with --help, or do I have to mess about with error hooks?

http://developer.gnome.org/doc/API/2.0/glib/glib-Commandline-option-parser.html
didn't mention one - before I use something I find from source I'd want 
to check that it was simply an oversight in the api docs :)

thanks,

rick jones
_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


Rick,

The link you supplied gave a partial example that works.  However, here
is a full example; notice the if statement around
g_option_context_parse().

BEGIN EXAMPLE
--------------------
/* gapc_notify.c        20070123
 
  libnotify messaging routine for Linux scripting languages.
  output a titled message box to the graphical screen for 10 seconds

  Copyright (C) 2007 James Scott, Jr. <skoona users sourceforge net>
  
  Compile Requirements: { tested on FC6 }
    libnotify-0.4.2
    libnotify-devel-0.4.2
    gtk-2.6+
    glib-2.6+
    
  Compile Command:
   $ gcc `pkg-config --cflags --libs libnotify` -o gapc_notify
gapc_notify.c
   
  Usage:
   $ ./gapc_notify -s 5 -t "title of messagebox" -m "message in the box"
   or 
   $ ./gapc_notify --help

  GPL2
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.   
*/

#include <libnotify/notify.h>
#include <stdio.h>
#include <unistd.h>

static gchar   *gd_pch_title;
static gchar   *gd_pch_body;
static gchar   *gd_pch_icon;
static gint     gd_i_seconds;

int main(int argc, char * argv[] ) 
{
    NotifyNotification *nctx;  
    GError  *gerror = NULL;
    GOptionContext *context = NULL;
    GOptionEntry entries[] = {
      {"title", 't', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING,
&gd_pch_title,
         "Title text on msg box", "Uninterruptible Power Supply"},
      {"message", 'm', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING,
&gd_pch_body,
         "Message body text", "message"},
      {"icon-name", 'i', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING,
&gd_pch_icon,
         "icon to include next to message",
"gtk-dialog-info ...-warning ...-error"},
      {"show-seconds", 's', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_INT,
&gd_i_seconds,
         "seconds to display message", "10"},         
      {NULL}
    };
    
    /* Get command line parms */    
    context = g_option_context_new (
                " - APCUPSD Commandline Notification Utility"
                " GPL2 [2007] <Skoona users sourceforge net>");
    g_option_context_add_main_entries(context, entries, NULL);
    g_option_context_set_ignore_unknown_options(context, FALSE);
    if (!(g_option_context_parse(context, &argc, &argv, &gerror))) {
         g_warning ("Parse command line failed: %s", gerror->message);
         g_option_context_free(context);
         g_error_free(gerror);
         return (1);
    }

    if (gd_pch_title == NULL) {
        gd_pch_title = "Uninterruptible Power Supply";
    }
    if (gd_pch_body == NULL) {
        gd_pch_body = "message";
    }
    if (gd_pch_icon == NULL) {
        gd_pch_icon = "gtk-dialog-info";
    }
    if (gd_i_seconds < 1 ) {
        gd_i_seconds = 10;
    }
  
    notify_init("gapc_notify");

    nctx = notify_notification_new (gd_pch_title, gd_pch_body,
gd_pch_icon, NULL); 
    
    notify_notification_set_timeout (nctx, (gd_i_seconds * 1000) ); //
10 seconds

    if (!notify_notification_show (nctx, NULL)) 
    {
    fprintf(stderr, "failed to send notification\n");
    return 2;
    }

    g_object_unref(G_OBJECT(nctx));
    
    notify_uninit();

    return 0;
}

--------------------
END EXAMPLE




James Scott, Jr.                                                 
Registered Linux User #270764
FC6 on Dual AMD-MP 2400+
Author: {gfhcm, gkrellfah2,gapcmon,giw}.sourceforge.net
http://mysite.verizon.net/skoona/index.html


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