Use local options for command line
This commit is contained in:
@@ -66,10 +66,27 @@ class CartridgesApplication(Adw.Application):
|
|||||||
|
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
shared.store = Store()
|
shared.store = Store()
|
||||||
super().__init__(
|
super().__init__(application_id=shared.APP_ID)
|
||||||
application_id=shared.APP_ID,
|
|
||||||
flags=Gio.ApplicationFlags.HANDLES_COMMAND_LINE,
|
search = GLib.OptionEntry()
|
||||||
)
|
search.long_name = "search"
|
||||||
|
search.short_name = ord("s")
|
||||||
|
search.flags = 0
|
||||||
|
search.arg = int(GLib.OptionArg.STRING)
|
||||||
|
search.arg_data = None
|
||||||
|
search.description = "Open the app with this term in the search entry"
|
||||||
|
search.arg_description = "TERM"
|
||||||
|
|
||||||
|
launch = GLib.OptionEntry()
|
||||||
|
launch.long_name = "launch"
|
||||||
|
launch.short_name = ord("l")
|
||||||
|
launch.flags = 0
|
||||||
|
launch.arg = int(GLib.OptionArg.STRING)
|
||||||
|
launch.arg_data = None
|
||||||
|
launch.description = "Run a game with the given game_id"
|
||||||
|
launch.arg_description = "GAME_ID"
|
||||||
|
|
||||||
|
self.add_main_option_entries((search, launch))
|
||||||
|
|
||||||
def do_activate(self) -> None: # pylint: disable=arguments-differ
|
def do_activate(self) -> None: # pylint: disable=arguments-differ
|
||||||
"""Called on app creation"""
|
"""Called on app creation"""
|
||||||
@@ -159,47 +176,40 @@ class CartridgesApplication(Adw.Application):
|
|||||||
|
|
||||||
shared.win.present()
|
shared.win.present()
|
||||||
|
|
||||||
def do_command_line(self, command_line) -> int:
|
def do_handle_local_options(self, options: GLib.VariantDict) -> int:
|
||||||
for index, arg in enumerate(args := command_line.get_arguments()):
|
if search := options.lookup_value("search"):
|
||||||
if arg == "--search":
|
self.init_search_term = search.get_string()
|
||||||
try:
|
elif game_id := options.lookup_value("launch"):
|
||||||
self.init_search_term = args[index + 1]
|
try:
|
||||||
except IndexError:
|
data = json.load(
|
||||||
pass
|
(path := shared.games_dir / (game_id.get_string() + ".json")).open(
|
||||||
break
|
"r", encoding="utf-8"
|
||||||
|
|
||||||
if arg == "--launch":
|
|
||||||
try:
|
|
||||||
game_id = args[index + 1]
|
|
||||||
data = json.load(
|
|
||||||
(path := shared.games_dir / (game_id + ".json")).open(
|
|
||||||
"r", encoding="utf-8"
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
executable = (
|
|
||||||
shlex.join(data["executable"])
|
|
||||||
if isinstance(data["executable"], list)
|
|
||||||
else data["executable"]
|
|
||||||
)
|
|
||||||
name = data["name"]
|
|
||||||
|
|
||||||
run_executable(executable)
|
|
||||||
|
|
||||||
data["last_played"] = int(time())
|
|
||||||
json.dump(data, path.open("w", encoding="utf-8"))
|
|
||||||
|
|
||||||
except (IndexError, KeyError, OSError, json.decoder.JSONDecodeError):
|
|
||||||
return 1
|
|
||||||
|
|
||||||
notification = Gio.Notification.new(_("Cartridges"))
|
|
||||||
notification.set_body(_("{} launched").format(name))
|
|
||||||
self.send_notification(
|
|
||||||
"launch",
|
|
||||||
notification,
|
|
||||||
)
|
)
|
||||||
return 0
|
executable = (
|
||||||
self.activate()
|
shlex.join(data["executable"])
|
||||||
return 0
|
if isinstance(data["executable"], list)
|
||||||
|
else data["executable"]
|
||||||
|
)
|
||||||
|
name = data["name"]
|
||||||
|
|
||||||
|
run_executable(executable)
|
||||||
|
|
||||||
|
data["last_played"] = int(time())
|
||||||
|
json.dump(data, path.open("w", encoding="utf-8"))
|
||||||
|
|
||||||
|
except (IndexError, KeyError, OSError, json.decoder.JSONDecodeError):
|
||||||
|
return 1
|
||||||
|
|
||||||
|
self.register()
|
||||||
|
notification = Gio.Notification.new(_("Cartridges"))
|
||||||
|
notification.set_body(_("{} launched").format(name))
|
||||||
|
self.send_notification(
|
||||||
|
"launch",
|
||||||
|
notification,
|
||||||
|
)
|
||||||
|
return 0
|
||||||
|
return -1
|
||||||
|
|
||||||
def load_games_from_disk(self) -> None:
|
def load_games_from_disk(self) -> None:
|
||||||
if shared.games_dir.is_dir():
|
if shared.games_dir.is_dir():
|
||||||
|
|||||||
Reference in New Issue
Block a user