1 | #include <gio/gio.h> |
2 | #include <stdlib.h> |
3 | #include <string.h> |
4 | |
5 | |
6 | static gint |
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 (dict: options, key: "version" , format_string: "b" , &count)) |
15 | { |
16 | g_print (format: "This is example-cmdline4, version 1.2.3\n" ); |
17 | return EXIT_SUCCESS; |
18 | } |
19 | |
20 | return -1; |
21 | |
22 | } |
23 | |
24 | static gint |
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 (dict: options, key: "flag" , format_string: "b" , &count)) |
35 | { |
36 | g_application_command_line_print (cmdline, format: "flag is set\n" ); |
37 | } |
38 | |
39 | return EXIT_SUCCESS; |
40 | } |
41 | |
42 | |
43 | int |
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 (application_id: "org.gtk.TestApplication" , |
60 | flags: G_APPLICATION_HANDLES_COMMAND_LINE); |
61 | |
62 | g_application_add_main_option_entries (application: app, entries); |
63 | |
64 | g_application_set_option_context_parameter_string (application: app, parameter_string: "- a simple command line example" ); |
65 | g_application_set_option_context_summary (application: app, |
66 | summary: "Summary:\n" |
67 | "This is a simple command line --help example." ); |
68 | g_application_set_option_context_description (application: app, |
69 | description: "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 (application: app, argc, argv); |
81 | |
82 | g_object_unref (object: app); |
83 | |
84 | return status; |
85 | } |
86 | |