Comand line arguments with GAction not works (GTK4)

,

How to handle comand line arguments because I am trying to work with GApplication (not GtkApplication) and two of its Signals ( command-line and handle-local-options)?

The program flow should be like this:

  • the user decides if activate callback should be called by passing noactivate as argument and g_application_release should be called if the case.

  • program version should be printed, when version is passed.

Nothing wokrs, how should I go with this one?

#include <gtk/gtk.h>

static void startup_clbk ( GApplication *application )
{
    g_return_if_fail ( G_IS_APPLICATION ( application ) );

    /// ***
    g_print ( "Started up\n" );

}

static void activate ( GApplication *application )
{
    g_return_if_fail ( G_IS_APPLICATION ( application ) );

    /// ***
    g_print ( "activated\n" );
}

static gint command_line_clbk ( GApplication *application,
                                GApplicationCommandLine *cmdline )
{
    GVariantDict *options;
    const gchar *no_activate = NULL;

    /// ***
    options = g_application_command_line_get_options_dict ( cmdline );
    g_variant_dict_lookup ( options, "noactivate", "&s", &no_activate );

    /// ***
    if ( no_activate )
    {
        g_print ( "Exiting...\n" );
        g_application_release ( application );

        /// ***
        return 0;
    }

    /// ***
    activate ( application );

    return 0;
}

static int local_options_clbk ( G_GNUC_UNUSED GApplication *app,
                                GVariantDict *options )
{
    gboolean version = FALSE;

    g_variant_dict_lookup ( options, "version", "b", &version );

    if ( version )
    {
        const guint major_version = gtk_get_major_version ();
        const guint minor_version = gtk_get_minor_version ();
        const guint micro_version = gtk_get_micro_version ();

        /// ***
        g_print ( "GTK-Version: %d.%d.%d\n", major_version, minor_version, micro_version );
        return 0;
    }

    return -1;
}

gint main ( void )
{
    GApplication *application;
    gint status;

    /// ***
    g_set_application_name ( "My GApplication Program" );

    /// ***
    application = g_application_new ( "my.gtk.app", G_APPLICATION_HANDLES_COMMAND_LINE );

    /// ***
    g_application_hold ( application );

    /// ***
    g_application_add_main_option ( G_APPLICATION ( application ), "version",    0, 0, G_OPTION_ARG_NONE,   "The program version",            NULL );
    g_application_add_main_option ( G_APPLICATION ( application ), "noactivate", 0, 0, G_OPTION_ARG_STRING, "Activate should not take place", "QUIT" );

    /// ***
    g_signal_connect_swapped ( application, "activate",             G_CALLBACK ( activate ),           application );
    g_signal_connect_swapped ( application, "startup",              G_CALLBACK ( startup_clbk ),       application );
    g_signal_connect_swapped ( application, "command-line",         G_CALLBACK ( command_line_clbk ),  application );
    g_signal_connect_swapped ( application, "handle-local-options", G_CALLBACK ( local_options_clbk ), application );

    /// ***
    status = g_application_run ( application, 0, NULL );

    /// ***
    g_object_unref ( application );

    /// ***
    return status;
}
g_application_run ( application, 0, NULL );

The second and third parameter are argc and argv. You aren’t passing on the actual command line parameters, so GApplication behaves as if no parameters were specified at all.

1 Like

I am filling like an Idiot right now :).
I made so many changes to my code that I have not realized that the arguments was removded.

Thank you

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.