1.4 <GApplicationCommandLine>

Class: <GApplicationCommandLine>

It derives from <GObject>.

<GApplicationCommandLine> represents a command-line invocation of an application.

It is created by <GApplication> and emitted in the application: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 application-command-line:get-arguments function. See gapplication-example-cmdline3.c 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.

For remote invocation, the originally-invoked process exits when application-command-line:done method is called. This method is also automatically called when the object is disposed.

The main use for <GApplicationCommandLine> (and the application: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 application: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.

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;
}

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

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

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

  argv = *arguments;

  if (argv[0] == NULL)
    {
      *exit_status = 0;
      return FALSE;
    }

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

  *exit_status = 0;

  return FALSE;
}

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

  ...
}

In this example of split commandline handling, options that start with --local- are handled locally, all other options are passed to the application: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.

static gboolean
my_cmdline_handler (gpointer data)
{
  GApplicationCommandLine *cmdline = data;

  // do the heavy lifting in an idle

  g_application_command_line_set_exit_status (cmdline, 0);
  g_object_unref (cmdline); // this releases the application

  return G_SOURCE_REMOVE;
}

static int
command_line (GApplication            *application,
              GApplicationCommandLine *cmdline)
{
  // keep the application running until we are done with this commandline
  g_application_hold (application);

  g_object_set_data_full (G_OBJECT (cmdline),
                          "application", application,
                          (GDestroyNotify)g_application_release);

  g_object_ref (cmdline);
  g_idle_add (my_cmdline_handler, cmdline);

  return 0;
}

In this example the commandline is not completely handled before the application: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

Properties

Property: application-command-line:arguments self

The commandline that caused this application:command-line signal emission.

Its value is a <GVariant>, or #f.

It can only be set when the object is made, with #:arguments. It cannot be read.

Property: application-command-line:is-remote self

Whether this is a remote commandline.

Its value is a boolean.

It is read only.

Property: application-command-line:options self

The options sent along with the commandline.

Its value is a <GVariant>, or #f.

It can only be set when the object is made, with #:options. It cannot be read.

Property: application-command-line:platform-data self

Platform-specific data for the commandline.

Its value is a <GVariant>, or #f.

It can only be set when the object is made, with #:platform-data. It cannot be read.

Procedures

Method: application-command-line:create-file-for-arg self arg ⇒ %return
Method: create-file-for-arg self arg ⇒ %return

Creates a <GFile> corresponding to a filename that was given as part of the invocation of self.

This differs from file-new-for-commandline-arg in that it resolves relative pathnames using the current working directory of the invoking process rather than the local process.

self

Passed: a <GApplicationCommandLine>.

a <GApplicationCommandLine>

arg

Passed: a file name, as a string.

an argument from self

%return

Returned: a <GFile>.

a new <GFile>

Method: application-command-line:done self
Method: done self

Signals that command line processing is completed.

For remote invocation, it causes the invoking process to terminate.

For local invocation, it does nothing.

This method should be called in the application:command-line handler, after the exit status is set and all messages are printed.

After this call, application-command-line:set-exit-status has no effect. Subsequent calls to this method are no-ops.

This method is automatically called when the <GApplicationCommandLine> object is disposed — so you can omit the call in non-garbage collected languages.

self

Passed: a <GApplicationCommandLine>.

a <GApplicationCommandLine>

Method: application-command-line:get-arguments self ⇒ %return
Method: get-arguments self ⇒ %return

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

The strings in the array may contain non-UTF-8 data on UNIX (such as filenames or arguments given in the system locale) but are always in UTF-8 on Windows.

If you wish to use the return value with GOptionContext, you must use g_option_context_parse_strv().

The return value is #f-terminated and should be freed using strfreev.

self

Passed: a <GApplicationCommandLine>.

a <GApplicationCommandLine>

%return

Returned: a vector, each a file name, as a string.

the string array containing the arguments (the argv)

argc

Not returned: it is the length of %return.

the length of the arguments array, or #f

Method: application-command-line:get-cwd self ⇒ %return
Method: get-cwd self ⇒ %return

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 #f.

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

self

Passed: a <GApplicationCommandLine>.

a <GApplicationCommandLine>

%return

Returned: a file name, as a string, or #f.

the current directory, or #f

Method: application-command-line:get-environ self ⇒ %return
Method: get-environ self ⇒ %return

Gets the contents of the ’environ’ variable of the command line invocation, as would be returned by get-environ, ie as a #f-terminated list of strings in the form ’NAME=VALUE’. The strings may contain non-utf8 data.

The remote application usually does not send an environment. Use 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 self exists.

See application-command-line:getenv if you are only interested in the value of a single environment variable.

self

Passed: a <GApplicationCommandLine>.

a <GApplicationCommandLine>

%return

Returned: a vector, each a file name, as a string, or #f.

the environment strings, or #f if they were not sent

Method: application-command-line:get-exit-status self ⇒ %return
Method: get-exit-status self ⇒ %return

Gets the exit status of self. See application-command-line:set-exit-status for more information.

self

Passed: a <GApplicationCommandLine>.

a <GApplicationCommandLine>

%return

Returned: a 32-bit integer.

the exit status

Method: application-command-line:get-is-remote? self ⇒ %return
Method: get-is-remote? self ⇒ %return

Determines if self represents a remote invocation.

self

Passed: a <GApplicationCommandLine>.

a <GApplicationCommandLine>

%return

Returned: a boolean.

#t if the invocation was remote

Method: application-command-line:get-options-dict self ⇒ %return
Method: get-options-dict self ⇒ %return

Gets the options that were passed to g_application_command_line().

If you did not override local_command_line() then these are the same options that were parsed according to the GOptionEntrys added to the application with g_application_add_main_option_entries() and possibly modified from your GApplication::handle-local-options handler.

If no options were sent then an empty dictionary is returned so that you don’t need to check for #f.

The data has been passed via an untrusted external process, so the types of all values must be checked before being used.

self

Passed: a <GApplicationCommandLine>.

a <GApplicationCommandLine>

%return

Returned: a <GVariantDict>.

a <GVariantDict> with the options

Method: application-command-line:get-platform-data self ⇒ %return
Method: get-platform-data self ⇒ %return

Gets the platform data associated with the invocation of self.

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

It comes from an untrusted external process and hence the types of all values must be validated before being used.

For local invocation, it will be #f.

self

Passed: a <GApplicationCommandLine>.

<GApplicationCommandLine>

%return

Returned: a <GVariant>, or #f.

the platform data, or #f

Method: application-command-line:get-stdin self ⇒ %return
Method: get-stdin self ⇒ %return

Gets the stdin of the invoking process.

The <GInputStream> can be used to read data passed to the standard input of the invoking process. This doesn’t work on all platforms. Presently, it is only available on UNIX when using a D-Bus daemon capable of passing file descriptors. If stdin is not available then #f will be returned. In the future, support may be expanded to other platforms.

You must only call this function once per commandline invocation.

self

Passed: a <GApplicationCommandLine>.

a <GApplicationCommandLine>

%return

Returned: a <GInputStream>, or #f.

a <GInputStream> for stdin

Method: application-command-line:getenv self name ⇒ %return
Method: getenv self name ⇒ %return

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

The remote application usually does not send an environment. Use 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 self exists.

self

Passed: a <GApplicationCommandLine>.

a <GApplicationCommandLine>

name

Passed: a file name, as a string.

the environment variable to get

%return

Returned: a string, or #f.

the value of the variable, or #f if unset or unsent

Method: application-command-line:print-literal self message
Method: print-literal self message

Prints a message using the stdout print handler in the invoking process.

Unlike g_application_command_line_print(), message is not a printf()-style format string. Use this function if message contains text you don’t have control over, that could include printf() escape sequences.

self

Passed: a <GApplicationCommandLine>.

a <GApplicationCommandLine>

message

Passed: a string.

the message

Method: application-command-line:printerr-literal self message
Method: printerr-literal self message

Prints a message using the stderr print handler in the invoking process.

Unlike g_application_command_line_printerr(), message is not a printf()-style format string. Use this function if message contains text you don’t have control over, that could include printf() escape sequences.

self

Passed: a <GApplicationCommandLine>.

a <GApplicationCommandLine>

message

Passed: a string.

the message

Method: application-command-line:set-exit-status self exit-status
Method: set-exit-status self exit-status

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

The return value of the application: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 self. 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.

This method is a no-op if application-command-line:done has been called.

self

Passed: a <GApplicationCommandLine>.

a <GApplicationCommandLine>

exit-status

Passed: a 32-bit integer.

the exit status

Virtual methods

Virtual method: application-command-line:do-done implementor self

Signals that command line processing is completed.

For remote invocation, it causes the invoking process to terminate.

For local invocation, it does nothing.

This method should be called in the application:command-line handler, after the exit status is set and all messages are printed.

After this call, application-command-line:set-exit-status has no effect. Subsequent calls to this method are no-ops.

This method is automatically called when the <GApplicationCommandLine> object is disposed — so you can omit the call in non-garbage collected languages.

To override it in a new class, give register-type #:vfuncs `((done . ,procedure)). The procedure is called as (procedure self).

implementor

Passed: the class whose implementation to call: <GApplicationCommandLine>, or a class that derives from it, or its GType number.

self

Passed: a <GApplicationCommandLine>.

a <GApplicationCommandLine>

Virtual method: application-command-line:do-get-stdin implementor self ⇒ %return

Gets the stdin of the invoking process.

The <GInputStream> can be used to read data passed to the standard input of the invoking process. This doesn’t work on all platforms. Presently, it is only available on UNIX when using a D-Bus daemon capable of passing file descriptors. If stdin is not available then #f will be returned. In the future, support may be expanded to other platforms.

You must only call this function once per commandline invocation.

To override it in a new class, give register-type #:vfuncs `((get-stdin . ,procedure)). The procedure is called as (procedure self) ⇒ %return.

implementor

Passed: the class whose implementation to call: <GApplicationCommandLine>, or a class that derives from it, or its GType number.

self

Passed: a <GApplicationCommandLine>.

a <GApplicationCommandLine>

%return

Returned: a <GInputStream>, or #f.

a <GInputStream> for stdin

Virtual method: application-command-line:do-print-literal implementor self message

Prints a message using the stdout print handler in the invoking process.

Unlike g_application_command_line_print(), message is not a printf()-style format string. Use this function if message contains text you don’t have control over, that could include printf() escape sequences.

To override it in a new class, give register-type #:vfuncs `((print-literal . ,procedure)). The procedure is called as (procedure self message).

implementor

Passed: the class whose implementation to call: <GApplicationCommandLine>, or a class that derives from it, or its GType number.

self

Passed: a <GApplicationCommandLine>.

a <GApplicationCommandLine>

message

Passed: a string.

the message

Virtual method: application-command-line:do-printerr-literal implementor self message

Prints a message using the stderr print handler in the invoking process.

Unlike g_application_command_line_printerr(), message is not a printf()-style format string. Use this function if message contains text you don’t have control over, that could include printf() escape sequences.

To override it in a new class, give register-type #:vfuncs `((printerr-literal . ,procedure)). The procedure is called as (procedure self message).

implementor

Passed: the class whose implementation to call: <GApplicationCommandLine>, or a class that derives from it, or its GType number.

self

Passed: a <GApplicationCommandLine>.

a <GApplicationCommandLine>

message

Passed: a string.

the message