* { "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, &randomize, "Randomize the data", NULL },
- * { NULL }
+ * G_OPTION_ENTRY_NULL
* };
*
* int
{
GList *groups;
- gchar *parameter_string;
+ gchar *parameter_string; /* (nullable) */
gchar *summary;
gchar *description;
* 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.
context = g_new0 (GOptionContext, 1);
+ /* Clear the empty string to NULL, otherwise we end up calling gettext(""),
+ * which returns the translation header. */
+ if (parameter_string != NULL && *parameter_string == '\0')
+ parameter_string = NULL;
+
context->parameter_string = g_strdup (parameter_string);
context->strict_posix = FALSE;
context->help_enabled = TRUE;
/**
* g_option_context_add_main_entries:
* @context: a #GOptionContext
- * @entries: a %NULL-terminated array of #GOptionEntrys
+ * @entries: (array zero-terminated=1): a %NULL-terminated array of #GOptionEntrys
* @translation_domain: (nullable): a translation domain to use for translating
* the `--help` output for the options in @entries
* with gettext(), or %NULL
return g_string_free (string, FALSE);
}
-G_GNUC_NORETURN
+G_NORETURN
static void
print_help (GOptionContext *context,
gboolean main_help,
if (idx < *argc - 1)
{
- if (!OPTIONAL_ARG (&group->entries[j]))
+ if (OPTIONAL_ARG (&group->entries[j]) && ((*argv)[idx + 1][0] == '-'))
+ value = NULL;
+ else
{
value = (*argv)[idx + 1];
add_pending_null (context, &((*argv)[idx + 1]), NULL);
*new_idx = idx + 1;
}
- else
- {
- if ((*argv)[idx + 1][0] == '-')
- value = NULL;
- else
- {
- value = (*argv)[idx + 1];
- add_pending_null (context, &((*argv)[idx + 1]), NULL);
- *new_idx = idx + 1;
- }
- }
}
else if (idx >= *argc - 1 && OPTIONAL_ARG (&group->entries[j]))
value = NULL;
NULL))
return NULL;
- /* Sanity check for a NUL terminator. */
- g_assert (memchr (cmdline, 0, len));
+ /* g_file_get_contents() guarantees to put a NUL immediately after the
+ * file's contents (at cmdline[len] here), even if the file itself was
+ * not NUL-terminated. */
+ g_assert (memchr (cmdline, 0, len + 1));
/* We could just return cmdline, but I think it's better
* to hold on to a smaller malloc block; the arguments
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));
+ nulled_out = g_newa0 (gboolean, arg_length);
for (j = 0; j < arg_length; j++)
{
if (context->help_enabled && (arg[j] == '?' ||
/**
* g_option_group_add_entries:
* @group: a #GOptionGroup
- * @entries: a %NULL-terminated array of #GOptionEntrys
+ * @entries: (array zero-terminated=1): a %NULL-terminated array of #GOptionEntrys
*
* Adds the options specified in @entries to @group.
*
for (n_entries = 0; entries[n_entries].long_name != NULL; n_entries++) ;
+ g_return_if_fail (n_entries <= G_MAXSIZE - group->n_entries);
+
group->entries = g_renew (GOptionEntry, group->entries, group->n_entries + n_entries);
/* group->entries could be NULL in the trivial case where we add no