GApplication: Add g_application_add_main_option

This function adds a single main option entry to be handeled by
GApplication. The option entry has it arg_data field set to NULL
and will be added to the applications packed_options.

The rationale for this is that bindings will be able to add
command line options even when they can't use the un-boxed struct
GOptionEntry.

https://bugzilla.gnome.org/show_bug.cgi?id=727455
This commit is contained in:
Jonas Danielsson 2014-08-13 07:06:36 -04:00
parent 4f775b7b7f
commit bf9c862504
3 changed files with 75 additions and 2 deletions

View File

@ -3040,6 +3040,7 @@ g_application_withdraw_notification
<SUBSECTION> <SUBSECTION>
g_application_run g_application_run
g_application_add_main_option_entries g_application_add_main_option_entries
g_application_add_main_option
g_application_add_option_group g_application_add_option_group
<SUBSECTION> <SUBSECTION>
g_application_set_default g_application_set_default

View File

@ -239,6 +239,9 @@ struct _GApplicationPrivate
GSList *option_groups; GSList *option_groups;
GHashTable *packed_options; GHashTable *packed_options;
gboolean options_parsed; gboolean options_parsed;
/* Allocated option strings, from g_application_add_main_option() */
GSList *option_strings;
}; };
enum enum
@ -664,6 +667,64 @@ g_application_add_main_option_entries (GApplication *application,
} }
} }
/**
* g_application_add_main_option:
* @application: the #GApplication
* @long_name: the long name of an option used to specify it in a commandline
* @short_name: the short name of an option
* @flags: flags from #GOptionFlags
* @arg: the type of the option, as a #GOptionArg
* @description: the description for the option in `--help` output
* @arg_description: (nullable): the placeholder to use for the extra argument
* parsed by the option in `--help` output
*
* Add an option to be handled by @application.
*
* Calling this function is the equivalent of calling
* g_application_add_main_option_entries() with a single #GOptionEntry
* that has its arg_data member set to %NULL.
*
* The parsed arguments will be packed into a #GVariantDict which
* is passed to #GApplication::handle-local-options. If
* %G_APPLICATION_HANDLES_COMMAND_LINE is set, then it will also
* be sent to the primary instance. See
* g_application_add_main_option_entries() for more details.
*
* See #GOptionEntry for more documentation of the arguments.
*
* Since: 2.42
**/
void
g_application_add_main_option (GApplication *application,
const char *long_name,
char short_name,
gint flags,
GOptionArg arg,
const char *description,
const char *arg_description)
{
gchar *dup_string;
GOptionEntry my_entry[2] = {
{ NULL, short_name, flags, arg, NULL, NULL, NULL },
{ NULL }
};
g_return_if_fail (G_IS_APPLICATION (application));
g_return_if_fail (long_name != NULL);
g_return_if_fail (description != NULL);
my_entry[0].long_name = dup_string = g_strdup (long_name);
application->priv->option_strings = g_slist_prepend (application->priv->option_strings, dup_string);
my_entry[0].description = dup_string = g_strdup (description);
application->priv->option_strings = g_slist_prepend (application->priv->option_strings, dup_string);
my_entry[0].arg_description = dup_string = g_strdup (arg_description);
application->priv->option_strings = g_slist_prepend (application->priv->option_strings, dup_string);
g_application_add_main_option_entries (application, my_entry);
}
/** /**
* g_application_add_option_group: * g_application_add_option_group:
* @application: the #GApplication * @application: the #GApplication
@ -1140,8 +1201,10 @@ g_application_finalize (GObject *object)
if (application->priv->main_options) if (application->priv->main_options)
g_option_group_free (application->priv->main_options); g_option_group_free (application->priv->main_options);
if (application->priv->packed_options) if (application->priv->packed_options)
{
g_slist_free_full (application->priv->option_strings, g_free);
g_hash_table_unref (application->priv->packed_options); g_hash_table_unref (application->priv->packed_options);
}
if (application->priv->impl) if (application->priv->impl)
g_application_impl_destroy (application->priv->impl); g_application_impl_destroy (application->priv->impl);
g_free (application->priv->id); g_free (application->priv->id);

View File

@ -168,6 +168,15 @@ void g_application_set_action_group (GApplic
GLIB_AVAILABLE_IN_2_40 GLIB_AVAILABLE_IN_2_40
void g_application_add_main_option_entries (GApplication *application, void g_application_add_main_option_entries (GApplication *application,
const GOptionEntry *entries); const GOptionEntry *entries);
GLIB_AVAILABLE_IN_2_42
void g_application_add_main_option (GApplication *application,
const char *long_name,
char short_name,
gint flags,
GOptionArg arg,
const char *description,
const char *arg_description);
GLIB_AVAILABLE_IN_2_40 GLIB_AVAILABLE_IN_2_40
void g_application_add_option_group (GApplication *application, void g_application_add_option_group (GApplication *application,
GOptionGroup *group); GOptionGroup *group);