From 791d6a1e4598b6eef953c74f2f21f5332d87c9cf Mon Sep 17 00:00:00 2001 From: Jason Moggridge Date: Sun, 2 Mar 2025 11:14:40 -0500 Subject: [PATCH] cli: add `callJson` ipc subcommand The `arguments` parser for the `ipc call` subcommand `allow_extra_args` does not gracefully handle many JSON strings. It will allow '{"hello": "world"}', but it will strip out square brackets and split the JSON string on commas. This is worked around by adding a new `callJson` ipc subcommand which accepts one string argument which is passed directly to the ipc call. --- src/launch/command.cpp | 7 +++++++ src/launch/launch_p.hpp | 2 ++ src/launch/parsecommand.cpp | 16 ++++++++++++++++ 3 files changed, 25 insertions(+) diff --git a/src/launch/command.cpp b/src/launch/command.cpp index 00ad613..3346da8 100644 --- a/src/launch/command.cpp +++ b/src/launch/command.cpp @@ -295,6 +295,13 @@ int ipcCommand(CommandState& cmd) { return qs::io::ipc::comm::queryMetadata(&client, *cmd.ipc.target, *cmd.ipc.name); } else if (*cmd.ipc.getprop) { return qs::io::ipc::comm::getProperty(&client, *cmd.ipc.target, *cmd.ipc.name); + } else if (*cmd.ipc.callJson) { + return qs::io::ipc::comm::callFunction( + &client, + *cmd.ipc.target, + *cmd.ipc.name, + {*cmd.ipc.jsonArgument} + ); } else { QVector arguments; for (auto& arg: cmd.ipc.arguments) { diff --git a/src/launch/launch_p.hpp b/src/launch/launch_p.hpp index 7780845..b0fb56d 100644 --- a/src/launch/launch_p.hpp +++ b/src/launch/launch_p.hpp @@ -71,10 +71,12 @@ struct CommandState { CLI::App* ipc = nullptr; CLI::App* show = nullptr; CLI::App* call = nullptr; + CLI::App* callJson = nullptr; CLI::App* getprop = nullptr; bool showOld = false; QStringOption target; QStringOption name; + QStringOption jsonArgument; std::vector arguments; } ipc; diff --git a/src/launch/parsecommand.cpp b/src/launch/parsecommand.cpp index 1edbf01..bf6c093 100644 --- a/src/launch/parsecommand.cpp +++ b/src/launch/parsecommand.cpp @@ -195,6 +195,22 @@ int parseCommand(int argc, char** argv, CommandState& state) { ->allow_extra_args(); } + { + auto* callJson = sub->add_subcommand( + "callJson", + "Call an IpcHandler function with a single JSON argument." + ); + state.ipc.callJson = callJson; + + callJson->add_option("target", state.ipc.target, "The target to message."); + + callJson->add_option("function", state.ipc.name) + ->description("The function to call in the target."); + + callJson->add_option("json", state.ipc.jsonArgument) + ->description("JSON string sent the called function."); + } + { auto* prop = sub->add_subcommand("prop", "Manipulate IpcHandler properties.")->require_subcommand();