Skip to content Skip to sidebar Skip to footer

Multiple Arguments In Gio.subprocess

I'm developing my first gnome-shell-extension currently. In the extension, I want to execute a simple shell command and use the output afterwards, for which I use Gio.Subprocess li

Solution 1:

Sorry, I haven't managed to finish that page (that's why it's in my sandbox 😉).

Here is our Promise wrapper for running a subprocess:

functionexecCommand(argv, input = null, cancellable = null) {
    let flags = Gio.SubprocessFlags.STDOUT_PIPE;

    if (input !== null)
        flags |= Gio.SubprocessFlags.STDIN_PIPE;

    let proc = newGio.Subprocess({
        argv: argv,
        flags: flags
    });
    proc.init(cancellable);

    returnnewPromise((resolve, reject) => {
        proc.communicate_utf8_async(input, cancellable, (proc, res) => {
            try {
                resolve(proc.communicate_utf8_finish(res)[1]);
            } catch (e) {
                reject(e);
            }
        });
    });
}

Now you have two reasonable choices, since you have a nice wrapper.

I would prefer this option myself, because if I'm launching sequential processes I probably want to know which failed, what the error was and so on. I really wouldn't worry about extra overhead, since the second process only executes if the first succeeds, at which point the first will have been garbage collected.

asyncfunctiondualCall() {
    try {
        let stdout1 = awaitexecCommand(['ProgramXYZ', '-a', '-bc']);
        let stdout2 = awaitexecCommand(['ProgramB']);
    } catch (e) {
        logError(e);
    }
}

On the other hand, there is nothing preventing you from launching a sub-shell if you really want to do shell stuff. Ultimately you're just offloading the same behaviour to a shell, though:

asyncfunctionshellCall() {
    try {
        let stdout = awaitexecCommand([
            '/bin/sh',
            '-c',
            'ProgramXYZ -a -bc && ProgramB'
        ]);
    } catch (e) {
        logError(e);
    }
}

Post a Comment for "Multiple Arguments In Gio.subprocess"