/*
 * Copyright © 2009 Christian Persch
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */

#include <stdlib.h>
#include <glib.h>

static gboolean
cb (const gchar *option_name,
    const gchar *value,
    gpointer     data,
    GError     **error)
{
  if (value)
    g_print ("Option %s argument: %s\n", option_name, value);
  else
    g_print ("No argument to option %s.\n", option_name);

  return TRUE;
}

static char *string = NULL;

static GOptionEntry options[] = {
    { "callback", 'c', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, cb, "Callback", "[ARG]" },
    { "callback-required", 'C', 0, G_OPTION_ARG_CALLBACK, cb, "Callback", "[ARG]" },
    { "string", 's', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_STRING, &string, "String", "[ARG]" },
    { "string-required", 'S', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_STRING, &string, "String", "[ARG]" },
    { NULL }
  };

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

  context = g_option_context_new (NULL);
  g_option_context_add_main_entries (context, options, NULL);
  if (!g_option_context_parse (context, &argc, &argv, &error)) {
    g_printerr ("%s\n", error->message);
    g_error_free (error);

    exit (EXIT_FAILURE);
  }

  g_print ("String arg: %s\n", string ? string : "(none)");

  exit (EXIT_SUCCESS);
}
