1

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.

yPhil
  • 8,049
  • 4
  • 57
  • 83

1 Answers1

0

I got the solution / answer on the official Gtk community channel:

class MyApp(Adw.Application):
    def __init__(self, *args, **kwargs):
        super().__init__(
            *args,
            application_id="org.example.myapp",
            flags=Gio.ApplicationFlags.HANDLES_OPEN,
            **kwargs
        )
        self.connect('activate', self.on_activate)

        self.add_main_option(
            "test",
            ord("t"),
            GLib.OptionFlags.NONE,
            GLib.OptionArg.NONE,
            "Command line test",
            None,
        )

        self.set_option_context_parameter_string("FILE")
        self.set_option_context_summary("  FILE is The video file to load")
        self.set_option_context_description("where FILE is The video file to load")

Notice the flag change in the constructor ; Also apparently, file opening capabilities don't need extended documentation, just a app [OPTIONS] FILE mention in the --help.

yPhil
  • 8,049
  • 4
  • 57
  • 83