* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
+/**
+ * SECTION:option
+ * @Short_description: parses commandline options
+ * @Title: Commandline option parser
+ *
+ * The GOption commandline parser is intended to be a simpler replacement for the
+ * popt library. It supports short and long commandline options, as shown in the
+ * following example:
+ *
+ * <literal>testtreemodel -r 1 --max-size 20 --rand --display=:1.0 -vb -- file1 file2</literal>
+ *
+ * The example demonstrates a number of features of the GOption commandline parser
+ * <itemizedlist><listitem><para>
+ * Options can be single letters, prefixed by a single dash. Multiple
+ * short options can be grouped behind a single dash.
+ * </para></listitem><listitem><para>
+ * Long options are prefixed by two consecutive dashes.
+ * </para></listitem><listitem><para>
+ * Options can have an extra argument, which can be a number, a string or a
+ * filename. For long options, the extra argument can be appended with an
+ * equals sign after the option name.
+ * </para></listitem><listitem><para>
+ * Non-option arguments are returned to the application as rest arguments.
+ * </para></listitem><listitem><para>
+ * An argument consisting solely of two dashes turns off further parsing,
+ * any remaining arguments (even those starting with a dash) are returned
+ * to the application as rest arguments.
+ * </para></listitem></itemizedlist>
+ *
+ * Another important feature of GOption is that it can automatically generate
+ * nicely formatted help output. Unless it is explicitly turned off with
+ * g_option_context_set_help_enabled(), GOption will recognize the
+ * <option>--help</option>, <option>-?</option>, <option>--help-all</option>
+ * and <option>--help-</option><replaceable>groupname</replaceable> options
+ * (where <replaceable>groupname</replaceable> is the name of a #GOptionGroup)
+ * and write a text similar to the one shown in the following example to stdout.
+ *
+ * <informalexample><screen>
+ * Usage:
+ * testtreemodel [OPTION...] - test tree model performance
+ *
+ * Help Options:
+ * -h, --help Show help options
+ * --help-all Show all help options
+ * --help-gtk Show GTK+ Options
+ *
+ * Application Options:
+ * -r, --repeats=N Average over N repetitions
+ * -m, --max-size=M Test up to 2^M items
+ * --display=DISPLAY X display to use
+ * -v, --verbose Be verbose
+ * -b, --beep Beep when done
+ * --rand Randomize the data
+ * </screen></informalexample>
+ *
+ * GOption groups options in #GOptionGroup<!-- -->s, which makes it easy to
+ * incorporate options from multiple sources. The intended use for this is
+ * to let applications collect option groups from the libraries it uses,
+ * add them to their #GOptionContext, and parse all options by a single call
+ * to g_option_context_parse(). See gtk_get_option_group() for an example.
+ *
+ * If an option is declared to be of type string or filename, GOption takes
+ * care of converting it to the right encoding; strings are returned in UTF-8,
+ * filenames are returned in the GLib filename encoding. Note that this only
+ * works if setlocale() has been called before g_option_context_parse().
+ *
+ * Here is a complete example of setting up GOption to parse the example
+ * commandline above and produce the example help output.
+ *
+ * <informalexample><programlisting>
+ * static gint repeats = 2;
+ * static gint max_size = 8;
+ * static gboolean verbose = FALSE;
+ * static gboolean beep = FALSE;
+ * static gboolean rand = FALSE;
+ *
+ * static GOptionEntry entries[] =
+ * {
+ * { "repeats", 'r', 0, G_OPTION_ARG_INT, &repeats, "Average over N repetitions", "N" },
+ * { "max-size", 'm', 0, G_OPTION_ARG_INT, &max_size, "Test up to 2^M items", "M" },
+ * { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "Be verbose", NULL },
+ * { "beep", 'b', 0, G_OPTION_ARG_NONE, &beep, "Beep when done", NULL },
+ * { "rand", 0, 0, G_OPTION_ARG_NONE, &rand, "Randomize the data", NULL },
+ * { NULL }
+ * };
+ *
+ * int
+ * main (int argc, char *argv[])
+ * {
+ * GError *error = NULL;
+ * GOptionContext *context;
+ *
+ * context = g_option_context_new ("- test tree model performance");
+ * g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
+ * g_option_context_add_group (context, gtk_get_option_group (TRUE));
+ * if (!g_option_context_parse (context, &argc, &argv, &error))
+ * {
+ * g_print ("option parsing failed: %s\n", error->message);
+ * exit (1);
+ * }
+ *
+ * // ...
+ *
+ * }
+ * </programlisting></informalexample>
+ */
#include "config.h"
#include "glibintl.h"
#include "gprintf.h"
-#include "galias.h"
#include <string.h>
#include <stdlib.h>
* Frees context and all the groups which have been
* added to it.
*
+ * Please note that parsed arguments need to be freed separately (see
+ * #GOptionEntry).
+ *
* Since: 2.6
*/
void g_option_context_free (GOptionContext *context)
*
* Enables or disables automatic generation of <option>--help</option>
* output. By default, g_option_context_parse() recognizes
- * <option>--help</option>, <option>-?</option>, <option>--help-all</option>
+ * <option>--help</option>, <option>-h</option>,
+ * <option>-?</option>, <option>--help-all</option>
* and <option>--help-</option><replaceable>groupname</replaceable> and creates
* suitable output to stdout.
*
return FALSE;
}
+static gboolean
+context_has_h_entry (GOptionContext *context)
+{
+ gsize i;
+ GList *list;
+
+ if (context->main_group)
+ {
+ for (i = 0; i < context->main_group->n_entries; i++)
+ {
+ if (context->main_group->entries[i].short_name == 'h')
+ return TRUE;
+ }
+ }
+
+ for (list = context->groups; list != NULL; list = g_list_next (list))
+ {
+ GOptionGroup *group;
+
+ group = (GOptionGroup*)list->data;
+ for (i = 0; i < group->n_entries; i++)
+ {
+ if (group->entries[i].short_name == 'h')
+ return TRUE;
+ }
+ }
+ return FALSE;
+}
+
/**
* g_option_context_get_help:
* @context: a #GOptionContext
gboolean seen[256];
const gchar *rest_description;
GString *string;
+ guchar token;
string = g_string_sized_new (1024);
if (!group)
{
list = context->groups;
-
+
+ token = context_has_h_entry (context) ? '?' : 'h';
+
g_string_append_printf (string, "%s\n -%c, --%-*s %s\n",
- _("Help Options:"), '?', max_length - 4, "help",
+ _("Help Options:"), token, max_length - 4, "help",
_("Show help options"));
/* We only want --help-all when there are groups */
retval = (* (GOptionArgFunc) entry->arg_data) (option_name, data, group->user_data, error);
+ if (!retval && error != NULL && *error == NULL)
+ g_set_error (error,
+ G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
+ _("Error parsing option %s"), option_name);
+
g_free (data);
return retval;
{ /* short option */
gint new_i = i, arg_length;
gboolean *nulled_out = NULL;
+ gboolean has_h_entry = context_has_h_entry (context);
arg = (*argv)[i] + 1;
arg_length = strlen (arg);
nulled_out = g_newa (gboolean, arg_length);
memset (nulled_out, 0, arg_length * sizeof (gboolean));
for (j = 0; j < arg_length; j++)
{
- if (context->help_enabled && arg[j] == '?')
- print_help (context, TRUE, NULL);
- parsed = FALSE;
+ if (context->help_enabled && (arg[j] == '?' ||
+ (arg[j] == 'h' && !has_h_entry)))
+ print_help (context, TRUE, NULL);
+ parsed = FALSE;
if (context->main_group &&
!parse_short_option (context, context->main_group,
i, &new_i, arg[j],
* (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().
+ * domain, see g_option_context_set_translation_domain().
*
* Since: 2.12
**/
* program functionality.
*
* Note that the summary is translated (see
- * g_option_context_set_translate_func(), g_option_context_set_translation_domain()).
+ * g_option_context_set_translate_func() and
+ * g_option_context_set_translation_domain()).
*
* Since: 2.12
*/
return context->description;
}
-
-
-#define __G_OPTION_C__
-#include "galiasdef.c"