6 my_cmdline_handler (gpointer data
)
8 GApplicationCommandLine
*cmdline
= data
;
15 GOptionContext
*context
;
16 GOptionEntry entries
[] = {
17 { "arg1", 0, 0, G_OPTION_ARG_INT
, &arg1
, NULL
, NULL
},
18 { "arg2", 0, 0, G_OPTION_ARG_NONE
, &arg2
, NULL
, NULL
},
19 { "help", '?', 0, G_OPTION_ARG_NONE
, &help
, NULL
, NULL
},
25 args
= g_application_command_line_get_arguments (cmdline
, &argc
);
27 /* We have to make an extra copy of the array, since g_option_context_parse()
28 * assumes that it can remove strings from the array without freeing them.
30 argv
= g_new (gchar
*, argc
+ 1);
31 for (i
= 0; i
<= argc
; i
++)
34 context
= g_option_context_new (NULL
);
35 g_option_context_set_help_enabled (context
, FALSE
);
36 g_option_context_add_main_entries (context
, entries
, NULL
);
42 if (!g_option_context_parse (context
, &argc
, &argv
, &error
))
44 g_application_command_line_printerr (cmdline
, "%s\n", error
->message
);
46 g_application_command_line_set_exit_status (cmdline
, 1);
51 text
= g_option_context_get_help (context
, FALSE
, NULL
);
52 g_application_command_line_print (cmdline
, "%s", text
);
57 g_application_command_line_print (cmdline
, "arg1 is %d and arg2 is %s\n",
58 arg1
, arg2
? "TRUE" : "FALSE");
59 g_application_command_line_set_exit_status (cmdline
, 0);
65 g_option_context_free (context
);
67 /* we are done handling this commandline */
68 g_object_unref (cmdline
);
70 return G_SOURCE_REMOVE
;
74 command_line (GApplication
*application
,
75 GApplicationCommandLine
*cmdline
)
77 /* keep the application running until we are done with this commandline */
78 g_application_hold (application
);
80 g_object_set_data_full (G_OBJECT (cmdline
),
81 "application", application
,
82 (GDestroyNotify
)g_application_release
);
84 g_object_ref (cmdline
);
85 g_idle_add (my_cmdline_handler
, cmdline
);
91 main (int argc
, char **argv
)
96 app
= g_application_new ("org.gtk.TestApplication",
97 G_APPLICATION_HANDLES_COMMAND_LINE
);
98 g_signal_connect (app
, "command-line", G_CALLBACK (command_line
), NULL
);
99 g_application_set_inactivity_timeout (app
, 10000);
101 status
= g_application_run (app
, argc
, argv
);
103 g_object_unref (app
);