Glib option parser: Default arguments are overridden



-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello list.

When I tried to use the Glibmm wrapper for the glib option parser I
recognized that it seems to override default settings an argument was
assigned to before calling Glib::OptionContext::parse with zero. I have
attached a slightly modified version of the example from the GTK+
documentation (using plain C glib API) and a Glibmm equivalent as
demonstration.

Calling both programs without any arguments shows the issue. Is this the
intended behavior/am I doing something wrong?

 -- armin

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFEKuZmhOtxKlDYm6cRAsEeAJ4np6fjux6a5BOTFS8moRwa8ZKlKwCg49aC
MmGR0GXTh0LDc75rd5xGgR0=
=b5cX
-----END PGP SIGNATURE-----
#include <stdio.h>
#include <glib.h>

static gint repeats = 2;
static gint max_size = 8;

static GOptionEntry entries[] = 
{
  { "repeats", 'r', 0, G_OPTION_ARG_INT, &repeats, "Average over N repetitions", "N" },
  { "max-size", 'm', 0, G_OPTION_ARG_INT, &max_size, "Test up to 2^M items", "M" },
  { NULL }
};

int 
main (int argc, char *argv[])
{
  GError *error = NULL;
  GOptionContext* context;

  context = g_option_context_new ("- test tree model performance");
  g_option_context_add_main_entries (context, entries, NULL);
  g_option_context_parse (context, &argc, &argv, &error);

  if(error != NULL)
  {
    fprintf(stderr, "%s\n", error->message);
    return -1;
  }

  printf("repeats: %d\n", repeats);
  printf("max_size: %d\n", max_size);

  return 0;
}
#include <iostream>
#include <glibmm.h>

int main(int argc, char *argv[])
{
  GError *error = NULL;
  GOptionContext* context;

  int repeats = 2;
  int max_size = 8;

  Glib::OptionEntry opt_repeats;
  Glib::OptionEntry opt_max_size;
  Glib::OptionGroup group("common", "Common options", "General options");

  opt_repeats.set_short_name('r');
  opt_repeats.set_long_name("repeats");
  opt_repeats.set_description("Average over N repetitions");

  opt_max_size.set_short_name('m');
  opt_max_size.set_long_name("max-size");
  opt_max_size.set_description("Test up to 2^M items");

  group.add_entry(opt_repeats, repeats);
  group.add_entry(opt_max_size, max_size);

  Glib::OptionContext ctx("- test tree model performance");
  ctx.set_main_group(group);

  ctx.parse(argc, argv);

  std::cout << "repeats: " << repeats << std::endl;
  std::cout << "max_size: " << max_size << std::endl;

  return 0;
}
all: gopt goptmm

gopt: gopt.c
	gcc gopt.c -o gopt `pkg-config --cflags --libs glib-2.0`
goptmm: gopt.cpp
	g++ gopt.cpp -o goptmm `pkg-config --cflags --libs glibmm-2.4`


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