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?