struct _GOptionContext
{
- GList *groups;
+ GList *groups;
- gchar *parameter_string;
+ gchar *parameter_string;
+ gchar *summary;
+ gchar *description;
- gboolean help_enabled;
- gboolean ignore_unknown;
+ GTranslateFunc translate_func;
+ GDestroyNotify translate_notify;
+ gpointer translate_data;
+
+ guint help_enabled : 1;
+ guint ignore_unknown : 1;
- GOptionGroup *main_group;
+ GOptionGroup *main_group;
/* We keep a list of change so we can revert them */
- GList *changes;
+ GList *changes;
- /* We also keep track of all argv elements that should be NULLed or
- * modified.
+ /* We also keep track of all argv elements
+ * that should be NULLed or modified.
*/
- GList *pending_nulls;
+ GList *pending_nulls;
};
struct _GOptionGroup
{
- gchar *name;
- gchar *description;
- gchar *help_description;
+ gchar *name;
+ gchar *description;
+ gchar *help_description;
- GDestroyNotify destroy_notify;
- gpointer user_data;
+ GDestroyNotify destroy_notify;
+ gpointer user_data;
- GTranslateFunc translate_func;
- GDestroyNotify translate_notify;
- gpointer translate_data;
+ GTranslateFunc translate_func;
+ GDestroyNotify translate_notify;
+ gpointer translate_data;
- GOptionEntry *entries;
- gint n_entries;
+ GOptionEntry *entries;
+ gint n_entries;
GOptionParseFunc pre_parse_func;
GOptionParseFunc post_parse_func;
*
* Creates a new option context.
*
- * The @parameter_text can serve multiple purposes. It can be used
+ * The @parameter_string can serve multiple purposes. It can be used
* to add descriptions for "rest" arguments, which are not parsed by
* the #GOptionContext, typically something like "FILES" or
- * "FILE1 FILE2...". (If you are using #G_OPTION_REMAINING for
+ * "FILE1 FILE2...". If you are using #G_OPTION_REMAINING for
* collecting "rest" arguments, GLib handles this automatically by
* using the @arg_description of the corresponding #GOptionEntry in
- * the usage summary.)
+ * the usage summary.
+ *
+ * Another usage is to give a short summary of the program
+ * functionality, like " - frob the strings", which will be displayed
+ * in the same line as the usage. For a longer description of the
+ * program functionality that should be displayed as a paragraph
+ * below the usage line, use g_option_context_set_summary().
*
- * Another common usage is to give a summary of the program
- * functionality. This can be a short summary on the same line,
- * like " - frob the strings", or a longer description in a paragraph
- * below the usage summary. In this case, @parameter_string should start
- * with two newlines, to separate the description from the usage summary:
- * "\n\nA program to frob strings, which will..."
+ * Note that the @parameter_string is translated (see
+ * g_option_context_set_translate_func()).
*
* Returns: a newly created #GOptionContext, which must be
* freed with g_option_context_free() after use.
free_pending_nulls (context, FALSE);
g_free (context->parameter_string);
+ g_free (context->summary);
+ g_free (context->description);
+ if (context->translate_notify)
+ (* context->translate_notify) (context->translate_data);
+
g_free (context);
}
rest_description = NULL;
if (context->main_group)
{
+
for (i = 0; i < context->main_group->n_entries; i++)
{
entry = &context->main_group->entries[i];
if (entry->long_name[0] == 0)
{
- rest_description = entry->arg_description;
+ rest_description = TRANSLATE (context->main_group, entry->arg_description);
break;
}
}
}
-
+
g_print ("%s\n %s %s%s%s%s%s\n\n",
_("Usage:"), g_get_prgname(), _("[OPTION...]"),
rest_description ? " " : "",
rest_description ? rest_description : "",
context->parameter_string ? " " : "",
- context->parameter_string ? context->parameter_string : "");
+ context->parameter_string ? TRANSLATE (context, context->parameter_string) : "");
+
+ if (context->summary)
+ g_print ("%s\n\n", TRANSLATE (context, context->summary));
memset (seen, 0, sizeof (gboolean) * 256);
shadow_map = g_hash_table_new (g_str_hash, g_str_equal);
g_print ("\n");
}
-
+
+ if (context->description)
+ g_print ("%s\n", TRANSLATE (context, context->description));
+
exit (0);
}
g_free);
}
+/**
+ * g_option_context_set_translate_func:
+ * @context: a #GOptionContext
+ * @func: the #GTranslateFunc, or %NULL
+ * @data: user data to pass to @func, or %NULL
+ * @destroy_notify: a function which gets called to free @data, or %NULL
+ *
+ * Sets the function which is used to translate the contexts
+ * user-visible strings, for <option>--help</option> output.
+ * If @func is %NULL, strings are not translated.
+ *
+ * Note that option groups have their own translation functions,
+ * this function only affects the @parameter_string (see g_option_context_nex()),
+ * the summary (see g_option_context_set_summary()) and the description
+ * (see g_option_context_set_description()).
+ *
+ * If you are using gettext(), you only need to set the translation
+ * domain, see g_context_group_set_translation_domain().
+ *
+ * Since: 2.12
+ **/
+void
+g_option_context_set_translate_func (GOptionContext *context,
+ GTranslateFunc func,
+ gpointer data,
+ GDestroyNotify destroy_notify)
+{
+ g_return_if_fail (context != NULL);
+
+ if (context->translate_notify)
+ context->translate_notify (context->translate_data);
+
+ context->translate_func = func;
+ context->translate_data = data;
+ context->translate_notify = destroy_notify;
+}
+
+/**
+ * g_option_context_set_translation_domain:
+ * @context: a #GOptionContext
+ * @domain: the domain to use
+ *
+ * A convenience function to use gettext() for translating
+ * user-visible strings.
+ *
+ * Since: 2.12
+ **/
+void
+g_option_context_set_translation_domain (GOptionContext *context,
+ const gchar *domain)
+{
+ g_return_if_fail (context != NULL);
+
+ g_option_context_set_translate_func (context,
+ (GTranslateFunc)dgettext_swapped,
+ g_strdup (domain),
+ g_free);
+}
+
+/**
+ * g_option_context_set_summary:
+ * @context: a #GOptionContext
+ * @summary: a string to be shown in <option>--help</option> output
+ * before the list of options, or %NULL
+ *
+ * Adds a string to be displayed in <option>--help</option> output
+ * before the list of options. This is typically a summary of the
+ * program functionality.
+ *
+ * Note that the summary is translated (see
+ * g_option_context_set_translate_func()).
+ *
+ * Since: 2.12
+ */
+void
+g_option_context_set_summary (GOptionContext *context,
+ const gchar *summary)
+{
+ g_return_if_fail (context != NULL);
+
+ g_free (context->summary);
+ context->summary = g_strdup (summary);
+}
+
+
+/**
+ * g_option_context_get_summary:
+ * @context: a #GOptionContext
+ *
+ * Returns the summary. See g_option_context_set_summary().
+ *
+ * Returns: the summary
+ *
+ * Since: 2.12
+ */
+G_CONST_RETURN gchar *
+g_option_context_get_summary (GOptionContext *context)
+{
+ g_return_val_if_fail (context != NULL, NULL);
+
+ return context->summary;
+}
+
+/**
+ * g_option_context_set_description:
+ * @context: a #GOptionContext
+ * @description: a string to be shown in <option>--help</option> output
+ * after the list of options, or %NULL
+ *
+ * Adds a string to be displayed in <option>--help</option> output
+ * after the list of options. This text often includes a bug reporting
+ * address.
+ *
+ * Note that the summary is translated (see
+ * g_option_context_set_translate_func()).
+ *
+ * Since: 2.12
+ */
+void
+g_option_context_set_description (GOptionContext *context,
+ const gchar *description)
+{
+ g_return_if_fail (context != NULL);
+
+ g_free (context->description);
+ context->description = g_strdup (description);
+}
+
+
+/**
+ * g_option_context_get_description:
+ * @context: a #GOptionContext
+ *
+ * Returns the description. See g_option_context_set_description().
+ *
+ * Returns: the description
+ *
+ * Since: 2.12
+ */
+G_CONST_RETURN gchar *
+g_option_context_get_description (GOptionContext *context)
+{
+ g_return_val_if_fail (context != NULL, NULL);
+
+ return context->description;
+}
+
+
#define __G_OPTION_C__
#include "galiasdef.c"