Gio.ApplicationCommandLine
Description: Reference for Gio.ApplicationCommandLine

### Class

# GioApplicationCommandLine

#### Description [src]

    class Gio.ApplicationCommandLine : GObject.Object
    {
      /* No available fields */
    }

`GApplicationCommandLine` represents a command-line invocation of an application.

It is created by `GApplication` and emitted in the `GApplication::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 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 `g_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 `GApplication::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 `GApplication::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 `GApplication::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 `GApplication::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.

#### Ancestors

- GObject

#### Instance methods

###### g_application_command_line_create_file_for_arg

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

since: 2.36

###### g_application_command_line_done

Signals that command line processing is completed.

since: 2.80

###### g_application_command_line_get_arguments

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

since: 2.28

###### g_application_command_line_get_cwd

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

since: 2.28

###### g_application_command_line_get_environ

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.

since: 2.28

###### g_application_command_line_get_exit_status

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

since: 2.28

###### g_application_command_line_get_is_remote

Determines if `cmdline` represents a remote invocation.

since: 2.28

###### g_application_command_line_get_options_dict

Gets the options that were passed to g_application_command_line().

since: 2.40

###### g_application_command_line_get_platform_data

Gets the platform data associated with the invocation of `cmdline`.

since: 2.28

###### g_application_command_line_get_stdin

Gets the stdin of the invoking process.

since: 2.34

###### g_application_command_line_getenv

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.

since: 2.28

###### g_application_command_line_print

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

since: 2.28

###### g_application_command_line_print_literal

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

since: 2.80

###### g_application_command_line_printerr

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

since: 2.28

###### g_application_command_line_printerr_literal

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

since: 2.80

###### g_application_command_line_set_exit_status

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

since: 2.28

##### Methods inherited from GObject (43)

Please see GObject for a full list of methods.

#### Properties

###### Gio.ApplicationCommandLine:arguments

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

since: 2.28

###### Gio.ApplicationCommandLine:is-remote

Whether this is a remote commandline.

since: 2.28

###### Gio.ApplicationCommandLine:options

The options sent along with the commandline.

since: 2.28

###### Gio.ApplicationCommandLine:platform-data

Platform-specific data for the commandline.

since: 2.28

#### Signals

##### Signals inherited from GObject (1)

###### GObject::notify

The notify signal is emitted on an object when one of its properties has its value set through g_object_set_property(), g_object_set(), et al.

#### Class structure

    struct GioApplicationCommandLineClass {
      void (* print_literal) (
        GApplicationCommandLine* cmdline,
        const gchar* message
      );
      void (* printerr_literal) (
        GApplicationCommandLine* cmdline,
        const gchar* message
      );
      GInputStream* (* get_stdin) (
        GApplicationCommandLine* cmdline
      );
      void (* done) (
        GApplicationCommandLine* cmdline
      );

    }

The `GApplicationCommandLineClass`-struct contains private data only.

###### Class members

`print_literal: void (* print_literal) ( GApplicationCommandLine* cmdline, const gchar* message )`

  No description available.

`printerr_literal: void (* printerr_literal) ( GApplicationCommandLine* cmdline, const gchar* message )`

  No description available.

`get_stdin: GInputStream* (* get_stdin) ( GApplicationCommandLine* cmdline )`

  No description available.

`done: void (* done) ( GApplicationCommandLine* cmdline )`

  No description available.

#### Virtual methods

###### Gio.ApplicationCommandLineClass.done

Signals that command line processing is completed.

since: 2.80

###### Gio.ApplicationCommandLineClass.get_stdin

Gets the stdin of the invoking process.

since: 2.34

###### Gio.ApplicationCommandLineClass.print_literal

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

since: 2.80

###### Gio.ApplicationCommandLineClass.printerr_literal

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

since: 2.80