Hi!
I’m trying to understand how to define CLI arguments using the new Gtk4 model but I only managed to define non-positional / flagged arguments:
# (...)
class MyApp(Adw.Application):
def __init__(self, *args, **kwargs):
super().__init__(
*args,
application_id="org.example.myapp",
flags=Gio.ApplicationFlags.HANDLES_COMMAND_LINE |
Gio.ApplicationFlags.HANDLES_OPEN,
**kwargs
)
self.connect('activate', self.on_activate)
self.add_main_option(
"test",
ord("t"), # <= This works
GLib.OptionFlags.NONE,
GLib.OptionArg.NONE,
"Command line test",
None,
)
self.add_main_option(
"file",
0, # <= This doesn't
GLib.OptionFlags.NONE,
GLib.OptionArg.FILENAME,
"File to load",
None,
)
def on_activate(self, app):
self.win = MainWindow(application=app)
self.win.present()
def on_quit(self, action, param):
self.quit()
def do_open(self, action, param):
print("Now what")
def do_command_line(self, command_line):
options = command_line.get_options_dict()
options = options.end().unpack()
if "test" in options:
print("Argument: %s" % options["test"])
if "file" in options: # <= And so this never fires
print("Argument: %s" % options["file"])
self.activate()
return 0
app = MyApp()
app.run(sys.argv)
Of course I tried every combination of OptionArg and OptionFlag constants.
This is really hard to google for, given that this API replaced argparse
, that itself (If I’m not mistaking) replaced something else before, so there is a lot of stale documentation out there
I can get the arguments by using
argv = command_line.get_arguments()
self.loaded_file = argv[1]
But that is not right, it’s totally bypassing the GtkApplication logic, and the help is not right:
# ./fwomaj -h
Usage:
fwomaj [OPTION…]
Help Options:
-h, --help Show help options
--help-all Show all help options
--help-gapplication Show GApplication options
Application Options:
-t, --test Command line test
--file Video file to load
It should say
Usage:
fwomaj [OPTION…] FILE
(...)
-t, --test Command line test
FILE Video file to load
So there is really something that I’m missing there.