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

int main(int argc, char **argv) {
	GOptionEntry opts[] = {
		{ "known",               0, 0, G_OPTION_ARG_FILENAME, NULL, "a known option", "<path>" },

		{ NULL,                  0, 0, 0,                     NULL,                    "", "" }
	};
	int my_argc;
	char **my_argv;

	char *known_option_1 = "--known=value1";
	char *known_option_2 = "--known=value2";
	char *unknown_option = "--unknown=value";

	char *known_option_arg = NULL;

	GOptionContext *opt_ctx = g_option_context_new("a test for specifying a _FILENAME/_STRING option twice and failing afterwards");
	GError *gerr = NULL;

	opts[0].arg_data = &known_option_arg;

	g_option_context_add_main_entries(opt_ctx, opts, NULL);
	g_option_context_set_ignore_unknown_options(opt_ctx, FALSE);

	my_argv = g_new(char *, 4);
	my_argc = 0;
	my_argv[my_argc++] = argv[0];
	my_argv[my_argc++] = known_option_1;
	my_argv[my_argc++] = known_option_2;
	my_argv[my_argc++] = unknown_option;

	if (FALSE == g_option_context_parse(opt_ctx, &my_argc, &my_argv, &gerr)) {
		g_debug("%s: %s",
				G_STRLOC,
				gerr->message);
		g_clear_error(&gerr);
	}

	g_free(my_argv);

	g_option_context_free(opt_ctx);

	/* it should be NULL ... but it isn't */
	g_free(known_option_arg);

	return 0;
}

