Struct gio::ApplicationCommandLine [−][src]
pub struct ApplicationCommandLine(_);
Expand description
ApplicationCommandLine
represents a command-line invocation of
an application. It is created by Application
and emitted
in the signal::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 ApplicationCommandLineExt::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 ApplicationCommandLine
(and the
signal::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
signal::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.
⚠️ The following code is in C ⚠️
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 comandline can be split between the launcher and the primary instance.
⚠️ The following code is in C ⚠️
static gboolean
test_local_cmdline (GApplication *application,
gchar ***arguments,
gint *exit_status)
{
gint i, j;
gchar **argv;
argv = *arguments;
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 signal::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.
⚠️ The following code is in C ⚠️
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 signal::Application::command-line
handler returns. Instead, we keep
a reference to the ApplicationCommandLine
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
Implements
Trait Implementations
This method returns an ordering between self
and other
values if one exists. Read more
This method tests less than (for self
and other
) and is used by the <
operator. Read more
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
Returns the type identifier of Self
.
Auto Trait Implementations
impl RefUnwindSafe for ApplicationCommandLine
impl !Send for ApplicationCommandLine
impl !Sync for ApplicationCommandLine
impl Unpin for ApplicationCommandLine
impl UnwindSafe for ApplicationCommandLine
Blanket Implementations
Mutably borrows from an owned value. Read more
Upcasts an object to a superclass or interface T
. Read more
Upcasts an object to a reference of its superclass or interface T
. Read more
Tries to downcast to a subclass or interface implementor T
. Read more
Tries to downcast to a reference of its subclass or interface implementor T
. Read more
Tries to cast to an object of type T
. This handles upcasting, downcasting
and casting between interface and interface implementors. All checks are performed at
runtime, while downcast
and upcast
will do many checks at compile-time already. Read more
Tries to cast to reference to an object of type T
. This handles upcasting, downcasting
and casting between interface and interface implementors. All checks are performed at
runtime, while downcast
and upcast
will do many checks at compile-time already. Read more
Casts to T
unconditionally. Read more
Casts to &T
unconditionally. Read more
Returns true
if the object is an instance of (can be cast to) T
.
pub fn set_properties_from_value(
&self,
property_values: &[(&str, Value)]
) -> Result<(), BoolError>
pub fn set_property<'a, N, V>(
&self,
property_name: N,
value: V
) -> Result<(), BoolError> where
V: ToValue,
N: Into<&'a str>,
pub fn set_property_from_value<'a, N>(
&self,
property_name: N,
value: &Value
) -> Result<(), BoolError> where
N: Into<&'a str>,
Safety Read more
Safety Read more
Safety Read more
Safety Read more
pub fn connect_notify<F>(&self, name: Option<&str>, f: F) -> SignalHandlerId where
F: 'static + Fn(&T, &ParamSpec) + Send + Sync,
pub fn connect_notify_local<F>(
&self,
name: Option<&str>,
f: F
) -> SignalHandlerId where
F: 'static + Fn(&T, &ParamSpec),
pub unsafe fn connect_notify_unsafe<F>(
&self,
name: Option<&str>,
f: F
) -> SignalHandlerId where
F: Fn(&T, &ParamSpec),
pub fn has_property<'a, N>(&self, property_name: N, type_: Option<Type>) -> bool where
N: Into<&'a str>,
pub fn find_property<'a, N>(&self, property_name: N) -> Option<ParamSpec> where
N: Into<&'a str>,
pub fn connect<'a, N, F>(
&self,
signal_name: N,
after: bool,
callback: F
) -> Result<SignalHandlerId, BoolError> where
F: Fn(&[Value]) -> Option<Value> + Send + Sync + 'static,
N: Into<&'a str>,
Same as connect
but takes a SignalId
instead of a signal name.
pub fn connect_local<'a, N, F>(
&self,
signal_name: N,
after: bool,
callback: F
) -> Result<SignalHandlerId, BoolError> where
F: Fn(&[Value]) -> Option<Value> + 'static,
N: Into<&'a str>,
Same as connect_local
but takes a SignalId
instead of a signal name.
pub unsafe fn connect_unsafe<'a, N, F>(
&self,
signal_name: N,
after: bool,
callback: F
) -> Result<SignalHandlerId, BoolError> where
F: Fn(&[Value]) -> Option<Value>,
N: Into<&'a str>,
Same as connect_unsafe
but takes a SignalId
instead of a signal name.
Emit signal by signal id.
Emit signal with details by signal id.
Emit signal by it’s name.
pub fn bind_property<'a, O, N, M>(
&'a self,
source_property: N,
target: &'a O,
target_property: M
) -> BindingBuilder<'a> where
O: ObjectType,
N: Into<&'a str>,
M: Into<&'a str>,
Same as emit
but takes Value
for the arguments.
Same as emit_by_name
but takes Value
for the arguments.
Returns a SendValue
clone of self
.
impl<'a, T, C> FromValueOptional<'a> for T where
C: ValueTypeChecker<Error = ValueTypeMismatchOrNoneError>,
T: FromValue<'a, Checker = C>,