1 #include <gio/gio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5
6 static gint
handle_local_options(GApplication * application,GVariantDict * options,gpointer user_data)7 handle_local_options (GApplication *application,
8 GVariantDict *options,
9 gpointer user_data)
10 {
11 guint32 count;
12
13 /* Deal (locally) with version option */
14 if (g_variant_dict_lookup (options, "version", "b", &count))
15 {
16 g_print ("This is example-cmdline4, version 1.2.3\n");
17 return EXIT_SUCCESS;
18 }
19
20 return -1;
21
22 }
23
24 static gint
command_line(GApplication * application,GApplicationCommandLine * cmdline,gpointer user_data)25 command_line (GApplication *application,
26 GApplicationCommandLine *cmdline,
27 gpointer user_data)
28 {
29 guint32 count;
30
31 GVariantDict *options = g_application_command_line_get_options_dict (cmdline);
32
33 /* Deal with arg option */
34 if (g_variant_dict_lookup (options, "flag", "b", &count))
35 {
36 g_application_command_line_print (cmdline, "flag is set\n");
37 }
38
39 return EXIT_SUCCESS;
40 }
41
42
43 int
main(int argc,char ** argv)44 main (int argc, char **argv)
45 {
46 GApplication *app;
47 int status;
48
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 },
52
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 },
55
56 { NULL }
57 };
58
59 app = g_application_new ("org.gtk.TestApplication",
60 G_APPLICATION_HANDLES_COMMAND_LINE);
61
62 g_application_add_main_option_entries (app, entries);
63
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");
75
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);
78
79 /* This application does absolutely nothing, except if a command line is given */
80 status = g_application_run (app, argc, argv);
81
82 g_object_unref (app);
83
84 return status;
85 }
86