gtkpygtkglibpygobject

Appliction command line int option with value 0 not availible


I'm trying to use the command line option handler that comes with GLib/GTK so that the other GTK options are also available in my appliction.

When I have a GLib.OptionArg.INT option, and the value of 0 is provided, the option is not available from Gio.ApplicationCommandLine.get_options_dict()

I.e. The following code will print Value: None. I expect it to print Value: 0. How can I get it to do this?

import gi

gi.require_version("Gio", "2.0")
gi.require_version("GLib", "2.0")

from gi.repository import Gio, GLib


app = Gio.Application(
    application_id="test.command-line",
    flags=Gio.ApplicationFlags.HANDLES_COMMAND_LINE,
)

app.add_main_option(
    "value", 0, GLib.OptionFlags.NONE, GLib.OptionArg.INT, "Int value", None
)


def command_line(app, command_line: Gio.ApplicationCommandLine):
    command_line_dict = command_line.get_options_dict()
    value = command_line_dict.lookup_value("value", GLib.VariantType.new("i"))
    print(f"Value: {value}")
    return 0


app.connect("command_line", command_line)
app.run(["--value", "0"])

Solution

  • This is a known issue with passing options through GApplication if the user sets them to their default value (which in this case is zero).