Add tests for the handling of non-option arguments, "--" and

2004-10-29  Matthias Clasen  <mclasen@redhat.com>

	* tests/option-test.c: Add tests for the handling of
	non-option arguments, "--" and G_OPTION_REMAINING.

	* glib/goption.[hc]: #define G_OPTION_REMAINING, which is
	a special long option name, which can be used for an option
	in the main group which collects the non-option arguments.
	It must be of type G_OPTION_ARG_STRING_ARRAY or
	G_OPTION_ARG_FILENAME_ARRAY. If the main group doesn't contain
	an option whose name is G_OPTION_REMAINING, the non-option
	arguments are left behind in argv as before.
This commit is contained in:
Matthias Clasen
2004-10-29 20:19:06 +00:00
committed by Matthias Clasen
parent a0e217e94a
commit a9fa61a13e
8 changed files with 304 additions and 10 deletions

View File

@@ -524,6 +524,184 @@ empty_test3 (void)
g_option_context_free (context);
}
/* check that non-option arguments are left in argv by default */
void
rest_test1 (void)
{
GOptionContext *context;
gboolean retval;
GError *error = NULL;
gchar **argv;
int argc;
GOptionEntry entries [] = {
{ "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, 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 foo --test bar", &argc);
retval = g_option_context_parse (context, &argc, &argv, &error);
g_assert (retval);
/* Check array */
g_assert (ignore_test1_boolean);
g_assert (strcmp (argv[0], "program") == 0);
g_assert (strcmp (argv[1], "foo") == 0);
g_assert (strcmp (argv[2], "bar") == 0);
g_assert (argv[3] == NULL);
g_strfreev (argv);
g_option_context_free (context);
}
/* check that -- works */
void
rest_test2 (void)
{
GOptionContext *context;
gboolean retval;
GError *error = NULL;
gchar **argv;
int argc;
GOptionEntry entries [] = {
{ "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, 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 foo --test -- -bar", &argc);
retval = g_option_context_parse (context, &argc, &argv, &error);
g_assert (retval);
/* Check array */
g_assert (ignore_test1_boolean);
g_assert (strcmp (argv[0], "program") == 0);
g_assert (strcmp (argv[1], "foo") == 0);
g_assert (strcmp (argv[2], "-bar") == 0);
g_assert (argv[3] == NULL);
g_strfreev (argv);
g_option_context_free (context);
}
/* check that G_OPTION_REMAINING collects non-option arguments */
void
rest_test3 (void)
{
GOptionContext *context;
gboolean retval;
GError *error = NULL;
gchar **argv;
int argc;
GOptionEntry entries [] = {
{ "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
{ G_OPTION_REMAINING, 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 foo --test bar", &argc);
retval = g_option_context_parse (context, &argc, &argv, &error);
g_assert (retval);
/* Check array */
g_assert (ignore_test1_boolean);
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);
}
/* check that G_OPTION_REMAINING and -- work together */
void
rest_test4 (void)
{
GOptionContext *context;
gboolean retval;
GError *error = NULL;
gchar **argv;
int argc;
GOptionEntry entries [] = {
{ "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
{ G_OPTION_REMAINING, 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 foo --test -- -bar", &argc);
retval = g_option_context_parse (context, &argc, &argv, &error);
g_assert (retval);
/* Check array */
g_assert (ignore_test1_boolean);
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);
}
/* test that G_OPTION_REMAINING works with G_OPTION_ARG_FILENAME_ARRAY */
void
rest_test5 (void)
{
GOptionContext *context;
gboolean retval;
GError *error = NULL;
gchar **argv;
int argc;
GOptionEntry entries [] = {
{ "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
{ G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_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 foo --test bar", &argc);
retval = g_option_context_parse (context, &argc, &argv, &error);
g_assert (retval);
/* Check array */
g_assert (ignore_test1_boolean);
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);
}
int
main (int argc, char **argv)
{
@@ -555,5 +733,12 @@ main (int argc, char **argv)
empty_test2 ();
empty_test3 ();
/* Test handling of rest args */
rest_test1 ();
rest_test2 ();
rest_test3 ();
rest_test4 ();
rest_test5 ();
return 0;
}