ApplicationCommandLine

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 GApplicationCommandLine object can provide the argc and argv parameters for use with the GOptionContext command-line parsing API, with the g_application_command_line_get_arguments() function. See [gapplication-example-cmdline3.c][gapplication-example-cmdline3] for an example.

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).

The main use for GApplicationCommandLine (and the command-line signal) is 'Emacs server' like use cases: You can set the EDITOR environment variable to have e.g. git use your favourite editor to edit commit messages, and if you already have an instance of the editor running, the editing will happen in the running instance, instead of opening a new one. An important aspect of this use case is that the process that gets started by git does not return until the editing is done.

Normally, the commandline is completely handled in the command-line handler. The launching instance exits once the signal handler in the primary instance has returned, and the return value of the signal handler becomes the exit status of the launching instance.

1 
2 static int
3 command_line (GApplication            *application,
4 GApplicationCommandLine *cmdline)
5 {
6 gchar **argv;
7 gint argc;
8 gint i;
9 
10 argv = g_application_command_line_get_arguments (cmdline, &argc);
11 
12 g_application_command_line_print (cmdline,
13 "This text is written back\n"
14 "to stdout of the caller\n");
15 
16 for (i = 0; i < argc; i++)
17 g_print ("argument `d:` `s`\n", i, argv[i]);
18 
19 g_strfreev (argv);
20 
21 return 0;
22 }

he complete example can be found here: gapplication-example-cmdline.c

In more complicated cases, the handling of the comandline can be split between the launcher and the primary instance.

1 
2 static gboolean
3 test_local_cmdline (GApplication   *application,
4 gchar        ***arguments,
5 gint           *exit_status)
6 {
7 gint i, j;
8 gchar **argv;
9 
10 argv = *arguments;
11 
12 i = 1;
13 while (argv[i])
14 {
15 if (g_str_has_prefix (argv[i], "--local-"))
16 {
17 g_print ("handling argument `s` locally\n", argv[i]);
18 g_free (argv[i]);
19 for (j = i; argv[j]; j++)
20 argv[j] = argv[j + 1];
21 }
22 else
23 {
24 g_print ("not handling argument `s` locally\n", argv[i]);
25 i++;
26 }
27 }
28 
29 *exit_status = 0;
30 
31 return FALSE;
32 }
33 
34 static void
35 test_application_class_init (TestApplicationClass *class)
36 {
37 G_APPLICATION_CLASS (class)->local_command_line = test_local_cmdline;
38 
39 ...
40 }

n this example of split commandline handling, options that start with --local- are handled locally, all other options are passed to the command-line handler which runs in the primary instance.

The complete example can be found here: gapplication-example-cmdline2.c

If handling the commandline requires a lot of work, it may be better to defer it.

1 
2 static gboolean
3 my_cmdline_handler (gpointer data)
4 {
5 GApplicationCommandLine *cmdline = data;
6 
7 // do the heavy lifting in an idle
8 
9 g_application_command_line_set_exit_status (cmdline, 0);
10 g_object_unref (cmdline); // this releases the application
11 
12 return G_SOURCE_REMOVE;
13 }
14 
15 static int
16 command_line (GApplication            *application,
17 GApplicationCommandLine *cmdline)
18 {
19 // keep the application running until we are done with this commandline
20 g_application_hold (application);
21 
22 g_object_set_data_full (G_OBJECT (cmdline),
23 "application", application,
24 (GDestroyNotify)g_application_release);
25 
26 g_object_ref (cmdline);
27 g_idle_add (my_cmdline_handler, cmdline);
28 
29 return 0;
30 }

n this example the commandline is not completely handled before the command-line handler returns. Instead, we keep a reference to the GApplicationCommandLine object and handle it later (in this example, in an idle). Note that it is necessary to hold the application until you are done with the commandline.

The complete example can be found here: gapplication-example-cmdline3.c

Constructors

this
this(GApplicationCommandLine* gApplicationCommandLine, bool ownedRef = false)

Sets our main struct and passes it to the parent class.

Members

Functions

createFileForArg
FileIF createFileForArg(string arg)

Creates a GFile corresponding to a filename that was given as part of the invocation of cmdline.

getApplicationCommandLineStruct
GApplicationCommandLine* getApplicationCommandLineStruct(bool transferOwnership = false)

Get the main Gtk struct

getArguments
string[] getArguments()

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

getCwd
string getCwd()

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

getEnviron
string[] getEnviron()

Gets the contents of the 'environ' variable of the command line invocation, as would be returned by g_get_environ(), ie as a NULL-terminated list of strings in the form 'NAME=VALUE'. The strings may contain non-utf8 data.

getExitStatus
int getExitStatus()

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

getIsRemote
bool getIsRemote()

Determines if cmdline represents a remote invocation.

getOptionsDict
VariantDict getOptionsDict()

Gets the options there were passed to g_application_command_line().

getPlatformData
Variant getPlatformData()

Gets the platform data associated with the invocation of cmdline.

getStdin
InputStream getStdin()

Gets the stdin of the invoking process.

getStruct
void* getStruct()

the main Gtk struct as a void*

getenv
string getenv(string 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.

setExitStatus
void setExitStatus(int exitStatus)

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

Static functions

getType
GType getType()

Variables

gApplicationCommandLine
GApplicationCommandLine* gApplicationCommandLine;

the main Gtk struct