GApplicationCommandLine

GApplicationCommandLine — A command-line invocation of an application

Synopsis

                    GApplicationCommandLine;
struct              GApplicationCommandLineClass;

gchar **                 g_application_command_line_get_arguments
                                                        (GApplicationCommandLine *cmdline,
                                                         int *argc);
const gchar *            g_application_command_line_get_cwd
                                                        (GApplicationCommandLine *cmdline);
const gchar * const * g_application_command_line_get_environ
                                                        (GApplicationCommandLine *cmdline);
const gchar *            g_application_command_line_getenv
                                                        (GApplicationCommandLine *cmdline,
                                                         const gchar *name);
gboolean            g_application_command_line_get_is_remote
                                                        (GApplicationCommandLine *cmdline);
GVariant *               g_application_command_line_get_platform_data
                                                        (GApplicationCommandLine *cmdline);

void                g_application_command_line_set_exit_status
                                                        (GApplicationCommandLine *cmdline,
                                                         int exit_status);
int                 g_application_command_line_get_exit_status
                                                        (GApplicationCommandLine *cmdline);

void                g_application_command_line_print    (GApplicationCommandLine *cmdline,
                                                         const gchar *format,
                                                         ...);
void                g_application_command_line_printerr (GApplicationCommandLine *cmdline,
                                                         const gchar *format,
                                                         ...);

Object Hierarchy

  GObject
   +----GApplicationCommandLine

Description

GApplicationCommandLine represents a command-line invocation of an application. It is created by GApplication and emitted in the "command-line" signal and virtual function.

The class contains the list of arguments that the program was invoked with. It is also possible to query if the commandline invocation was local (ie: the current process is running in direct response to the invocation) or remote (ie: some other process forwarded the commandline to this process).

The exit status of the originally-invoked process may be set and messages can be printed to stdout or stderr of that process. The lifecycle of the originally-invoked process is tied to the lifecycle of this object (ie: the process exits when the last reference is dropped).

Example 15. Handling commandline arguments with GApplication

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <gio/gio.h>
#include <stdlib.h>
#include <string.h>

static int
command_line (GApplication            *application,
              GApplicationCommandLine *cmdline)
{
  gchar **argv;
  gint argc;
  gint i;

  argv = g_application_command_line_get_arguments (cmdline, &argc);

  g_application_command_line_print (cmdline,
                                    "This text is written back\n"
                                    "to stdout of the caller\n");

  for (i = 0; i < argc; i++)
    g_print ("argument %d: %s\n", i, argv[i]);

  g_strfreev (argv);

  return 0;
}

int
main (int argc, char **argv)
{
  GApplication *app;
  int status;

  app = g_application_new ("org.gtk.TestApplication",
                           G_APPLICATION_HANDLES_COMMAND_LINE);
  g_signal_connect (app, "command-line", G_CALLBACK (command_line), NULL);
  g_application_set_inactivity_timeout (app, 10000);

  status = g_application_run (app, argc, argv);

  g_object_unref (app);

  return status;
}


Example 16. Complicated commandline handling

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <gio/gio.h>
#include <stdlib.h>
#include <string.h>

static int
command_line (GApplication            *application,
              GApplicationCommandLine *cmdline)
{
  gchar **argv;
  gint argc;
  gint i;

  argv = g_application_command_line_get_arguments (cmdline, &argc);

  for (i = 0; i < argc; i++)
    g_print ("handling argument %s remotely\n", argv[i]);

  g_strfreev (argv);

  return 0;
}

static gboolean
test_local_cmdline (GApplication   *application,
                    gchar        ***arguments,
                    gint           *exit_status)
{
  gint i, j;
  gchar **argv;

  argv = *arguments;

  for (i = 0; argv[i]; i++)
    {
      if (g_str_has_prefix (argv[i], "--local-"))
        {
          g_print ("handling argument %s locally\n", argv[i]);
          for (j = i + 1; argv[j]; j++)
            {
              argv[j - 1] = argv[j];
              argv[j] = NULL;
            }
        }
    }

  *exit_status = 0;

  return FALSE;
}

typedef GApplication TestApplication;
typedef GApplicationClass TestApplicationClass;

G_DEFINE_TYPE (TestApplication, test_application, G_TYPE_APPLICATION)

static void
test_application_finalize (GObject *object)
{
  G_OBJECT_CLASS (test_application_parent_class)->finalize (object);
}

static void
test_application_init (TestApplication *app)
{
}

static void
test_application_class_init (TestApplicationClass *class)
{
  G_OBJECT_CLASS (class)->finalize = test_application_finalize;
  G_APPLICATION_CLASS (class)->local_command_line = test_local_cmdline;
}

GApplication *
test_application_new (const gchar       *application_id,
                      GApplicationFlags  flags)
{
  g_return_val_if_fail (g_application_id_is_valid (application_id), NULL);

  g_type_init ();

  return g_object_new (test_application_get_type (),
                       "application-id", application_id,
                       "flags", flags,
                       NULL);
}

int
main (int argc, char **argv)
{
  GApplication *app;
  int status;

  app = test_application_new ("org.gtk.TestApplication", 0);
  g_application_set_inactivity_timeout (app, 10000);
  g_signal_connect (app, "command-line", G_CALLBACK (command_line), NULL);

  status = g_application_run (app, argc, argv);

  g_object_unref (app);

  return status;
}


Details

GApplicationCommandLine

typedef struct _GApplicationCommandLine GApplicationCommandLine;

The GApplicationCommandLine structure contains private data and should only be accessed using the provided API

Since 2.28


struct GApplicationCommandLineClass

struct GApplicationCommandLineClass {
};

The GApplicationCommandLineClass structure contains private data only

Since 2.28


g_application_command_line_get_arguments ()

gchar **                 g_application_command_line_get_arguments
                                                        (GApplicationCommandLine *cmdline,
                                                         int *argc);

Gets the list of arguments that was passed on the command line.

The strings in the array may contain non-utf8 data.

The return value is NULL-terminated and should be freed using g_strfreev().

cmdline :

a GApplicationCommandLine

argc :

the length of the arguments array, or NULL. [out]

Returns :

the string array containing the arguments (the argv). [array length=argc][transfer full]

Since 2.28


g_application_command_line_get_cwd ()

const gchar *            g_application_command_line_get_cwd
                                                        (GApplicationCommandLine *cmdline);

Gets the working directory of the command line invocation. The string may contain non-utf8 data.

It is possible that the remote application did not send a working directory, so this may be NULL.

The return value should not be modified or freed and is valid for as long as cmdline exists.

cmdline :

a GApplicationCommandLine

Returns :

the current directory, or NULL

Since 2.28


g_application_command_line_get_environ ()

const gchar * const * g_application_command_line_get_environ
                                                        (GApplicationCommandLine *cmdline);

Gets the contents of the 'environ' variable of the command line invocation, as would be returned by g_get_environ(). The strings may contain non-utf8 data.

The remote application usually does not send an environment. Use G_APPLICATION_SEND_ENVIRONMENT to affect that. Even with this flag set it is possible that the environment is still not available (due to invocation messages from other applications).

The return value should not be modified or freed and is valid for as long as cmdline exists.

cmdline :

a GApplicationCommandLine

Returns :

the environment strings, or NULL if they were not sent. [array zero-terminated=1][transfer none]

Since 2.28


g_application_command_line_getenv ()

const gchar *            g_application_command_line_getenv
                                                        (GApplicationCommandLine *cmdline,
                                                         const gchar *name);

Gets the value of a particular environment variable of the command line invocation, as would be returned by g_getenv(). The strings may contain non-utf8 data.

The remote application usually does not send an environment. Use G_APPLICATION_SEND_ENVIRONMENT to affect that. Even with this flag set it is possible that the environment is still not available (due to invocation messages from other applications).

The return value should not be modified or freed and is valid for as long as cmdline exists.

cmdline :

a GApplicationCommandLine

name :

the environment variable to get

Returns :

the value of the variable, or NULL if unset or unsent

Since 2.28


g_application_command_line_get_is_remote ()

gboolean            g_application_command_line_get_is_remote
                                                        (GApplicationCommandLine *cmdline);

Determines if cmdline represents a remote invocation.

cmdline :

a GApplicationCommandLine

Returns :

TRUE if the invocation was remote

Since 2.28


g_application_command_line_get_platform_data ()

GVariant *               g_application_command_line_get_platform_data
                                                        (GApplicationCommandLine *cmdline);

Gets the platform data associated with the invocation of cmdline.

This is a GVariant dictionary containing information about the context in which the invocation occured. It typically contains information like the current working directory and the startup notification ID.

For local invocation, it will be NULL.

cmdline :

GApplicationCommandLine

Returns :

the platform data, or NULL

Since 2.28


g_application_command_line_set_exit_status ()

void                g_application_command_line_set_exit_status
                                                        (GApplicationCommandLine *cmdline,
                                                         int exit_status);

Sets the exit status that will be used when the invoking process exits.

The return value of the "command-line" signal is passed to this function when the handler returns. This is the usual way of setting the exit status.

In the event that you want the remote invocation to continue running and want to decide on the exit status in the future, you can use this call. For the case of a remote invocation, the remote process will typically exit when the last reference is dropped on cmdline. The exit status of the remote process will be equal to the last value that was set with this function.

In the case that the commandline invocation is local, the situation is slightly more complicated. If the commandline invocation results in the mainloop running (ie: because the use-count of the application increased to a non-zero value) then the application is considered to have been 'successful' in a certain sense, and the exit status is always zero. If the application use count is zero, though, the exit status of the local GApplicationCommandLine is used.

cmdline :

a GApplicationCommandLine

exit_status :

the exit status

Since 2.28


g_application_command_line_get_exit_status ()

int                 g_application_command_line_get_exit_status
                                                        (GApplicationCommandLine *cmdline);

Gets the exit status of cmdline. See g_application_command_line_set_exit_status() for more information.

cmdline :

a GApplicationCommandLine

Returns :

the exit status

Since 2.28


g_application_command_line_print ()

void                g_application_command_line_print    (GApplicationCommandLine *cmdline,
                                                         const gchar *format,
                                                         ...);

Formats a message and prints it using the stdout print handler in the invoking process.

If cmdline is a local invocation then this is exactly equivalent to g_print(). If cmdline is remote then this is equivalent to calling g_print() in the invoking process.

cmdline :

a GApplicationCommandLine

format :

a printf-style format string

... :

arguments, as per format

Since 2.28


g_application_command_line_printerr ()

void                g_application_command_line_printerr (GApplicationCommandLine *cmdline,
                                                         const gchar *format,
                                                         ...);

Formats a message and prints it using the stderr print handler in the invoking process.

If cmdline is a local invocation then this is exactly equivalent to g_printerr(). If cmdline is remote then this is equivalent to calling g_printerr() in the invoking process.

cmdline :

a GApplicationCommandLine

format :

a printf-style format string

... :

arguments, as per format

Since 2.28

See Also

GApplication