Add GOption.

2004-08-02  Anders Carlsson  <andersca@gnome.org>

	* glib/Makefile.am:
	* glib/glib.h:
	* glib/goption.c:
	* glib/goption.h:
	* tests/.cvsignore:
	* tests/Makefile.am:
	* tests/option-test.c:
	Add GOption.
This commit is contained in:
Anders Carlsson 2004-08-02 21:40:40 +00:00 committed by Anders Carlsson
parent f1024215a4
commit deee47edfd
12 changed files with 1838 additions and 0 deletions

View File

@ -1,3 +1,14 @@
2004-08-02 Anders Carlsson <andersca@gnome.org>
* glib/Makefile.am:
* glib/glib.h:
* glib/goption.c:
* glib/goption.h:
* tests/.cvsignore:
* tests/Makefile.am:
* tests/option-test.c:
Add GOption.
2004-08-02 Matthias Clasen <mclasen@redhat.com>
* glib/gmacros.h: Add a G_GNUC_INTERNAL macro to mark function

View File

@ -1,3 +1,14 @@
2004-08-02 Anders Carlsson <andersca@gnome.org>
* glib/Makefile.am:
* glib/glib.h:
* glib/goption.c:
* glib/goption.h:
* tests/.cvsignore:
* tests/Makefile.am:
* tests/option-test.c:
Add GOption.
2004-08-02 Matthias Clasen <mclasen@redhat.com>
* glib/gmacros.h: Add a G_GNUC_INTERNAL macro to mark function

View File

@ -1,3 +1,14 @@
2004-08-02 Anders Carlsson <andersca@gnome.org>
* glib/Makefile.am:
* glib/glib.h:
* glib/goption.c:
* glib/goption.h:
* tests/.cvsignore:
* tests/Makefile.am:
* tests/option-test.c:
Add GOption.
2004-08-02 Matthias Clasen <mclasen@redhat.com>
* glib/gmacros.h: Add a G_GNUC_INTERNAL macro to mark function

View File

@ -1,3 +1,14 @@
2004-08-02 Anders Carlsson <andersca@gnome.org>
* glib/Makefile.am:
* glib/glib.h:
* glib/goption.c:
* glib/goption.h:
* tests/.cvsignore:
* tests/Makefile.am:
* tests/option-test.c:
Add GOption.
2004-08-02 Matthias Clasen <mclasen@redhat.com>
* glib/gmacros.h: Add a G_GNUC_INTERNAL macro to mark function

View File

@ -1,3 +1,14 @@
2004-08-02 Anders Carlsson <andersca@gnome.org>
* glib/Makefile.am:
* glib/glib.h:
* glib/goption.c:
* glib/goption.h:
* tests/.cvsignore:
* tests/Makefile.am:
* tests/option-test.c:
Add GOption.
2004-08-02 Matthias Clasen <mclasen@redhat.com>
* glib/gmacros.h: Add a G_GNUC_INTERNAL macro to mark function

View File

@ -63,6 +63,7 @@ libglib_2_0_la_SOURCES = \
gmem.c \
gmessages.c \
gnode.c \
goption.c \
gpattern.c \
gprimes.c \
gqsort.c \
@ -133,6 +134,7 @@ glibsubinclude_HEADERS = \
gmem.h \
gmessages.h \
gnode.h \
goption.h \
gpattern.h \
gprimes.h \
gqsort.h \

View File

@ -50,6 +50,7 @@
#include <glib/gmem.h>
#include <glib/gmessages.h>
#include <glib/gnode.h>
#include <glib/goption.h>
#include <glib/gpattern.h>
#include <glib/gprimes.h>
#include <glib/gqsort.h>

1195
glib/goption.c Normal file

File diff suppressed because it is too large Load Diff

144
glib/goption.h Normal file
View File

@ -0,0 +1,144 @@
/* goption.h - Option parser
*
* Copyright (C) 2004 Anders Carlsson <andersca@gnome.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifndef __G_OPTION_H__
#define __G_OPTION_H__
#include <glib/gerror.h>
#include <glib/gquark.h>
G_BEGIN_DECLS
/* Should go into gtypes.h */
typedef const gchar * (*GTranslateFunc) (const gchar *str,
gpointer data);
typedef struct _GOptionContext GOptionContext;
typedef struct _GOptionGroup GOptionGroup;
typedef struct _GOptionEntry GOptionEntry;
typedef enum
{
G_OPTION_FLAG_HIDDEN = 1 << 0,
G_OPTION_FLAG_IN_MAIN = 1 << 1,
} GOptionFlags;
typedef enum
{
G_OPTION_ARG_NONE,
G_OPTION_ARG_STRING,
G_OPTION_ARG_INT,
G_OPTION_ARG_CALLBACK,
G_OPTION_ARG_FILENAME,
G_OPTION_ARG_STRING_ARRAY,
G_OPTION_ARG_FILENAME_ARRAY,
} GOptionArg;
typedef gboolean (*GOptionArgFunc) (const gchar *option_name,
const gchar *value,
gpointer data,
GError **error);
typedef gboolean (*GOptionParseFunc) (GOptionContext *context,
GOptionGroup *group,
gpointer data,
GError **error);
typedef void (*GOptionErrorFunc) (GOptionContext *context,
GOptionGroup *group,
gpointer data,
GError **error);
#define G_OPTION_ERROR (g_option_context_error_quark ())
typedef enum
{
G_OPTION_ERROR_UNKNOWN_OPTION,
G_OPTION_ERROR_BAD_VALUE,
G_OPTION_ERROR_FAILED
} GOptionError;
GQuark g_option_context_error_quark (void) G_GNUC_CONST;
struct _GOptionEntry
{
const char *long_name;
char short_name;
int flags;
GOptionArg arg;
gpointer arg_data;
const char *description;
const char *arg_description;
};
GOptionContext *g_option_context_new (const gchar *parameter_string);
void g_option_context_free (GOptionContext *context);
void g_option_context_set_help_enabled (GOptionContext *context,
gboolean help_enabled);
gboolean g_option_context_get_help_enabled (GOptionContext *context);
void g_option_context_set_ignore_unknown_options (GOptionContext *context,
gboolean ignore_unknown);
gboolean g_option_context_get_ignore_unknown_options (GOptionContext *context);
void g_option_context_add_main_entries (GOptionContext *context,
const GOptionEntry *entries,
const gchar *translation_domain);
gboolean g_option_context_parse (GOptionContext *context,
gint *argc,
gchar ***argv,
GError **error);
void g_option_context_add_group (GOptionContext *context,
GOptionGroup *group);
void g_option_context_set_main_group (GOptionContext *context,
GOptionGroup *group);
GOptionGroup *g_option_context_get_main_group (GOptionContext *context);
GOptionGroup *g_option_group_new (const gchar *name,
const gchar *description,
const char *help_description,
gpointer user_data,
GDestroyNotify destroy);
void g_option_group_set_parse_hooks (GOptionGroup *group,
GOptionParseFunc pre_parse_func,
GOptionParseFunc post_parse_func);
void g_option_group_set_error_hook (GOptionGroup *group,
GOptionErrorFunc error_func);
void g_option_group_free (GOptionGroup *group);
void g_option_group_add_entries (GOptionGroup *group,
const GOptionEntry *entries);
void g_option_group_set_translate_func (GOptionGroup *group,
GTranslateFunc func,
gpointer data,
GDestroyNotify destroy_notify);
void g_option_group_set_translation_domain (GOptionGroup *group,
const gchar *domain);
G_END_DECLS
#endif /* __G_OPTION_H__ */

View File

@ -33,6 +33,7 @@ markup-test
markup-escape-test
module-test
node-test
option-test
printf-test
queue-test
qsort-test

View File

@ -79,6 +79,7 @@ test_programs = \
markup-escape-test \
module-test \
node-test \
option-test \
patterntest \
printf-test \
queue-test \
@ -132,6 +133,7 @@ markup_escape_test_LDADD = $(progs_ldadd)
module_test_LDADD = $(module_ldadd) $(module_test_exp)
module_test_LDFLAGS = $(G_MODULE_LDFLAGS)
node_test_LDADD = $(progs_ldadd)
option_test_LDADD = $(progs_ldadd)
printf_test_LDADD = $(progs_ldadd)
queue_test_LDADD = $(progs_ldadd)
qsort_test_LDADD = $(progs_ldadd)

438
tests/option-test.c Normal file
View File

@ -0,0 +1,438 @@
#include <glib.h>
#include "goption.h"
#include <string.h>
int error_test1_int;
char *error_test2_string;
gboolean error_test3_boolean;
int arg_test1_int;
gchar *arg_test2_string;
gchar **array_test1_array;
gboolean ignore_test1_boolean;
gboolean ignore_test2_boolean;
gchar **
split_string (const char *str, int *argc)
{
gchar **argv;
int len;
argv = g_strsplit (str, " ", 0);
for (len = 0; argv[len] != NULL; len++);
if (argc)
*argc = len;
return argv;
}
gchar *
join_stringv (int argc, char **argv)
{
int i;
GString *str;
str = g_string_new (NULL);
for (i = 0; i < argc; i++)
{
g_string_append (str, argv[i]);
if (i < argc - 1)
g_string_append_c (str, ' ');
}
return g_string_free (str, FALSE);
}
/* Performs a shallow copy */
char **
copy_stringv (char **argv, int argc)
{
return g_memdup (argv, sizeof (char *) * (argc + 1));
}
static gboolean
error_test1_pre_parse (GOptionContext *context,
GOptionGroup *group,
gpointer data,
GError **error)
{
g_assert (error_test1_int == 0x12345678);
return TRUE;
}
static gboolean
error_test1_post_parse (GOptionContext *context,
GOptionGroup *group,
gpointer data,
GError **error)
{
g_assert (error_test1_int == 20);
/* Set an error in the post hook */
g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, NULL);
return FALSE;
}
void
error_test1 (void)
{
GOptionContext *context;
gboolean retval;
GError *error = NULL;
gchar **argv;
int argc;
GOptionGroup *main_group;
GOptionEntry entries [] =
{ { "test", 0, 0, G_OPTION_ARG_INT, &error_test1_int, NULL, NULL },
{ NULL } };
context = g_option_context_new (NULL);
g_option_context_add_main_entries (context, entries, NULL);
/* Set pre and post parse hooks */
main_group = g_option_context_get_main_group (context);
g_option_group_set_parse_hooks (main_group,
error_test1_pre_parse, error_test1_post_parse);
/* Now try parsing */
argv = split_string ("program --test 20", &argc);
retval = g_option_context_parse (context, &argc, &argv, &error);
g_assert (retval == FALSE);
/* On failure, values should be reset */
g_assert (error_test1_int == 0x12345678);
g_strfreev (argv);
g_option_context_free (context);
}
static gboolean
error_test2_pre_parse (GOptionContext *context,
GOptionGroup *group,
gpointer data,
GError **error)
{
g_assert (strcmp (error_test2_string, "foo") == 0);
return TRUE;
}
static gboolean
error_test2_post_parse (GOptionContext *context,
GOptionGroup *group,
gpointer data,
GError **error)
{
g_assert (strcmp (error_test2_string, "bar") == 0);
/* Set an error in the post hook */
g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, NULL);
return FALSE;
}
void
error_test2 (void)
{
GOptionContext *context;
gboolean retval;
GError *error = NULL;
gchar **argv;
int argc;
GOptionGroup *main_group;
GOptionEntry entries [] =
{ { "test", 0, 0, G_OPTION_ARG_STRING, &error_test2_string, NULL, NULL },
{ NULL } };
context = g_option_context_new (NULL);
g_option_context_add_main_entries (context, entries, NULL);
/* Set pre and post parse hooks */
main_group = g_option_context_get_main_group (context);
g_option_group_set_parse_hooks (main_group,
error_test2_pre_parse, error_test2_post_parse);
/* Now try parsing */
argv = split_string ("program --test bar", &argc);
retval = g_option_context_parse (context, &argc, &argv, &error);
g_error_free (error);
g_assert (retval == FALSE);
g_assert (strcmp (error_test2_string, "foo") == 0);
g_strfreev (argv);
g_option_context_free (context);
}
static gboolean
error_test3_pre_parse (GOptionContext *context,
GOptionGroup *group,
gpointer data,
GError **error)
{
g_assert (!error_test3_boolean);
return TRUE;
}
static gboolean
error_test3_post_parse (GOptionContext *context,
GOptionGroup *group,
gpointer data,
GError **error)
{
g_assert (error_test3_boolean);
/* Set an error in the post hook */
g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, NULL);
return FALSE;
}
void
error_test3 (void)
{
GOptionContext *context;
gboolean retval;
GError *error = NULL;
gchar **argv;
int argc;
GOptionGroup *main_group;
GOptionEntry entries [] =
{ { "test", 0, 0, G_OPTION_ARG_NONE, &error_test3_boolean, NULL, NULL },
{ NULL } };
context = g_option_context_new (NULL);
g_option_context_add_main_entries (context, entries, NULL);
/* Set pre and post parse hooks */
main_group = g_option_context_get_main_group (context);
g_option_group_set_parse_hooks (main_group,
error_test3_pre_parse, error_test3_post_parse);
/* Now try parsing */
argv = split_string ("program --test", &argc);
retval = g_option_context_parse (context, &argc, &argv, &error);
g_error_free (error);
g_assert (retval == FALSE);
g_assert (!error_test3_boolean);
g_strfreev (argv);
g_option_context_free (context);
}
void
arg_test1 (void)
{
GOptionContext *context;
gboolean retval;
GError *error = NULL;
gchar **argv;
int argc;
GOptionEntry entries [] =
{ { "test", 0, 0, G_OPTION_ARG_INT, &arg_test1_int, NULL, NULL },
{ NULL } };
context = g_option_context_new (NULL);
g_option_context_add_main_entries (context, entries, NULL);
/* Now try parsing */
argv = split_string ("program --test 20 --test 30", &argc);
retval = g_option_context_parse (context, &argc, &argv, &error);
g_assert (retval);
/* Last arg specified is the one that should be stored */
g_assert (arg_test1_int == 30);
g_strfreev (argv);
g_option_context_free (context);
}
void
arg_test2 (void)
{
GOptionContext *context;
gboolean retval;
GError *error = NULL;
gchar **argv;
int argc;
GOptionEntry entries [] =
{ { "test", 0, 0, G_OPTION_ARG_STRING, &arg_test2_string, NULL, NULL },
{ NULL } };
context = g_option_context_new (NULL);
g_option_context_add_main_entries (context, entries, NULL);
/* Now try parsing */
argv = split_string ("program --test foo --test bar", &argc);
retval = g_option_context_parse (context, &argc, &argv, &error);
g_assert (retval);
/* Last arg specified is the one that should be stored */
g_assert (strcmp (arg_test2_string, "bar") == 0);
g_free (arg_test2_string);
g_strfreev (argv);
g_option_context_free (context);
}
void
ignore_test1 (void)
{
GOptionContext *context;
gboolean retval;
GError *error = NULL;
gchar **argv, **argv_copy;
int argc;
gchar *arg;
GOptionEntry entries [] =
{ { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
{ NULL } };
context = g_option_context_new (NULL);
g_option_context_set_ignore_unknown_options (context, TRUE);
g_option_context_add_main_entries (context, entries, NULL);
/* Now try parsing */
argv = split_string ("program --test --hello", &argc);
argv_copy = copy_stringv (argv, argc);
retval = g_option_context_parse (context, &argc, &argv, &error);
g_assert (retval);
/* Check array */
arg = join_stringv (argc, argv);
g_assert (strcmp (arg, "program --hello") == 0);
g_free (arg);
g_strfreev (argv_copy);
g_free (argv);
g_option_context_free (context);
}
void
ignore_test2 (void)
{
GOptionContext *context;
gboolean retval;
GError *error = NULL;
gchar **argv;
int argc;
gchar *arg;
GOptionEntry entries [] =
{ { "test", 't', 0, G_OPTION_ARG_NONE, &ignore_test2_boolean, NULL, NULL },
{ NULL } };
context = g_option_context_new (NULL);
g_option_context_set_ignore_unknown_options (context, TRUE);
g_option_context_add_main_entries (context, entries, NULL);
/* Now try parsing */
argv = split_string ("program -test", &argc);
retval = g_option_context_parse (context, &argc, &argv, &error);
g_assert (retval);
/* Check array */
arg = join_stringv (argc, argv);
g_assert (strcmp (arg, "program -es") == 0);
g_free (arg);
g_strfreev (argv);
g_option_context_free (context);
}
void
array_test1 (void)
{
GOptionContext *context;
gboolean retval;
GError *error = NULL;
gchar **argv;
int argc;
GOptionEntry entries [] =
{ { "test", 0, 0, G_OPTION_ARG_STRING_ARRAY, &array_test1_array, NULL, NULL },
{ NULL } };
context = g_option_context_new (NULL);
g_option_context_add_main_entries (context, entries, NULL);
/* Now try parsing */
argv = split_string ("program --test foo --test bar", &argc);
retval = g_option_context_parse (context, &argc, &argv, &error);
g_assert (retval);
/* Check array */
g_assert (strcmp (array_test1_array[0], "foo") == 0);
g_assert (strcmp (array_test1_array[1], "bar") == 0);
g_assert (array_test1_array[2] == NULL);
g_strfreev (array_test1_array);
g_strfreev (argv);
g_option_context_free (context);
}
void
add_test1 (void)
{
GOptionContext *context;
GOptionEntry entries1 [] =
{ { "test1", 0, 0, G_OPTION_ARG_STRING_ARRAY, NULL, NULL, NULL },
{ NULL } };
GOptionEntry entries2 [] =
{ { "test2", 0, 0, G_OPTION_ARG_STRING_ARRAY, NULL, NULL, NULL },
{ NULL } };
context = g_option_context_new (NULL);
g_option_context_add_main_entries (context, entries1, NULL);
g_option_context_add_main_entries (context, entries2, NULL);
g_option_context_free (context);
}
int
main (int argc, char **argv)
{
/* Test that restoration on failure works */
error_test1_int = 0x12345678;
error_test1 ();
error_test2_string = "foo";
error_test2 ();
error_test3_boolean = FALSE;
error_test3 ();
/* Test that special argument parsing works */
arg_test1 ();
arg_test2 ();
/* Test string arrays */
array_test1 ();
/* Test ignoring options */
ignore_test1 ();
ignore_test2 ();
add_test1 ();
return 0;
}