Passing flags to Adw.Application in PyGObject again

I recently asked about Adw application flags and how to pass multiple flags:

And at the time, I thought everything worked OK. But then I noticed that I can’t have both set at the same time. Here is a test app:

#!/usr/bin/env python3

# Python imports
import binascii  # Convert between binary and ASCII
import codecs  # Codec registry and base classes
import os  # Miscellaneous operating system interfaces
import sys  # System-specific parameters and functions
import threading  # Thread-based parallelism

# Adwaita, Gio, GLib, GObject, Gtk imports and versions
import gi
gi.require_version('Adw', '1')
gi.require_version('Gio', '2.0')
gi.require_version('GLib', '2.0')
gi.require_version('GObject', '2.0')
gi.require_version('Gtk', '4.0')
from gi.repository import Adw, Gio, GLib, GObject, Gtk  # noqa: E402

# Define constants
APP_ID = 'com.github.flagtest'

class MainWindow(Gtk.ApplicationWindow):

    """Main application window."""

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)


class flagTest(Adw.Application):

    def __init__(self, *args, **kwargs):
        super().__init__(
            *args,
            **kwargs,
            application_id=APP_ID,
            flags=Gio.ApplicationFlags.HANDLES_COMMAND_LINE | 
            Gio.ApplicationFlags.HANDLES_OPEN
                  
        )
        self.files = []
        self.add_main_option(
            "test",
            ord("t"),
            GLib.OptionFlags.NONE,
            GLib.OptionArg.NONE,
            "Command line test",
            None,
        )
        self.connect("activate", self.on_activate)

    def on_activate(self, app):
        self.win = MainWindow(application=app)
        self.win.present()

    def do_command_line(self, command_line):
        options = command_line.get_options_dict()
        # convert GVariantDict -> GVariant -> dict
        options = options.end().unpack()

        if "test" in options:
            print("test enabled")
            # This is printed on the main instance
            pass

        # Activate application
        self.activate()

        return 0

    # File provided from the command line
    def do_open(self, files, n_files, hint):

        print("do_open:", files)

        # Activate application
        self.activate()

        return


app = flagTest()
app.run(sys.argv)

The “do_open” print statement is only being printed when I use the “HANDLES_OPEN” flag and only that flag. But then the command-line option “-t” doesn’t work (“test enabled” is not printed). I think the user tried to advise me to only use the “HANDLES_OPEN” flag since it enables the “HANDLES_COMMAND_LINE”, but I am not sure that works now.

Even if I use the pipe character to separate multiple flags, the result is the same, “HANDLES_COMMAND_LINE” works, “HANDLES_OPEN” does not.

Is this even achievable?

Bump. Anyone knows how to help me out? :slight_smile:

Hey! I’d suggest reading the docs of Gio.Application.run, GioApplication::handle-local-options and other linked docs there.

It largely depends on your use case, but if you want to handle command line (with HANDLES_COMMAND_LINE), you’d have to go with something like this:

    def do_command_line(self, command_line):
        options = command_line.get_options_dict()
        options = options.end().unpack()

        if "test" in options:
            print("test enabled")
            pass

        args = command_line.get_arguments()[1:]
        if args:
            # you need to extract the file path manually
            self.open_file(Gio.File.new_for_path(args[0]))
        else:
            self.activate()

        return 0

    def open_file(self, file):
        print(file.get_path())
        self.activate()
        return 0

but if you only care about the local instance, you can remove the HANDLES_COMMAND_LINE flag and handle the command line in do_handle_local_options which will automatically call activate and open based on the options or lack thereof

    def do_handle_local_options(self, options_dict):
        options = options_dict.end().unpack()
        if "test" in options:
            print("test enabled")
        return -1
3 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.