diff --git a/CHANGELOG.md b/CHANGELOG.md index cf44d1ace..7219e625e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - The `short_channel_id` separator has been changed to be `x` to match the specification. - JSON API: `listpeers` now includes `funding_allocation_msat`, which returns a map of the amounts initially funded to the channel by each peer, indexed by channel id. - `option_data_loss_protect` is now enabled by default. +- JSON API: `help` with a `command` argument gives a JSON array, like other commands. ### Deprecated diff --git a/lightningd/jsonrpc.c b/lightningd/jsonrpc.c index 3f6427ed9..3d9d7bb49 100644 --- a/lightningd/jsonrpc.c +++ b/lightningd/jsonrpc.c @@ -329,6 +329,17 @@ static void json_add_help_command(struct command *cmd, } +static const struct json_command *find_command(struct json_command **commands, + const char *buffer, + const jsmntok_t *cmdtok) +{ + for (size_t i = 0; i < tal_count(commands); i++) { + if (json_tok_streq(buffer, cmdtok, commands[i]->name)) + return commands[i]; + } + return NULL; +} + static struct command_result *json_help(struct command *cmd, const char *buffer, const jsmntok_t *obj UNNEEDED, @@ -337,6 +348,7 @@ static struct command_result *json_help(struct command *cmd, struct json_stream *response; const jsmntok_t *cmdtok; struct json_command **commands = cmd->ld->jsonrpc->commands; + const struct json_command *one_cmd; if (!param(cmd, buffer, params, p_opt("command", param_tok, &cmdtok), @@ -344,29 +356,25 @@ static struct command_result *json_help(struct command *cmd, return command_param_failed(); if (cmdtok) { - for (size_t i = 0; i < tal_count(commands); i++) { - if (json_tok_streq(buffer, cmdtok, commands[i]->name)) { - response = json_stream_success(cmd); - json_add_help_command(cmd, response, commands[i]); - goto done; - } - } - return command_fail(cmd, JSONRPC2_METHOD_NOT_FOUND, - "Unknown command '%.*s'", - cmdtok->end - cmdtok->start, - buffer + cmdtok->start); - } + one_cmd = find_command(commands, buffer, cmdtok); + if (!one_cmd) + return command_fail(cmd, JSONRPC2_METHOD_NOT_FOUND, + "Unknown command '%.*s'", + cmdtok->end - cmdtok->start, + buffer + cmdtok->start); + } else + one_cmd = NULL; response = json_stream_success(cmd); json_object_start(response, NULL); json_array_start(response, "help"); - for (size_t i=0; i