Merge branch '976-disable-assert-checks' into 'master'
[glib.git] / gio / tests / gapplication-example-cmdline4.c
blob1120062122f9129b77b316f03880fffc746abb16
1 #include <gio/gio.h>
2 #include <stdlib.h>
3 #include <string.h>
6 static gint
7 handle_local_options (GApplication *application,
8 GVariantDict *options,
9 gpointer user_data)
11 guint32 count;
13 /* Deal (locally) with version option */
14 if (g_variant_dict_lookup (options, "version", "b", &count))
16 g_print ("This is example-cmdline4, version 1.2.3\n");
17 return EXIT_SUCCESS;
20 return -1;
24 static gint
25 command_line (GApplication *application,
26 GApplicationCommandLine *cmdline,
27 gpointer user_data)
29 guint32 count;
31 GVariantDict *options = g_application_command_line_get_options_dict (cmdline);
33 /* Deal with arg option */
34 if (g_variant_dict_lookup (options, "flag", "b", &count))
36 g_application_command_line_print (cmdline, "flag is set\n");
39 return EXIT_SUCCESS;
43 int
44 main (int argc, char **argv)
46 GApplication *app;
47 int status;
49 GOptionEntry entries[] = {
50 /* A version flag option, to be handled locally */
51 { "version", 'v', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, NULL, "Show the application version", NULL },
53 /* A dummy flag option, to be handled in primary */
54 { "flag", 'f', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, NULL, "A flag argument", NULL },
56 { NULL }
59 app = g_application_new ("org.gtk.TestApplication",
60 G_APPLICATION_HANDLES_COMMAND_LINE);
62 g_application_add_main_option_entries (app, entries);
64 g_application_set_option_context_parameter_string (app, "- a simple command line example");
65 g_application_set_option_context_summary (app,
66 "Summary:\n"
67 "This is a simple command line --help example.");
68 g_application_set_option_context_description (app,
69 "Description:\n"
70 "This example illustrates the use of "
71 "g_application command line --help functionalities "
72 "(parameter string, summary, description). "
73 "It does nothing at all except displaying information "
74 "when invoked with --help argument...\n");
76 g_signal_connect (app, "handle-local-options", G_CALLBACK (handle_local_options), NULL);
77 g_signal_connect (app, "command-line", G_CALLBACK (command_line), NULL);
79 /* This application does absolutely nothing, except if a command line is given */
80 status = g_application_run (app, argc, argv);
82 g_object_unref (app);
84 return status;