GNOME Goal #1: Move from popt to GOption

Since GNOME 2.10, GLib provides GOption, a commandline option parser. The help output of your program will be much nicer :-) And it will enable us to slowly get rid of popt (even if libgnome will still have to depend on it for compatibility reasons).

The documentation is fairly complete, so go read it!

{i} To know if a module uses popt and is concerned by this migration, you can grep for POPT in the whole module. If there's no result, you can be 95% sure that it's not used by the module.

Try to put a descriptive string as argument of g_option_context_new(). A good string might be the one you have as comment in the .desktop file for the module. Are we sure that this is the right usage of this argument? See bug #336085 -- ClaudioSaavedra.

Note that you don't have to call g_option_context_free() since GnomeProgram will do it when you unref it.

GOption will also automatically call g_set_progname() for you.

The most noticable difference is the change in the structure:

struct poptOption {
  const char * longName; /* may be NULL */
  char shortName;         /* may be '\0' */
  int argInfo;
  void * arg;             /* depends on argInfo */
  int val;                /* 0 means don't return, just update flag */
  char * descrip;         /* description for autohelp -- may be NULL */
  char * argDescrip;      /* argument description for autohelp */
};

typedef struct {
  const gchar *long_name;
  gchar        short_name;
  gint         flags;

  GOptionArg   arg;
  gpointer     arg_data;
  
  const gchar *description;
  const gchar *arg_description;
} GOptionEntry;

Also, don't forget to look at G_OPTION_REMAINING if you used to parse argv yourself (looking for filename arguments, eg).

libgnome 2.14 supports GOption and makes it easy to use it. Here's a short example:

static const GOptionEntry options[] = {
        { "size", 0, 0, G_OPTION_ARG_STRING, &size, N_("Specify the size"), NULL},
        { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &files, NULL,  N_("[FILE...]") },
        { NULL}
};

int
main (int argc, char **argv)
{
        GnomeProgram *program;
        GOptionContext *context;

        /* do what you want to do before */

        /* gettext needs to be initialised here, at the latest */
        bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
        bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
        textdomain (GETTEXT_PACKAGE);

        context = g_option_context_new (_("- Show how to use GOption"));

        g_option_context_add_main_entries (context, options, GETTEXT_PACKAGE);

        program = gnome_program_init
                            (argv [0], "0.0.0.0", LIBGNOMEUI_MODULE,
                            argc, argv,
                            GNOME_PARAM_GOPTION_CONTEXT, context,
                            GNOME_PARAM_NONE);

        /* you can now use the size and files variables */

       g_object_unref (program);
       exit (0);
}

Note that you can ignore unknown options with g_option_context_set_ignore_unknown_options (context, TRUE);

Also because GOption support for libgnome was added in 2.14 (in GLib much earlier), you need to update configure.(ac|in) to require libgnome 2.14 like this:

LIBGNOME_REQUIRED=2.14.0
PKG_CHECK_MODULES([MYLIBS],[libgnome-2.0 >= $LIBGNOME_REQUIRED])

Status of this goal

1 module remaining

State

Markup

todo

<: #ff8080> todo

patch

<: #ffc849> [[wiki:GnomeBug:xxxxx|patch]]

done

<: #80ff80> [[wiki:GnomeBug:xxxxx|done]]

not needed

<: #80ff80> not needed

Above are the states and corresponding markup to update the modules state table below.

Tarball

Status

Desktop

alacarte

not needed

bug-buddy

done

cheese

done

control-center

done

dasher

done

deskbar-applet

not needed

eel

done

ekiga

done

eog

done

epiphany

done

evince

done

evolution-data-server

done

evolution

done

evolution-exchange

not needed

evolution-webcal

done

fast-user-switch-applet

not needed

file-roller

done

gcalctool

not needed

gconf-editor

done

gdm

done

gedit

done

gnome-applets

done

gnome-backgrounds

not needed

gnome-desktop

not needed

gnome-doc-utils

not needed

gnome-games

done

gnome-icon-theme

not needed

gnome-keyring

not needed

gnome-netstatus

not needed

gnome-nettool

done

gnome-mag

done

gnome-media

done

gnome-menus

not needed

gnome-panel

done

gnome-power-manager

done

gnome-python-desktop

not needed

gnome-screensaver

done

gnome-session

done

gnome-settings-daemon

done

gnome-speech

not needed

gnome-system-monitor

not needed

gnome-system-tools

done

gnome-terminal

done

gnome-themes

not needed

gnome-user-docs

not needed

gnome-utils

done

gnome-volume-manager

done

gok

done

gstreamer

done

gst-plugins

not needed

gtk-engines

not needed

gtkhtml

not needed

gtksourceview

not needed

gucharmap

done

gvfs

done

libgail-gnome

not needed

libgtop

done

libgweather

done

liboobs

not needed

librsvg

done

libsoup

not needed

libwnck

not needed

metacity

done

mousetweaks

done

nautilus

done

nautilus-cd-burner

done

orca

not needed

pygtksourceview

not needed

scrollkeeper

not needed

Seahorse

done

sound-juicer

done

swfdec-gnome

done

tomboy

not needed

totem-pl-parser

done

totem

done

vinagre

done

vino

not needed

vte

not needed

yelp

done

zenity

done

Desktop (to be deprecated)

gnome-keyring-manager

done

libgnomeprint

done

libgnomeprintui

done

Platform

GConf

done

at-spi

done

atk

not needed

gail

not needed

glib

not needed

gnome-mime-data

not needed

gnome-vfs

done

gtk+

done

gtk-doc

not needed

intltool

not needed

libglade

not needed

libxml2

not needed

libxslt

not needed

pango

done

pkgconfig

not needed

Has its own copy of popt

Platform (to be deprecated)

ORBit2

done

audiofile

not needed

esound

not needed

libIDL

not needed

libart_lgpl

not needed

libbonobo

done

libbonoboui

done

libgnome

done

libgnomecanvas

not needed

libgnomeui

done

Admin

pessulus

not needed

sabayon

not needed

Development Tools

accerciser

not needed

anjuta

done

devhelp

done

gdl

not needed

glade3

done

gnome-build

done

gnome-devel-docs

not needed

Bindings (C++)

glibmm

not needed

gtkmm

not needed

libglademm

not needed

gconfmm

not needed

libgnomecanvasmm

not needed

libgnome-vfsmm

not needed

libgnomemm

done

libgnomeuimm

done

libxml++

not needed

Bindings (java)

glib-java (jg-common)

not needed

libgtk-java

not needed

libgnome-java

not needed

libgconf-java

not needed

libglade-java

not needed

Bindings (perl)

Glib

not needed

Gtk2

not needed

Gtk2-GladeXML

not needed

Gnome2-GConf

not needed

Gnome2-Canvas

not needed

Gnome2-VFS

not needed

Gnome2

not needed

Bindings (python)

pygobject

done

pygtk

not needed

GnomePython

done

Other

camorama

to do

gnome-cups-manager

done

gthumb

done

libgda

done

startup-notification

not needed

Initiatives/GnomeGoals/PoptGOption (last edited 2013-12-18 13:44:47 by LuisMenina)