Hello everyone,
I think GOption is ascii-only on linux (though I’m often wrong!). For example, with this test program:
/* compile with
* gcc -Wall try354.c $(pkg-config glib-2.0 --cflags --libs)
*/
#include <stdio.h>
#include <glib.h>
static char *main_option_expression = NULL;
static GOptionEntry main_batch_options[] = {
{ "expression", 'e', 0, G_OPTION_ARG_STRING, &main_option_expression,
"evaluate and print EXPRESSION", "EXPRESSION" },
{ NULL }
};
int
main(int argc, char **argv)
{
GOptionContext *context = g_option_context_new("goption-test");
GOptionGroup *main_group = g_option_group_new(NULL, NULL, NULL, NULL, NULL);
g_option_group_add_entries(main_group, main_batch_options);
g_option_context_set_main_group(context, main_group);
GError *error = NULL;
if (!g_option_context_parse(context, &argc, &argv, &error)) {
if (error) {
fprintf(stderr, "%s\n", error->message);
g_error_free(error);
}
exit(1);
}
if (main_option_expression)
printf("expression = %s\n", main_option_expression);
return 0;
}
```
I see:
$ ./a.out -e 12
expression = 12
$ ./a.out -e 😀
Invalid byte sequence in conversion input
$
Looking at goption.c:1150, it has:
case G_OPTION_ARG_STRING:
{
gchar *data;
#ifdef G_OS_WIN32
if (!context->strv_mode)
data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
else
data = g_strdup (value);
#else
data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
#endif
ie. on non-win32 platforms it always calls g_locale_to_utf8(), and the default locale is "ANSI_X3.4-1968", which will flag an error for utf8 input. On Windows, you can use g_option_context_parse_strv() instead and avoid this conversion, but on linux it seems to be ASCII only.
Is there a way around this?
(edit: this was with glib 2.88 on ubuntu 26.04, but glib main seems to be the same in this respect)