I use GLib to parse some command line options. The problem is that I want to make two of these parameters mandatory so that the program terminates using the help screen if the user omits them.
My code is as follows:
static gint line = -1; static gint column = -1; static GOptionEntry options[] = { {"line", 'l', 0, G_OPTION_ARG_INT, &line, "The line", "L"}, {"column", 'c', 0, G_OPTION_ARG_INT, &column, "The column", "C"}, {NULL} }; ... int main(int argc, char** argv) { GError *error = NULL; GOptionContext *context; context = g_option_context_new ("- test"); g_option_context_add_main_entries (context, options, NULL); if (!g_option_context_parse(context, &argc, &argv, &error)) { usage(error->message, context); } ... return 0; }
If I omit one of these parameters, either as on the command line (g_option_context_parse) is still successful and the values in question (row and column) or still -1. How can I tell GLib about parsing failure if the user does not pass both options on the command line? Maybe I'm just blind, but I could not find a flag that I can add to my GOptionEntry data structure to report this so that these fields are required.
Of course, I could check if one of these variables is still -1, but then the user can simply pass this value on the command line, and I want to print a separate error message if the values are out of range.
source share