I asked this over on StackOverflow a couple of days ago, but no responses so I figured I’d try here. Feel free to respond in either place.
Writing a Gtk (3.0, for the moment) application’s main()
function and installing the standard GLib/Gio GApplication
scaffolding, I’ve hit a snag when it comes to command-line argument processing.
I’m already convinced I don’t want to handle the commandline myself if I can avoid it, because that’s exactly what the toolkits are supposed to be there for. At least, in theory.
So the command line is set up by defining a GOptionEntry
array and passing it to g_application_add_main_option_entries()
in the usual fashion:
GOptionEntry options[] =
{
{ "verbose", 'v', 0,
G_OPTION_ARG_NONE, &opt_verbose,
"Be verbose", NULL },
{ "otherarg", 'o', 0,
G_OPTION_ARG_STRING, &opt_other,
"Secret", NULL },
{ NULL }
};
app = gtk_application_new("org.me.myapp", G_APPLICATION_NON_UNIQUE);
g_application_add_main_option_entries(G_APPLICATION(app), options);
So far, so good. But now I want to include a --debug
flag, and modify the G_MESSAGES_DEBUG
environment variable to append the G_LOG_DOMAIN
when debugging is enabled. (I’d rather not write my own log handling, either.)
That part’s also easy enough, using an argument-processing callback:
gboolean
enable_debug (const gchar* option_name, const gchar* value,
gpointer data, GError** error)
{
// ...
}
GOptionEntry options[] =
{
{ "verbose", 'v', 0,
G_OPTION_ARG_NONE, &opt_verbose,
"Be verbose", NULL },
{ "otherarg", 'o', 0,
G_OPTION_ARG_STRING, &opt_other,
"Secret", NULL },
{ "debug", 'd', G_OPTION_FLAG_NO_ARG,
G_OPTION_ARG_CALLBACK, &enable_debug,
"Enable debug output", NULL },
{ NULL }
};
But, now say I want to actually have that gpointer data
set to something other than NULL
, and pass some user data to enable_debug()
. (Specifically, give it a place to also store the debug flag, when enabled.)
According to the docs, data
carries…
User data added to the
GOptionGroup
containing the option when it was created withg_option_group_new()
But I’m not using g_option_group_new()
.
I’d be happy to use g_option_group_new()
, but AFAICT there’s no way to create the main GApplication
option group “by hand” (by calling g_option_group_new()
). It’s only created implicitly by calling g_application_add_main_option_entries()
, which doesn’t allow user data to be attached to the group.
And if I were to create a new GOptionGroup
for the --debug
option, using g_option_group_new()
followed by g_application_add_option_group()
, --debug
would be relegated to a different option group than the rest of my arguments, and it wouldn’t be shown in the main --help
output — the user would have to run myapp --help-debugging
(or whatever I named it) in order to see the --debug
flag in the argument synopsis.
Do I have all that right? Are GOptionArgFunc
callbacks that take user data really mutually exclusive with arguments that are part of the application’s main option group? Is there a reason why that would be the case, or is it just a weird hole in the API?